Home » Linux » Linux Rename Multiple Files

Guide to Renaming Multiple Files in Linux

This guide will show you how to rename multiple files in Linux – renaming files sequentially, changing the extension, adding the date, and other renaming tasks.

We’ve already covered moving and renaming files and directories using the mv command. However, renaming multiple files at once is a bit more complex. While it can be done with the mv command, some tools are built specifically for the task.

Read on to find out what those tools are and how they can be used.

Each tool has differences in how the renaming operations are defined which makes them appropriate for different scenarios.

Before trying out any of these commands, make sure you’ve backed up your files, just in case something goes wrong! Renaming files is essentially moving them to a new location; a typo could result in them being moved somewhere you can’t find them.

How to Rename Multiple Files with mv

First up, the mv command. It moves and renames files and directories. It does this one at a time.

To use it to rename more than one file at a time, a for loop statement can be used to iterate over multiple files and execute the mv command on each to rename them.

Check out this article for more ways to use the Bash for loop.

The mv command is available on all Linux systems and is the most practical way of renaming files as it doesn’t rely on any additional software packages being present.

The best way to illustrate how this combination of the for loop and mv command is with some examples:

Add a File Extension/Append Text to All File Names in a Directory

The following command will add a file extension (or can be used to append any text to the file name) to all files in the current directory:

for file in $( ls ); do mv $file $file.bak; done

The ls command lists the contents of the current directory. The files are then iterated over using a for loop, and the .bak extension is added to each file name.

Change the File Extension of All Files in a Directory

Rather than adding text, text can be replaced:

for file in *.txt; do mv -- "$file" "${i%.txt}.bak"; done

Above, the text .txt is replaced with the text .bak

This could also be used to remove the file extension by simply removing the .bak portion of the command.

Renaming Only Certain Files

Similarly, the list of files returned by the ls command can be limited with wildcards, which can be used to limit the files which will be renamed:

for file in $( ls *.txt ); do mv $file $file.bak; done

Above, only files with names matching the pattern *.txt have the .bak extension appended to their filename.

Appending the Current Date

It’s common to want to add the current date to many files – if you’re replacing or moving or backing them up. The following line does just that:

for file in /path/to/directory/*; do mv $file $file-`date +%m%d%y`; done

Note that in this case, the directory the files are stored in is specified. In addition, the current date will be appended in the given format.

Sequential Numbering

If a while loop is used instead of a for loop, the iteration can be used to name files sequentially:

ls -v | cat -n | while read num file; do mv -n "$file" "$num.ext"; done

How to Rename Multiple Files with mmv

mmv (Mass Move and Rename) does what it says on the tin – it’s a tool for moving or renaming multiple files simultaneously.

It may not be installed on your system by default – it can be installed on Ubuntu/Debian based systems by running:

sudo apt install mmv

…or on RedHat/Fedora based systems by running:

sudo yum install mmv

You can view the full user manual for the mmv command by running:

man mmv

…or you can check out the examples below to see how it’s used.

Changing the File Extension of Multiple Files with mmv

The below example will replace the .txt file extension with the .bak file extension:

mmv \*.txt \#1.bak

Replacing Text / Changing Extensions in Filenames with mmv

This example will replace the .txt extension with .bak:

mmv '*.txt' '#1.bak'

There are some more examples of how mmv can re-arrange file names in the Ubuntu documentation.

How to Rename Multiple Files with rename

The rename command is another tool built specifically for renaming multiple files. It is not always installed by default and can be installed on Ubuntu/Debian by running:

sudo apt install rename

…or on RedHat/Fedora based systems by running:

sudo dnf install prename

There are multiple programs called rename – the one used in this article specifically is the Perl rename utility available in many package managers.

The Perl rename command uses Regular Expressions to define the renaming operations to be performed.

Change File Extension with rename

rename 's/\.bak$/\.txt/' *.bak

The above command line will change all .txt files to .bak files.

Uppercase to Lowercase

As rename uses regular expressions, it can quickly change file names based on matched patterns. The below command changes all uppercase characters to lowercase:

rename 'y/A-Z/a-z/' *

…and this one does the reverse:

rename 'y/a-z/A-Z/' *

Replaces Spaces with Underscores

To prepare files for hosting online, it’s usually advisable to remove spaces from the file names. This can be easily done with the rename utility:

rename 's/\s+/_/g' *

 

SHARE:
Photo of author
Author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment