station station - 8 months ago 29
Bash Question

Rename multiple files by replacing a particular pattern in the filenames using a shell script

Write a simple script that will automatically rename a number of files. As an example we want the file *001.jpg renamed to user defined string + 001.jpg (ex: MyVacation20110725_001.jpg) The usage for this script is to get the digital camera photos to have file names that make some sense.

I need to write a shell script for this. Can someone suggest how to begin?


An example to help you get off the ground.

for f in *.jpg; do mv "$f" "`echo $f | sed s/IMG/VACATION/`"; done

In this example, I am assuming that all your image files begin with "IMG" and you want to replace "IMG" with "VACATION".

The shell automatically evaluates *.jpg to all the matching files.

The second argument of mv (the new name of the file) is the output of the sed command that is replacing "IMG" with "VACATION".