How to rename multiple files by replacing word in file name?
Solution 1:
rename 's/ACDC/AC-DC/' *.xxx
from man rename
DESCRIPTION
"rename" renames the filenames supplied according to the rule specified as the
first argument. The perlexpr argument is a Perl expression which is expected to modify the
$_ string in Perl for at least some of the filenames specified. If a given filename is not
modified by the expression, it will not be renamed. If no filenames are given on
the command line, filenames will be read via standard input.
For example, to rename all files matching "*.bak" to strip the extension, you might say
rename 's/\.bak$//' *.bak
To translate uppercase names to lower, you'd use
rename 'y/A-Z/a-z/' *
Solution 2:
This answer contains the good parts from all other answers, while leaving out such heresy as ls | while read
.
Current directory:
for file in ACDC*.xxx; do
mv "$file" "${file//ACDC/AC-DC}"
done
Including subdirectories:
find . -type f -name "ACDC*" -print0 | while read -r -d '' file; do
mv "$file" "${file//ACDC/AC-DC}"
done
Newline characters are really unlikely to be in filenames, so this can be simpler while still working with names containing spaces:
find . -type f -name "ACDC*" | while read -r file; do
mv "$file" "${file//ACDC/AC-DC}"
done
Solution 3:
To use the util-linux version of rename
that Phil referred to (on Ubuntu, it's called rename.ul
):
rename ACDC AC-DC ACDC*
or
rename.ul ACDC AC-DC ACDC*
Solution 4:
Using the bash shell
find . -type f -name "ACDC*" -print0 | while read -d $'\0' f
do
new=`echo "$f" | sed -e "s/ACDC/AC-DC/"`
mv "$f" "$new"
done
Note: using find
will process the current directory, and the directories under.