Home > Mobile >  Renaming in batch Linux command and extension
Renaming in batch Linux command and extension

Time:10-08

I have many pictures in a folder /home/pictures in linux.

image1.jpg
image2.jpg
image4.png
image3.jpg
....

How I can add a suffix to the files in the command line with a .jpg extension to get this result:

ext_image1.jpg
ext_image2.jpg
image4.png
ext_image3.jpg

CodePudding user response:

Do you mean add a suffix to the file name or a prefix to the file name, if you want to add a prefix(according to your example):

you could implement it with find in bash or zsh:

cd /home/pictures
find * -name '*.jpg' -exec mv {,ext_}{} \;

With enter image description here

enter image description here

CodePudding user response:

Another way with ls with RegEx and than use the listing in a loop.

Example bash 5.0.3(1)-release (i686-pc-linux-gnu)

€ touch image1.jpg image2.jpg image3.jpg image4.jpg
€ ls *.jpg
image1.jpg  image2.jpg  image3.jpg  image4.jpg
€ ls *[1-3].jpg
image1.jpg  image2.jpg  image3.jpg
€ for file in $(ls *[1-3]*.jpg) ; do mv ${file} $(printf "ext_%s" ${file}); done
€ ls *.jpg
ext_image1.jpg  ext_image2.jpg  ext_image3.jpg  image4.jpg
  • Related