Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add suffix to all files in the directory with an extension

Tags:

bash

How to add a suffix to all files in the current directory in bash?

Here is what I've tried, but it keeps adding an extra .png to the filename.

for file in *.png; do mv "$file" "${file}_3.6.14.png"; done 
like image 351
user1757703 Avatar asked Jul 06 '14 03:07

user1757703


People also ask

Which command is used to list all the files having extension txt?

locate command syntax: Similarly, you can follow the syntax of locate command for finding all files with any specific extension such as “. txt.”

How do I add lines to all files?

Just say echo "" >> file . This will append a new line at the end of file .


2 Answers

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

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

like image 94
Barmar Avatar answered Oct 03 '22 21:10

Barmar


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.
like image 21
Avinash Raj Avatar answered Oct 03 '22 21:10

Avinash Raj