Simple way to rename multiple files in unix/linux

The situation is you have a number of files with sequence filenames, and you would like to rename it, for example you would like to rename from photos01a.jpg to photos01.jpg, yes, it just a simple removing the ‘a‘ from it, but it would be an annoying jobs if the number of files is quite big.

This is the ‘before’ file listing:

# ls -al
total 106
drwxr-xr-x 2 www www 1024 Mar 28 10:56 .
drwxr-xr-x 4 www www 512 Mar 28 09:05 ..
-rw-r--r-- 1 www www 2681 Mar 28 10:56 photos01a.jpg
-rw-r--r-- 1 www www 3033 Mar 28 10:56 photos02a.jpg
-rw-r--r-- 1 www www 2963 Mar 28 10:56 photos03a.jpg
-rw-r--r-- 1 www www 2813 Mar 28 10:56 photos04a.jpg
-rw-r--r-- 1 www www 2879 Mar 28 10:56 photos05a.jpg
-rw-r--r-- 1 www www 2840 Mar 28 10:56 photos06a.jpg
-rw-r--r-- 1 www www 3061 Mar 28 10:56 photos07a.jpg
-rw-r--r-- 1 www www 3200 Mar 28 10:56 photos08a.jpg
-rw-r--r-- 1 www www 2918 Mar 28 10:56 photos09a.jpg
-rw-r--r-- 1 www www 2729 Mar 28 10:56 photos10a.jpg
-rw-r--r-- 1 www www 2753 Mar 28 10:56 photos11a.jpg
-rw-r--r-- 1 www www 2940 Mar 28 10:56 photos12a.jpg
-rw-r--r-- 1 www www 2798 Mar 28 10:56 photos13a.jpg
-rw-r--r-- 1 www www 2595 Mar 28 10:56 photos14a.jpg
-rw-r--r-- 1 www www 2640 Mar 28 10:56 photos15a.jpg
-rw-r--r-- 1 www www 2475 Mar 28 10:56 photos16a.jpg
-rw-r--r-- 1 www www 2907 Mar 28 10:56 photos17a.jpg
-rw-r--r-- 1 www www 2890 Mar 28 10:56 photos18a.jpg
-rw-r--r-- 1 www www 2904 Mar 28 10:56 photos19a.jpg
-rw-r--r-- 1 www www 5507 Mar 28 10:56 photos20a.jpg
-rw-r--r-- 1 www www 5370 Mar 28 10:56 photos21a.jpg
-rw-r--r-- 1 www www 5343 Mar 28 10:56 photos22a.jpg
-rw-r--r-- 1 www www 5576 Mar 28 10:56 photos23a.jpg

and we know that ls -1 will gives just the filename output:

# ls -1
photos01a.jpg
photos02a.jpg
photos03a.jpg
photos04a.jpg
photos05a.jpg
photos06a.jpg
photos07a.jpg
photos08a.jpg
photos09a.jpg
photos10a.jpg
photos11a.jpg
photos12a.jpg
photos13a.jpg
photos14a.jpg
photos15a.jpg
photos16a.jpg
photos17a.jpg
photos18a.jpg
photos19a.jpg
photos20a.jpg
photos21a.jpg
photos22a.jpg
photos23a.jpg

This is what I do:

First I will make sure my shell is Bourne, if not, just run sh to get the Bourne shell.

# env | grep SHELL
SHELL=/bin/csh
# sh

Then, use the output of ls -1 with the for loop in Bourne shell to execute the mv (move/rename) command. Here I use the sed command to do the online text replacement to the filename entry.

# for i in `ls -1`
> do
> mv $i `echo $i | sed 's/a.jpg/.jpg/g'`
> done
#

That’s it, doesn’t matter how many files you have, the looping will take care of it in just a few seconds.

*Note: pay close attention to the type of single-quote used in the “for” loop above. There are two type of single-quote which carry totally different meaning.

 

First single-quote is ” ” which is the normal single-quote, this just mean that we want to enclose whatever strings inside.

 

The second single-quote is ” ` ” which is a bit slanted, normally sited on the same row with numbers key on the keyboard, this ” ` ” single-quote means in unix/linux scripting is – execute whatever inside the quote.

 

so in the sample above, it will execute ” echo $i | sed ‘s/a.jpg/.jpg/g “

This is the ‘after‘ file listing:

# ls -la
total 104
drwxr-xr-x 2 www www 1024 Mar 28 11:07 .
drwxr-xr-x 4 www www 512 Mar 28 09:05 ..
-rw-r--r-- 1 www www 2681 Mar 28 10:56 photos01.jpg
-rw-r--r-- 1 www www 3033 Mar 28 10:56 photos02.jpg
-rw-r--r-- 1 www www 2963 Mar 28 10:56 photos03.jpg
-rw-r--r-- 1 www www 2813 Mar 28 10:56 photos04.jpg
-rw-r--r-- 1 www www 2879 Mar 28 10:56 photos05.jpg
-rw-r--r-- 1 www www 2840 Mar 28 10:56 photos06.jpg
-rw-r--r-- 1 www www 3061 Mar 28 10:56 photos07.jpg
-rw-r--r-- 1 www www 3200 Mar 28 10:56 photos08.jpg
-rw-r--r-- 1 www www 2918 Mar 28 10:56 photos09.jpg
-rw-r--r-- 1 www www 2729 Mar 28 10:56 photos10.jpg
-rw-r--r-- 1 www www 2753 Mar 28 10:56 photos11.jpg
-rw-r--r-- 1 www www 2940 Mar 28 10:56 photos12.jpg
-rw-r--r-- 1 www www 2798 Mar 28 10:56 photos13.jpg
-rw-r--r-- 1 www www 2595 Mar 28 10:56 photos14.jpg
-rw-r--r-- 1 www www 2640 Mar 28 10:56 photos15.jpg
-rw-r--r-- 1 www www 2475 Mar 28 10:56 photos16.jpg
-rw-r--r-- 1 www www 2907 Mar 28 10:56 photos17.jpg
-rw-r--r-- 1 www www 2890 Mar 28 10:56 photos18.jpg
-rw-r--r-- 1 www www 2904 Mar 28 10:56 photos19.jpg
-rw-r--r-- 1 www www 5507 Mar 28 10:56 photos20.jpg
-rw-r--r-- 1 www www 5370 Mar 28 10:56 photos21.jpg
-rw-r--r-- 1 www www 5343 Mar 28 10:56 photos22.jpg
-rw-r--r-- 1 www www 5576 Mar 28 10:56 photos23.jpg

 

Leave a Reply

Your email address will not be published. Required fields are marked *