Chriz74
Chriz74

Reputation: 1480

rename file extension from terminal doesn't work as expected

I have a bunch of files named like this:

file.jpg?sw=450&sh=450

I want to batch rename them removing that awful extension and get this:

file.jpg

I tried this script:

for file in *'.jpg?sw=450&sh=450'; do mv "$file" "${file%}".jpg; done

and also this script:

for file in *'.jpg?sw=450&sh=450'; do mv "$file" "${file%}'.jpg?sw=450&sh=450'".jpg; done

What happens is I get this result:

file.jpg?sw=450&sh=450.jpg

Upvotes: 0

Views: 82

Answers (1)

hunteke
hunteke

Reputation: 3716

Bash is one way to do it, although I might consider use of rename, which renames based on regular expressions. Consider:

$ rename 's/\.jpg\?.*$/.jpg/' *.jpg\?*

This says:

  • Given the files that match the glob *.jpg\?*
  • Replace, in the filenames, occurrences of .jpg that have a question mark following them and all characters after to the end: /\.jpg\?.*$/
  • With the simple string .jpg

Upvotes: 1

Related Questions