When simple stuff fails you've gotta wing it

So you want to rename a bunch of digital camera JPEGs into something more meaningful or perhaps add some text to a bunch of file names. Get ready to use some heavy artillery here because we are diving straight into it.

Renaming a bunch of files

$ ls
a.JPG  b.JPG  c.txt  d.JPGa

# TESTING FIRST USING THE -n SWITCH!!! VERY IMPORTANT STEP!!!
$ rename -n 's/\.JPG$/.jpg/' *
a.JPG renamed as a.jpg
b.JPG renamed as b.jpg

# Actually renaming the files now
$ rename 's/\.JPG$/.jpg/' *
$ ls
a.jpg  b.jpg  c.txt  d.JPGa

Combine renaming with finding

Sometimes you need to pick out specific files out of a bunch or perhaps even out of a bunch of folders. For this purpose you can combine the find utility with rename and achieve quite a powerful result.

$ find .
.
./image2.JPG
./office.odt
./folder
./folder/image-in-a-folder.JPG
./image1.JPG

# TESTING FIRST USING THE -n SWITCH!!! VERY IMPORTANT STEP!!!
$ find . -iname '*.jpg' -exec rename -n 's/JPG$/jpg/' {} \;
./image2.JPG renamed as ./image2.jpg
./folder/image-in-a-folder.JPG renamed as ./folder/image-in-a-folder.jpg
./image1.JPG renamed as ./image1.jpg

# Actually renaming the files now
$ find . -iname '*.jpg' -exec rename 's/JPG$/jpg/' {} \;

# The result
$ find .
.
./image1.jpg
./image2.jpg
./office.odt
./folder
./folder/image-in-a-folder.jpg

Your thoughts

No comments

Add comment

* - required field

*




*