Linux laptop showing a bash prompt
Fatmawati Achmad Zaenuri/

Get to grips with the file renaming powerhouse of the Linux world and give mv—and yourself—a rest. Rename is flexible, fast, and sometimes even easier.聽 Here’s a tutorial to this powerhouse of a command.

What’s Wrong With mv?

There’s nothing wrong with mv . The command does a fine a job, and it is found on all Linux distributions, in macOS, and in other Unix-like operating systems. So it’s always available.聽But sometimes you just need a bulldozer, not a shovel.

The mv command has a purpose in life, and that is to move files. It is a happy side effect that it can be used to move an existing file into a new file, with a new name. The net effect is to rename the file, so we get what we want. But mv is not a dedicated file renaming tool.

Renaming a Single File With mv

To use mv to rename a file type mv, a space, the name of the file, a space, and the new name you wish the file to have. Then press Enter.

You can use聽ls to check the file has been renamed.

mv oldfile.txt newfile.txt
ls *.txt

Renaming Multiple Files with mv

Things get trickier when you want to rename multiple files. mv has no capability to deal with renaming multiple files. You must resort to using some nifty Bash tricks.聽That’s fine if you know some medium-grade command-line fu, but the complexity of renaming multiple files with mv stands in stark contrast to the ease of using mv to rename a single file.

Things escalate quickly.


Let’s say we’ve got a directory with a variety of files in it, of differing types. Some of these files have a “.prog” extension. We want to rename them at the command line so that they have a “.prg” extension.

How do we wrangle mv into doing that for us? Let’s take a look at the files.

ls *.prog -l

Here’s one way to do it that doesn’t resort to writing an actual Bash script file.

for f in *.prog; do mv -- "$f" "${f%.prog}.prg"

DId that work? Let’s check the files and see.

ls *.pr*

So, yes, it worked. They’re all “.prg” files now, and there are no “.prog” files in the directory.

What Just Happened?

What did that long command actually do? Let’s break it down.

for f in *.prog; do mv -- "$f" "${f%.prog}.prg"

The first part starts a loop that is going to process each “.prog” file in the directory, in turn.

The next part says what the processing will do.聽It is using聽mv to move each file to a new file. The new file is going to be named with the original file’s name excluding the聽 “.prog” part. A new extension of “.prg”聽will be used instead.

There Has to be a Simpler Way

Most definitely. It is the rename command.

rename is not part of a standard Linux distribution, so you will need to install it. It also has a different name in different families of Linux, but they all work the same way.聽You’ll just have to substitute the appropriate command name according to the Linux flavor you’re using.

in Ubuntu and Debian-derived distributions you install rename like this:

sudo apt-get install rename

In Fedora and RedHat-derived distributions you install prename like this. Note the initial “p,” which stands for Perl.

sudo dnf install prename

To install it in Manjaro Linux use the following command. Note that the renaming command is called perl-rename.

sudo pacman -Syu perl-rename

Let’s Do That Again

And this time we’ll use rename.聽We’ll roll back the clock so that we have a set of “.prog” files.

ls *.prog


Now let’s use the following command to rename them. We’ll then check with ls whether it worked. Remember to substitute rename with the appropriate command name for your Linux if you’re not using Ubuntu or a Debian-derived Linux.

rename 's/.prog/.prg/' *.prog
ls *.pr*

That worked, they’re now all “.prg” files, and there are no “.prog” files left in the directory.

What Happened This TIme?

Let’s explain that bit of magic, in three parts.

The first part is the command name, rename (or prename or perl-rename , for the other distributions).

The last part is *.prog, which tells rename to operate on all “.prog” files.

The middle part defines the work we want to be done on each filename. The s means substitute. The first term (.prog) is what rename will search for in each filename and the second term (.prg)聽 is what it will be substituted with.


The middle part of the command, or central expression, is a Perl ‘regular expression‘ and it is what gives the rename command its flexibility.

Changing Other Parts of a Filename

We’ve changed filename extensions so far, let’s amend other parts of the filenames.

In the directory are a lot of C source code files. All of the filenames are prefixed with “slang_”. We can check this with ls.

ls sl*.c

We are going to replace all occurrences of “slang_” with “sl_”. The format of the command is already familiar to us. We’re just changing the search term, the replacement term, and the file type.

rename 's/slang_/sl_' *.c

This time we are looking for “.c” files, and searching for “slang_”. Whenever “slang_” is found in a filename it is substituted with “sl_”.

We can check the result of that command by repeating the ls command from above with the same parameters:

ls sl*.c

Deleting Part of a Filename

We can remove a part of a filename by substituting the search term with nothing.

ls *.c
rename 's/sl_//' *.c
ls *.c


We can see from the ls command that our “.c” files are all prepended with “sl_”. Let’s get rid of that altogether.

The rename command follows the same format as before. We’re going to be looking for “.c” files. The search term is “sl_”, but there is no substitution term. Two backslashes without anything between them means nothing, an empty string.

rename will process each “.c” file in turn. It will search for “sl_” in the filename. If it is found, it will be replaced by nothing. In other words, the search term is deleted.

The second use of the ls command confirms that the “sl_” prefix has been removed from every “.c” file.

