BASH file rename

Rename All Files in a Folder with BASH

There are those weird occasions when you need to import a mass of images into an application, but they all have different names, and the application wants a file name pattern. Well, you can do this with a simple BASH file rename.

It would be so much simpler if your files looked like this:
Image1.jpg
Image2.jpg
etc.

In BASH, and probably most shells, you can do something like this…

ext="jpg";count=0;for f in `ls`;do let count=count+1 && mv $f $f$count.$ext;done

Change the value in ext=”” to whatever extension you want. You can also format the destination name, so maybe you want: Image_1.jpg in which case you would have: $f_$count.$ext

If this were in a script (say, bash-file-rename.sh), as opposed to on the command line, it would look more like this:

ext="jpg"
count=0
for f in `ls`
do 
    let count=count+1 
    mv $f $f$count.$ext
done

As you can see, adding a semi-colon to the end of each line allows you to pull them all up into a single line, much simpler for the command line.

Leave a Reply

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

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