How to Bulk Rename Files in Linux (Terminal or GUI)

There are several ways to bulk rename files in Linux. If you want to bulk rename files with a GUI tool, try Thunar file manager, or Krename.

Tip: To install Thunar on Ubuntu/Debian, type sudo apt-get install thunar in a terminal. To install Krename, type sudo apt-get install krename.

How to bulk rename files from the terminal

If you have a directory of files that you would like to bulk rename, you can use the rename command from the terminal.

UPDATE: I believe the Perl-based rename command is only available on Debian-based Linux distros, but there are instructions on adding it to other distros below.

The syntax for the rename command is:

rename [ -v ] [ -n ] [ -f ] perlexpr [ files ]

-v means "verbose" and it will output the names of the files when it renames them. It is a good idea to use this feature so you can keep track of what is being renamed. It is also a good idea to do a test run with -n which will do a test run where it won't rename any files, but will show you a list of files that would be renamed.

The "perlexpr" part of the command is a Perl expression. Don't panic yet...

The "rename" command in action

Here is an example of the rename command:

rename -n ’s/\.htm$/\.html/’ *.htm

The -n means that it's a test run and will not actually change any files. It will show you a list of files that would be renamed if you removed the -n. In the case above, it will convert all files in the current directory from a file extension of .htm to .html.

If the output of the above test run looked ok then you could run the final version:

rename -v ’s/\.htm$/\.html/’ *.htm

The -v is optional, but it's a good idea to include it because it is the only record you will have of changes that were made by the rename command as shown in the sample output below:

$ rename -v 's/\.htm$/\.html/' *.htm
3.htm renamed as 3.html
4.htm renamed as 4.html
5.htm renamed as 5.html

The tricky part in the middle is a Perl substitution with regular expressions, highlighted below:

rename -v ’s/\.htm$/\.html/’ *.htm

Tip: There is an intro to Perl regular expressions here.

Basically the "s" means substitute. The syntax is s/old/new/ — substitute the old with the new.

A . (period) has a special meaning in a regular expression — it means "match any character". We don't want to match any character in the example above. It should match only a period. The backslash is a way to "escape" the regular expression meaning of "any character" and just read it as a normal period.

The $ means the end of the string. \.htm$ means that it will match .htm but not .html.

It's fairly basic — substitute .htm with .html:


The last part of the command, highlighted below, means to apply the rename command to every file that ends with .htm (the * is a wildcard).

rename -v ’s/\.htm$/\.html/’ *.htm

Other Examples

Maybe you have a digital camera that takes photos with filenames something like 00001234.JPG, 00001235.JPG, 00001236.JPG. You could make the .JPG extension lowercase with the following command executed from the same directory as the images:

rename -v 's/\.JPG$/\.jpg/' *.JPG

Here is the output of the above command:

$ rename -v 's/\.JPG$/\.jpg/' *.JPG
00001111.JPG renamed as 00001111.jpg
00001112.JPG renamed as 00001112.jpg
00001113.JPG renamed as 00001113.jpg

That is simple enough, as it is similar to the .html example earlier. You could also bulk rename them with something descriptive at the beginning like this:

Tip: Before trying more complicated renaming like in the example below, do a test run with the -n option as described at the beginning of this tutorial.

rename -v 's/(\d{8})\.JPG$/BeachPics_$1\.jpg/' *.JPG

That will change filenames that have the pattern ########.JPG (8 numbers and capital .JPG) to something like BeachPics_########.jpg (the same 8 numbers and changing the extension to lowercase .jpg). Here is a test run with the -n option:

$ rename -n 's/(\d{8})\.JPG$/BeachPics_$1\.jpg/' *.JPG
00001111.JPG renamed as BeachPics_00001111.jpg
00001112.JPG renamed as BeachPics_00001112.jpg
00001113.JPG renamed as BeachPics_00001113.jpg

Here's a quick breakdown of the Perl substitution with the regular expression above.

The highlighted section below means to count 8 digits. The parentheses mean to save those 8 digits for later because they are going to be used again in the second half of the substitution:


