Issue
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?
Solution
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 contain the string 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 replaces IMG
with VACATION
.
If your filenames include whitespace pay careful attention to the "$f"
notation. You need the double-quotes to preserve the whitespace.
Answered By - Susam Pal Answer Checked By - Terry (WPSolving Volunteer)