Add suffix to all files in the directory with an extension

If you are familiar with regular expressions sed is quite nice.

a) modify the regular expression to your liking and inspect the output

ls | sed -E "s/(.*)\.png$/\1_foo\.png/

b) add the p flag, so that sed provides you the old and new paths. Feed this to xargs with -n2, meaning that it should keep the pairing of 2 arguments.

ls | sed -E "p;s/(.*)\.png/\1_foo\.png/" | xargs -n2 mv


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

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


You could do this through rename command,

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

Through bash,

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

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

  • ${i%.*} Anything after last dot would be cutdown. So .png part would be cutoff from the filename.
  • mv $i ${i%.*}_3.6.14.png Rename original .png files with the filename+_3.6.14.png.

Tags:

Bash