In the highlighted section below, it adds the string BeachPics, and underscore, and then the text in parentheses from the first half of the substitution. $1 will insert the string from the first set of parentheses that it finds — in this case the 8 digits. If you have more than one set of parentheses you can access the second set with the Perl variable $2 and so on.


Final Refinement

The following variation would make even cleaner-looking filenames. See if you can figure out how it works:

$ rename -n 's/\d{5}(\d{3})\.JPG$/BeachPics_$1\.jpg/' *.JPG
00000123.JPG renamed as BeachPics_123.jpg
00000124.JPG renamed as BeachPics_124.jpg
00000125.JPG renamed as BeachPics_125.jpg

Learning Perl Regular Expressions

You can learn more about Perl regex here and in the Perl regular expression documentation, and the quickstart. Also check out this regex cheatsheet. You can learn more about the rename command by typing man rename in the terminal.

Adding the Rename Command to Non-Debian Distros

The following information was provided by Dan Fego on how to add the Perl-based rename command to non-Debian Linux distros:

I did the following [on Gentoo], though I'm aware there's both more concise and generic ways to do it. This was just my path of discovery.

[the following commands are from Ubuntu]

# which rename
# ls -l /usr/bin/rename
lrwxrwxrwx 1 root root 24 Dec 2 02:13 /usr/bin/rename ->
# ls -l /etc/alternatives/rename
lrwxrwxrwx 1 root root 16 Dec 6 00:29 /etc/alternatives/rename ->
# ls -l /usr/bin/prename
-rwxr-xr-x 1 root root 2987 Dec 4 04:18 /usr/bin/prename

Once I had the final link in the chain [on Ubuntu], I knew which file to copy [to Gentoo], though really I didn't need to follow it all that way. The reason I did to begin with was because I tried to copy it with the filesystem mounted, and it referred to absolute paths which didn't exist outside of the Ubuntu environment. The prename script is fully self-contained, assuming you've got Perl on your system (which just about any self-respecting Linux distro does).

The code from Ubuntu's rename/prename is here (Gutsy Gibbon).

Average: 4.8 (73 votes)



Thanks for these tips, I have succesfully renamed a directory of files coming from my JVC Everio with *.mod extension that Nautilus was associating with Amiga tracker format, while they are MPEGs with AC3 audio.

Thank you for your tips.

Thank you for your tips. It's very useful for me.

rename utilities?

I read this reference a long time ago, and remember using this rename utility you speak of. I'm running a Gentoo box, which seems to come with a crappy rename utility that doesn't match the one you mention here. Is it specifically an Ubuntu thing? Could you possibly point me in the direction of its source? I'm sure I don't have to tell you how difficult it could be to construct a search query to find something like this. :-)

Webmaster Tips's picture

Perl rename command, Debian Linux

I think the command is only on Debian distros. There is some info about it in this thread about renaming files. I'm not sure how to move it to Gentoo.

I've attached some code from the Ubuntu Gutsy Gibbon version here, though I'm not sure if that will help.

hey, here is something very

hey, here is something very useful I just used to rename files:

find abc -name qwer_\* | gawk '{a=$1;sub(/\qwer_/,"/",a);printf "mv %-100s %s\n",$1,a}' > xxx

Ok, it is not my code, but from one of my colleagues at work. I used it to eliminate the prefix for a bunch of files recursively in a directory. You should replace abc for the directory where the search is done, qwer_\* for the kind of files you wish to look for (in this case, all files that start with qwer_). If you want to do anything else other than to eliminate the prefix, it should be done on the second qwer_ (if you want to add words, count, etc...).

this command will create a file named xxx, so you can see what it will do before run it.

to run, just type . xxx

ps.: this command gives 100 characters space for the file names, so before run you should verify that there is at least one space between the files names in xxx. If too short, just increase the 100 in the command line....

Webmaster Tips's picture

rename files

Thanks for posting that. I'll try it out...

Syndicate content