Search

bash change filename and use sequential numbers

bash change filename and use sequential numbers

Today I took a large number of photographs on my camera and needed to renumber them as well as rename them in linux.

Here is how I did it, and I hope you find it useful.

My filenames are of the format DSC_nnnn.JPG, where nnnn is a zero padded number in the range 0001 to 9999, for example DSC_1234.JPG
I wish to change them to park_images_mmmm.jpg, where mmmm is a zero padded number in the range 0001 to 9999 that does not necessarily tally with the nnnn from the above filename, for example DSC_1234.JPG could be renamed to park_images_0001.jpg.

Note that I use bash expansion to select a range of files that may not be sequential

Open up a terminal and chdir to the directory the images are in
cd images
Type the following onto one line, making your own changes as needed
a=1
for i in DSC_039?.JPG DSC_0[456]*JPG
do
mv -v $i park_images_`printf “%04d”.jpg $a`
let a=a+1
done

Related posts

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.