-1

I would like to rename all .jpg files in a folder on Linux through one line in the terminal. The filenames all end with numbers ranging from one to three digits. I would like to get rid of the numbers at the end of the file extension.

From:

file1.jpg62
file2.jpg193
file3.jpg3

To:

file1.jpg
file2.jpg
file3.jpg

What would a rename or mv command look like to do this?

  • You can use parameter expansion to get rid of everything past the last `.` (period), something like `${file%.*}`, and just add `.jpg` since all of the files are `.jpg`. I don't think your question can be answered with just *one* command, although I'm not familiar with the ins and outs of `sed` and `awk`. You would need to loop over the files and use `mv` or `rename` with the parameter expansion. – Jonny Henly Mar 07 '19 at 17:46

1 Answers1

1

strip the extension and just add it after match.

for i in * ; do mv "${i}" "${i%%.*}.jpg" ; done

this is for the usercase above only. it doesn't consider having duplicate files etc.

Eran
  • 26
  • 3
  • You can also use `for i in *.jpg*` to limit to those files, `${i%.*}` to only strip the last extension in the case of a name like `image.modified.v2.0.jpg42`, and optionally `mv -i` to not overwrite duplicates – that other guy Mar 07 '19 at 18:47