Limit Changes to Specific Parts of Filenames

Let’s use ls聽to look at files that have the string “param” in their filename. Then we’ll use rename to replace that string with the string “parameter”. We’ll use ls once more to see the effect the rename command has had on those files.

ls *param*
rename 's/param/parameter' *.c
ls *param*


Four files are found that have “param” in their filename. param.c, param_one.c, and param_two.c all have “param” at the start of their name. third_param.c has “param” at the end of its name, just before the extension.

The rename command is going to search for “param” everywhere in the filename, and replace it with “parameter” in all cases.

The second use of the聽ls command shows us that that is exactly what has happened. Whether “param” was at the start or at the end of the filename, it has been replaced by “parameter.”

We can use Perl’s metacharacters to refine the behavior of the middle expression. Metacharacters are symbols that represent positions or sequences of characters. For example, ^ means “start of a string,” $ means “end of a string,” and . means any single character (apart from a newline character).

We’re going to use the start of string metacharacter ( ^ ) to restrict our search to the start of the filenames.

ls *param*.c
rename 's/^parameter/value/' *.c
ls *param*.c
ls value*.c

The files we renamed earlier are listed, and we can see the string “parameter” is at the start of three filenames and it is at the end of one of the filenames.


Our rename command uses the start of line聽(^)聽 metacharacter before the search term “parameter.” This tells rename to only consider the search term to have been found if it is at the start of the filename. The search string “parameter” will be ignored if it is anywhere else in the filename.

Checking with ls, we can see that the filename that had “parameter” at the end of the filename has not been modified, but the聽three filenames that had “parameter” at the start of their names have had the search string replaced by the substitute term “value.”

The power of rename lies in the power of Perl.聽All of the power of Perl is at your disposal.

Searching With Groupings

rename has yet more tricks up its sleeve. Let’s consider the case where you might have files with similar strings in their names. They’re not exactly the same strings, so a simple search and substitution won’t work here.

In this example we use ls聽to check which files we have that start with “str”. There are two of them, string.c and strangle.c. We can rename both strings at once using a technique called grouping.

The central expression of this rename command will search for strings within filenames that have the character sequence “stri” or “stra” where those sequences are immediately followed by “ng”. In other words, our search term is going to look for “string” and “strang”. The substitution term is “bang”.

ls str*.c
rename 's/(stri|stra)ng/bang/' *.c
ls ban*.c


Using聽ls聽a second time confirms that string.c has become bang.c and strangle.c is now bangle.c.

Using Translations With rename

The rename command can perform actions on filenames called translations.聽A simple example of a translation would be to force a set of filenames into uppercase.

In the rename command below notice that we’re not using an s/聽to start the central expression, we’re using y/.聽This tells聽rename we’re not performing a substitution; we’re performing a translation.

The a-z聽term is a Perl expression that means all lowercase characters in the sequence from a to z.聽Similarly, the A-Z聽term represents all uppercase letters in the sequence from A to Z.

The central expression in this command could be paraphrased as “if any of the lowercase letters from a to z are found in the filename, replace them with the corresponding characters from the sequence of uppercase characters from A to Z.”

To force the filenames of all “.prg” files to uppercase, use this command:

rename ‘y/a-z/A-Z/’ *.prg

ls *.PRG


The ls command shows us that all of the “.prg” filenames are now in uppercase. In fact, to be strictly accurate, they’re not “.prg” files anymore. They’re “.PRG” files. Linux is case sensitive.

We can reverse that last command by reversing the position of the a-z and A-Z聽terms in the central expression.

rename ‘y/A-Z/a-z/’ *.PRG

ls *.prg

You (Wo|Do)n’t Learn Perl in Five Minutes

Getting to grips with Perl is time well spent. But to start using the time-saving capabilities of the rename command, you don’t need to have much Perl knowledge at all to reap large benefits in power, simplicity and time.

Linux Commands
Files tar pv cat tac chmod grepdiff sed ar man pushd popd fsck testdisk seq fd pandoc cd $PATH awk join jq fold uniq journalctl tail stat ls fstab echo less chgrp chown rev look strings type rename zip unzip mount umount install fdisk mkfs rm rmdir rsync df gpg vi nano mkdir du ln patch convert rclone shred srm
Processes alias screen top nice renice progress strace systemd tmux chsh history at batch free which dmesg chfn usermod ps chroot xargs tty pinky lsof vmstat timeout wall yes kill sleep sudo su time groupadd usermod groups lshw shutdown reboot halt poweroff passwd lscpu crontab date bg fg
Networking netstat ping traceroute ip ss whois fail2ban bmon dig finger nmap ftp curl wget who whoami w iptables ssh-keygenufw

RELATED:Best Linux Laptops for Developers and Enthusiasts

Profile Photo for Dave McKay Dave McKay
Dave McKay first used computers when punched paper tape was in vogue, and he has been programming ever since. After over 30 years in the IT industry, he is now a full-time technology journalist. During his career, he has worked as a freelance programmer, manager of an international software development team, an IT services project manager, and, most recently, as a Data Protection Officer. His writing has been published by聽,,, and Dave is a Linux evangelist and open source advocate.
Read Full Bio 禄

The above article may contain affiliate links, which help support How-To Geek.