Batch Renaming Files From the Command Line with Bash.

This is a simple example of renaming a number of files with one shell command.

Start by opening the terminal.

For this example there’s a number of images from a digital camera with names like DSC_283.jpg. The goal is to rename them all to something a little more descriptive. These were photos from the Grand Canyon so they can be named grand_canyon_1, 2, 3 and so on.

Test first by listing the selected files and echoing the command before actually executing it.

Select the correct files and iterate over them with a for loop

for i in *.jpg; do ls -s "$i"; done

Test echoing out the file names with a counter.

C=1; for i in *.jpg; do echo "mv $i $C"; let "C += 1"; done

Test with the mv command again by echoing out the command as a string.

C=1; for i in *.jpg; do echo "mv $i trees$C.jpg"; let "C += 1"; done

Remove the echo statement, adjust formatting as needed and run the command.

C=1; for i in *.jpg; do mv "$i" "grand_canyon_$C.jpg"; let "C += 1"; done

Your files should now be renamed.

Reference:
http://www.debian-administration.org/articles/150
Batch file renaming with regex

Leave a Reply

Your email address will not be published. Required fields are marked *