How to rename multiple files on Linux

Renaming files on Linux systems is usually handled by the mv (move) command. The syntax is just mv old.txt new.txt. Simple enough, but what if we have multiple files that need to be renamed at once, even hundreds of them? The default mv utility can’t handle renaming more than one file unless we do a bit of scripting. There are also other utilities we can install to solve the problem, like rename and mmv.

In this guide, we’ll show you how to use the mv command as well as the rename and mmv tools to rename multiple files on your Linux distro. We’ll go over several examples so you can understand the syntax and how to use each method.

In this tutorial you will learn:

  • How to rename multiple files at once with mv command
  • How to install rename on major Linux distros
  • How to install mmv on major Linux distros
  • How to use mmv, through command examples
  • How to use rename, through command examples

Renaming multiple files at once on Linux

Renaming multiple files at once on Linux

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software mv, rename, mmv
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Rename multiple files at once with mv command



Renaming multiple files with the mv command is easier if you know a little bit of Bash scripting. Take a look at some of the examples below to see some common uses with this method.

  1. The following command will add a .txt file extension to all files in your present working directory.
    $ for i in $( ls ); do mv $i $i.txt; done
    
  2. To remove a file extension from all files, you can use this command.
    $ for i in $( ls *.txt ); do mv $i ${i%.*}; done
    
  3. We can also change the file extension of every file. The following command will change all files with the .log extension to .txt.
    $ for i in *.log; do mv -- "$i" "${i%.log}.txt"; done
    
  4. You can also use the find command, along with -exec option or xargs command to rename multiple files at once. This command will append .bak to every file that begins with the pattern “file”.
    $ find . -type f -name 'file*' -print0 | xargs --null -I{} mv {} {}.bak
    
  5. This command uses find and the -exec option to append “_backup” to all files that end in the .txt extension.
    $ find . -name "*.txt" -exec mv {} {}_backup \;
    
  6. We can also use xargs to do the same thing. This command will accept standard input from the ls command, then use xargs to append “_backup” to all files that end in the .txt extension.
    $ ls *.txt | xargs -I{} mv {} {}_backup
    


  7. To change all files with the .txt in the present directory to have lowercase letters only, this command will do the job.
    $ for i in `ls *.txt`; do mv "$i" "`echo $i | tr '[A-Z]' '[a-z]'`"; done
    

The advantage of this method is that we don’t need any extra Linux software – we just use the native mv utility, and sometimes coupled with ls, find, or xargs. However, as we’ve seen in these examples, it can be rather complex to do something simple like rename a few files. Check out some of the other methods below to see some more convenient options that require installation.

Install rename utility

Your Linux distro may already have rename installed by default. Just in case it’s not already installed, you can use the appropriate command below to install it through your system’s package manager.

To install rename on Ubuntu, Debian, and Linux Mint:

$ sudo apt install rename

To install rename on CentOS, Fedora, AlmaLinux, and Red Hat:

$ sudo dnf install prename

To install rename on Arch Linux and Manjaro:

$ sudo pacman -S perl-rename

rename command examples

The rename command accepts perl expression to do the actual renaming. If you’re already familiar with this syntax, then rename will be very easy to use. If not, don’t worry, we have some examples below that will help you get the hang of it.



Here we’ve compiled some of the most common scenarios that you would need to rename multiple files at once. These commands assume that the files you wish to rename are in your present working directory. You can always specify the path to other directories if you wish.

There are a few options you can use with rename. The -n option will show you what changes rename is going to make, without actually making the changes. This helps you avoid any undesirable changes. The -v (verbose) option will print the names of files that have been successfully renamed.

  1. This command will rename uppercase files to lowercase.
    $ rename 'y/A-Z/a-z/' *
    

    Or, to convert lowercase to uppercase:

    $ rename 'y/a-z/A-Z/' *
    
  2. To change the extension of a bunch of files, use the following syntax. This particular example will convert .log files to .txt files.
    $ rename 's/\.log$/\.txt/' *.log
    
  3. To capitalize the first letter of each file name, use the following command.
    $ rename 's/\b(\w)/\U$1/g' *
    
  4. To remove all blank spaces from file names, execute this command.
    $ rename "s/ *//g" *
    

    Or, to replace spaces with underscores:

    $ rename 's/\s+/_/g' *
    
  5. To remove a file extension from a bunch of files, use the following syntax. This example will remove the .bak extension.
    $ rename 's/\.bak$//' *.bak
    

Install mmv utility

You can use the appropriate command below to install mmv through your system’s package manager.

To install mmv on Ubuntu, Debian, and Linux Mint:

$ sudo apt install mmv


To install mmv on CentOS, Fedora, AlmaLinux, and Red Hat:

$ sudo dnf install mmv

To install mmv on Arch Linux and Manjaro:

$ git clone https://aur.archlinux.org/mmv.git
$ cd mmv/
$ makepkg -si

mmv command examples

The most attractive feature of mmv is its ability to easily rename a lot of files by accepting wildcards in the command. This allows you to easily append or remove certain patterns from file names, or even arrange text within the names. Check out some of the examples below to learn the syntax for the mmv tool, and see some of the most common commands that you may find yourself running with it. Keep in mind that you can use the -n option with mmv to preview changes.

  1. Use this example to change the file extension on a bunch of files. This command will change all files with extension .htm to .html.
    $ mmv '*.htm' '#1.html'
    
  2. This command will replace the first occurrence of “foo” with “bar” in all file names.
    $ mmv '*foo*' '#1bar#2'
    
  3. The following command gives you an idea of how you can use mmv to rearrange parts of a file name. This example will take files like access.log-dbserver and rejected.log-webserver and rename them to access-dbserver.log and rejected-webserver.log, respectively. For this to work, we just need to have a consistent pattern, such as the dash and period in this case.


    $ mmv '*.log-*' '#1-#2.log'
    
  4. Change all file names to lowercase letters by using this command.
    $ mmv '*' '#l1'
    
  5. Conversely, change files names to all uppercase letters with this one.
    $ mmv '*' '#u1'
    

Closing Thoughts

In this guide, we learned several different methods to rename multiple files simultaneously on Linux. We also saw a multitude of command line examples that can cover a lot of different scenarios or be easily adapted to fit similar situations. Bulk renaming can be done through native bash utilities like mv, ls, xargs, and find, or we can install the mmv or rename utility to make our life a little easier. Every method has their strengths and weaknesses, so use whichever one you think will be most convenient for you.



Comments and Discussions
Linux Forum