Rename Multiple Files at Once

Rename (or perl-rename on some Linux distributions) allows you to rename many file names with rules specified but the user. Basically, it is a small wrapper that uses the perlexpr from Perl.

Getting the package

The following code is for Debian based distributions.

apt-get install rename

The following code is for Arch Linux based distributions using pacman/packer.

pacman -S perl-rename

How to use

Please note that the following commands will use rename but you may need to use perl-rename depending your Linux distribution.

The rename command is devided in 2 sections. The first section is the syntax of what you are looking for and what you want to replace it with. The second section is simply a way to identify on which files you want to apply the renaming.

The first part is devided in 4 subsections. Subsection 1 is a required s and subsection 4 is a required g. If you already used the sed command you will be familiar with those delimiters. Subsection 2 is what you are looking to change and subsection 3 is what you want to replace it with.

Two useful flags are the -n and -v. The first one will tell you not to execute the command (so you can verify if your renaming works) and the second flag allows you to print the current state and the modified state of the file.

Renaming the first N characters

This is useful for when you go on a trip and you want to rename all you pictures. Let's pretend that we have 10 pictures all starting with DCIM*****.jpg, where ***** are 5 numbers that we what to keep. Let's try to rename those pictures to Beach*****.jpg.

rename -vn 's/DCIM/Beach/g' DCIM*.jpg

Alright, that seems to work. Now we are ready to apply the changes. Just remove the n flag

The subsection 3 is optional, so if you simply want to remove the DCIM but keep the ***** you can simply do it like this.

rename -v 's/DCIM//g' DCIM*.jpg

Removing the first character

Let's say you have a bunch of hidden files on your computer that you would like to be shown from now on. You simply need to remove the . character.

rename -v 's/^\.//g' *.*

What is this mess? The ^ character tells the search to start at the beginning of the filename (like in regex). For the . we need to escape it so it is not considered as the . character from a regex. The *.* at the end will search in all the files with an extension.

Another example, let's say we want to remove the _ character.

rename -v 's/^_//g' *.*

Yet another example where we remove the first character of all the files (this time we use the . regex character).

rename -v 's/^.//g' *.*

Removing the middle characters

Now you that you are getting familiar with the beginning of a file, let's look in the middle. I have to files on my system called sw.ras.001 and sw.ras.002 where I would like to remove the ras part (with one of the . character). That should be quite easy.

rename -v 's/ras\.//g' sw.ras.*

What to do when I have multiple parts to change

You are in a lab and you just did thousands experiences but you didn't named your files in a good format. In addition, your budget got cut in half and your supervisor Prof Smith won't be able to pay you for the extra time. Don't worry, I got you covered.

File names

Let's try to rename those so it appears that your supervisor did all the work.

rename -vn 's/^.{10}(.{8}).{4}(.{3}).{1}/20$1-Smith$2/g' *.results
[Cecilia]_16-02-29_-_[001].results -> 2016-02-29-Smith001.results
[Cecilia]_16-02-29_-_[002].results -> 2016-02-29-Smith002.results
[Cecilia]_16-02-29_-_[003].results -> 2016-02-29-smith003.results

See, I told you I had you covered. Now let's analyse this together by starting with the search criteria.

  • ^.{10} starts at the beginning and removes the 10 first characters ( [Cecilia]_ )
  • (.{8}) put the 8 characters in the first automatic variable ($1) ( 16-02-29 )
  • .{4} remove the next 4 characters ( _-_[ )
  • (.{3}) keeps the 3 numbers in the second automatic variable ($2) ( 001, 002, 003 )
  • .{1} remove the last character (you can just say . if you want)( ] )

The extension is not touch by our search, so it won't be modify. Let's take a look at renaming part.

  • 20 Since you were in a hurry with the date, let's fix it by adding the current century ( 20 )
  • $1 Let's use your first variable to complete the date( 16-02-29 )
  • -Smith Don't forget your advisor's name this time( -Smith )
  • $2 Nor the version of the file, if you do you will overwrite all your files over the same one ( 001, 002, 003 )

Check your new names, double check them, triple check them, and because Prof Smith will ask, quadruple check them. Now you are ready to remove the n flag.

Adding text

You are refactoring the name of all your TV-Series? This could be useful for you. Sometime you just need to specify the season number so your scrapper can recognise your video. Find a character that you can easily find in all your filenames and use it to add more characters.

File names
Arrow - 01.mkv
Arrow - 02.mkv
Arrow - 03.mkv

In this case, we will use the - character

rename -v 's/ - / - s02e/g' *.mkv
Arrow - s02e01.mkv
Arrow - s02e02.mkv
Arrow - s02e03.mkv

So here we keep the 2 spaces and the - characters and simply add at the end of it.


Radio Stations