Rename multiple files by replacing a particular pattern in the filenames using a shell script
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.
You can use rename utility to rename multiple files by a pattern. For example following command will prepend string MyVacation2011_ to all the files with jpg extension.
rename 's/^/MyVacation2011_/g' *.jpg
or
rename <pattern> <replacement> <file-list>
for file in *.jpg ; do mv $file ${file//IMG/myVacation} ; done
Again assuming that all your image files have the string "IMG" and you want to replace "IMG" with "myVacation".
With bash you can directly convert the string with parameter expansion.
Example: if the file is IMG_327.jpg, the mv
command will be executed as if you do mv IMG_327.jpg myVacation_327.jpg
. And this will be done for each file found in the directory matching *.jpg
.
IMG_001.jpg -> myVacation_001.jpg
IMG_002.jpg -> myVacation_002.jpg
IMG_1023.jpg -> myVacation_1023.jpg
etcetera...