junk food for the brain …
How to Mass Rename Files in Linux
When entrusted with the chore of renaming multiple file, the convenience of a script shines. After all, we ain’t robots designed to do just one thing. Today, I’ll show one method of renaming files, using a for loop in bash.
First, the task we are going to do:
We have a list of files:
[raja@atreides test]$ ls data_file_1 data_file_2 data_file_4 data_file_6 data_file_8 data_file_10 data_file_3 data_file_5 data_file_7 data_file_9
We have been instructed to replace the underscores (_) in the file names with hyphens (-).
A menial task not really suited for humans. We’ll start building a script for this, one piece at a time.
- Check which type of shell you are using.
echo $SHELL
As I am using bash scripting, make sure the result is
/bin/bash - List out all the file that you wish to rename
$ ls data_file_*
It should give you output like this:-
[raja@atreides test]$ ls data_file_* data_file_1 data_file_2 data_file_4 data_file_6 data_file_8 data_file_10 data_file_3 data_file_5 data_file_7 data_file_9
- Lets display each one of this in a for loop.
for file_name in `ls data_file_*`; do echo "The file:" $file_name; done
What this for loop does is take each of the file names given by the
lscommand and store it in the$file_namebash variable. You’ll see this:-[raja@atreides test]$ for file_name in `ls data_file_*`; do echo "The file:" $file_name; done The file: data_file_1 The file: data_file_10 The file: data_file_2 The file: data_file_3 The file: data_file_4 The file: data_file_5 The file: data_file_6 The file: data_file_7 The file: data_file_8 The file: data_file_9
- Now that we’ve stored the file names in variables, we can use the
sedcommand to edit it, replacing the underscores with a hyphen. I’llechothe file name and pipe it to sed, to make sure it replaces the file name, and not modify the files itself.I used the sed replace command,
swith a global option behind, theg, to make sure it replaces all occurrences of the underscore. Without the ‘g’, sed will only replace the first underscore and ignore the rest.for file_name in `ls data_file_*`; do echo "The file:" $file_name; echo $file_name | sed 's/_/-/g' ; done
When you run this command in your console, you will see this output:
[raja@atreides test]$ for file_name in `ls data_file_*`; do echo "The file:" $file_name; echo $file_name | sed 's/_/-/g' ; done The file: data_file_1 data-file-1 The file: data_file_10 data-file-10 The file: data_file_2 data-file-2 The file: data_file_3 data-file-3 The file: data_file_4 data-file-4 The file: data_file_5 data-file-5 The file: data_file_6 data-file-6 The file: data_file_7 data-file-7 The file: data_file_8 data-file-8 The file: data_file_9 data-file-9
- Of course, all this script is doing right now is editing the file name in the $file_name variable itself. Even though you can see what the modified file name will look like, we have not actually renamed the files on your hard disk yet. (This can be a good method of testing your sed-fu, before you really do it
) - In order to actually rename the file, I’ll put the results of sed’s transformation in a variable called
$new_file_name.I’ll also print this out to the terminal, so you can see the changes that are about to take place.for file_name in `ls data_file_*`; do echo "The file:" $file_name; new_file_name=$(echo $file_name | sed 's/_/-/g'); echo "New name:" $new_file_name ; done
You’ll then see this being output on the terminal:
[raja@atreides test]$ for file_name in `ls data_file_*`; do echo "The file:" $file_name; new_file_name=$(echo $file_name | sed 's/_/-/g'); echo "New name:" $new_file_name ; done The file: data_file_1 New name: data-file-1 The file: data_file_10 New name: data-file-10 The file: data_file_2 New name: data-file-2 The file: data_file_3 New name: data-file-3 The file: data_file_4 New name: data-file-4 The file: data_file_5 New name: data-file-5 The file: data_file_6 New name: data-file-6 The file: data_file_7 New name: data-file-7 The file: data_file_8 New name: data-file-8 The file: data_file_9 New name: data-file-9
- Now that we’ve got both the old AND new file names, nicely stored in variables, its time to actually rename them using the
mvcommand. The syntax for renaming ismv current_file_name new_file_name. I think you’re starting to see where these variables come in handy.
for file_name in `ls data_file_*`; do echo "The file:" $file_name; new_file_name=$(echo $file_name | sed 's/_/-/g'); echo "New name:" $new_file_name ; mv -v $file_name $new_file_name; done
I’ve added a
-vto themv, so it displays what it is changing. As I always say, a little more verbosity is always good, especially on the terminal. Your final output will be something like this:-[raja@atreides test]$ for file_name in `ls data_file_*`; do echo "The file:" $file_name; new_file_name=$(echo $file_name | sed 's/_/-/g'); echo "New name:" $new_file_name ; mv -v $file_name $new_file_name; done The file: data_file_1 New name: data-file-1 `data_file_1' -> `data-file-1' The file: data_file_10 New name: data-file-10 `data_file_10' -> `data-file-10' The file: data_file_2 New name: data-file-2 `data_file_2' -> `data-file-2' The file: data_file_3 New name: data-file-3 `data_file_3' -> `data-file-3' The file: data_file_4 New name: data-file-4 `data_file_4' -> `data-file-4' The file: data_file_5 New name: data-file-5 `data_file_5' -> `data-file-5' The file: data_file_6 New name: data-file-6 `data_file_6' -> `data-file-6' The file: data_file_7 New name: data-file-7 `data_file_7' -> `data-file-7' The file: data_file_8 New name: data-file-8 `data_file_8' -> `data-file-8' The file: data_file_9 New name: data-file-9 `data_file_9' -> `data-file-9'
An
lswill reveal that all files have really been changed this time.[raja@atreides test]$ ls data-file-1 data-file-2 data-file-4 data-file-6 data-file-8 data-file-10 data-file-3 data-file-5 data-file-7 data-file-9
This should give you an idea on how to mass rename files. Of course there are probably better ways. Please do comment if you know some.
July 13, 2009 - 3:44 pm
Thanks for this detailed step-by-step guide. This was really helpful for mass renaming ordered image files on our site (see link for example). All the other linux mass rename guides I came across only showed how to rename extensions, but yours showed how to rename pretty much anything.
July 13, 2009 - 6:58 pm
Hey, no problem. Glad it helped someone.