How to Rename Files and Directories in Linux: Using the Command Line 2024

How to Rename Files and Directories in Linux

If you’re a Linux user, you’ll frequently need to rename files. Fortunately, the process is simple, whether you prefer using a graphical file manager or the command-line interface. However, renaming several files at once can be difficult, particularly for newcomers to the Linux world.

In this tutorial, we’ll teach you how to rename files and directories using the mv and rename commands. Our step-by-step guide will show you how to complete this task with ease, regardless of your experience level. So, whether you’re a seasoned Linux user or just getting started, read on to learn more about renaming files in Linux!

Renaming Files with the mv Command

The mv command, which stands for move, is employed for the purpose of renaming or relocating files from one location to another. The syntax of the mv command is depicted below:

ShellScript
mv [OPTIONS] source destination

One or multiple files or directories can serve as the source, while the destination can be either a singular file or a directory.

  • When multiple files are selected as the source, the destination must be a directory. Consequently, the selected source files will be relocated to the targeted directory.
  • When you provide a singular file as the source and the destination is a pre-existing directory, the file will be transferred to the designated directory.
  • To change the name of a file, you must indicate one file as the source and another file as the destination target.

An instance of how to rename a file named file1.txt to file2.txt would involve executing the following command:

ShellScript
mv file1.txt file2.txt

Renaming multiple files with the mv Command

The mv command has the ability to rename individual files only. However, it can be combined with other commands, such as find or incorporated within bash for or while loops to rename multiple files simultaneously.

To illustrate, consider the following example which demonstrates how to leverage the Bash for loop to rename all .html files in the present directory by modifying the file extension from .html to .php.

ShellScript
for f in *.html; do
    mv -- "$f" "${f%.html}.php"
done

Let’s go over the code step-by-step:

  1. In the first line, a for loop is created to go through all files that have the .html extension.
  2. For each item in the list, the second line renames the file by replacing the .html extension with .php. The expression ${file%.html} uses shell parameter expansion to remove the .html part of the file name.
  3. The keyword done marks the end of the loop segment.

Here’s an alternative example that uses the find command with mv to accomplish the same task:

ShellScript
find . -depth -name "*.html" -exec sh -c 'f="{}"; mv -- "$f" "${f%.html}.php"' \;

The find command is utilizing the -exec option to pass each file ending with .html in the current directory to mv individually. The placeholder {} represents the current file name being processed.

As demonstrated in the aforementioned examples, renaming multiple files with the mv command is a complex task that necessitates proficiency in Bash scripting.

Renaming Files with the rename Command

The rename command allows you to rename multiple files at once, and it requires some familiarity with regular expressions. Compared to the simpler mv command, it offers more advanced functionality.

There are two different versions of the rename command, each with its own syntax. In this tutorial, we’ll be working with the Perl version of the command. If you don’t already have it installed on your system, you can easily install it using your distribution package manager.

  • Install rename on Ubuntu and Debian
ShellScript
sudo apt install rename
  • Install rename on CentOS and Fedora
ShellScript
sudo yum install prename
  • Install rename on Arch Linux
ShellScript
yay perl-rename ## or yaourt -S perl-rename

The format for the rename command can be expressed in the following way:

ShellScript
rename [OPTIONS] perlexpr files

By using the rename command with a perlexpr regular expression, you can rename files based on specific patterns. If you need more information on perl regular expressions, you can read this resource.

For instance, the following command would replace the extension .html with .php for all files:

ShellScript
rename 's/.html/.php/' \*.html

Use the -n option to print names of files to be renamed, without actually renaming them.

ShellScript
rename -n 's/.html/.php/' \*.html

The resulting display will have a similar appearance to the following:

Output:

rename(file-90.html, file-90.php)
rename(file-91.html, file-91.php)
rename(file-92.html, file-92.php)
rename(file-93.html, file-93.php)
rename(file-94.html, file-94.php)

The rename command does not overwrite existing files by default. To enable the overwriting of existing files, use the -f option:

ShellScript
rename -f 's/.html/.php/' \*.html

Here are some additional typical ways to utilize the rename command:

  • Replace spaces in filenames with underscores
ShellScript
rename 'y/ /\_/' \*
  • Convert filenames to lowercase
ShellScript
rename 'y/A-Z/a-z/' \*
  • Convert filenames to uppercase
ShellScript
rename 'y/a-z/A-Z/' \*

Conclusion

In our demonstration, we illustrated how to utilize the rename and mv commands to change the names of files in Linux. Alongside these commands, there exist other alternatives to rename files, including mmv. For those new to Linux who may find the command line intimidating, there are also GUI batch rename utilities available such as Métamorphose.

Please don’t hesitate to share your inquiries or feedback with us by leaving a comment.

Alex

Alex

Hey there! My name is Alex and I'm a professional content writer. I'm also lucky enough to be part of an amazing trio team! I absolutely love what I do and I'm passionate about creating content that engages, informs, and entertains. Whether it's writing blog posts, website copy, or social media content, I always strive to deliver high-quality work that exceeds expectations.

We will be happy to hear your thoughts

Leave a reply

TrioGuide
Logo