Bash: Add suffix to all files in a directory with extension

I want to add a suffix to all files in the current directory.

Here's what I still haven't looked for stackoverflow and Google (and messed around a bit), but it continues to fail. I want to add line 3.6.16 to all .png files in my directory. I can make part of the prefix, but my suffix fails (I guess due to the extension).

Using bash:

 for item in *.png; do mv "$file" "${file}_3.6.14.png"; done 
+7
bash
source share
2 answers
 for file in *.png; do mv "$file" "${file%.png}_3.6.14.png" done 

${file%.png} expands to ${file} with the removal of the suffix .png .

+20
source share

You can do this with the rename command,

 rename 's/\.png/_3.6.14.png/' *.png 

Via bash,

 for i in *.png; do mv "$i" "${i%.*}_3.6.14.png"; done 

It replaces .png in all .png files with _3.6.14.png .

  • ${i%.*} Anything after the last dot would be a shorthand. Thus, the .png part will be disconnected from the file name.
  • mv $i ${i%.*}_3.6.14.png Rename the original .png files with the file name + _3.6.14.png.
+8
source share

All Articles