Easily Renaming Multiple Files
Posted on September 18th, 2009 in Web Development | 1 Comment »
Update (2009-10-23): This works well for adding a prefix to files in a folder. To add a suffix to a file read “Renaming files – Adding a suffix to the file name” instead.
Here is a quick way to rename multiple files using bash.
Objective: Rename all image files in a directory in uppercase, to lowercase and append a tn_ prefix.
Step 1: Prepare a working directory
mkdir test cd test touch FILE_1.jpg touch FILE_2.jpg touch FILE_3.jpg
Step 2: Test first – print out all files in lower case
for i in *.jpg; do echo $i | tr [:upper:] [:lower:]; done
Output
file_1.jpg file_2.jpg file_3.jpg
Step 3: Test first – print out all files prefixed with tn_ and lower cased
for i in *.jpg; do echo "tn_"$i | tr [:upper:] [:lower:]; done
Output
tn_file_1.jpg tn_file_2.jpg tn_file_3.jpg
Step 4: Rename the files in the directory
for i in *.jpg; do mv $i `echo "tn_"$i | tr [:upper:] [:lower:]`; done
Done!
One Response
[...] solution provided in Easily Renaming Multiple Files works perfectly well if we want to add a prefix to a file. This solution does not work too well if [...]