How To Rename Files in Linux

Try this guide with our instant dedicated server for as low as 40 Euros

Rename Files in Linux

You can easily rename a single file in Linux by simply right-clicking and renaming the file by choosing the Rename option.

Now, imagine you need to rename 100 (or more) image files in a folder. You can imagine the hassle and the time commitment for this activity. Renaming files and directories is an everyday activity that many of us don’t give much thought to.

Renaming files in Linux, especially the bulk naming, might seem daunting if you’re new to the command-line environment. However, with the proper knowledge and tools, you can easily rename files in bulk. In this guide, we’ll explore various techniques to make file renaming in Linux a breeze!

Table Of Contents

  1. How to Rename Files in Linux
    1. Method #1: Rename Files with the mv Command
      1. The Basic Syntax
      2. How to Use mv to Rename a Single File
      3. How to Use mv to Rename Multiple Files
  2. Method #2: Rename Files with the rename Command
    1. Install the rename Command
    2. The Basic Syntax
    3. How to Use rename to Rename File Extensions
    4. How to Use rename to Replace a Part of a Filename
    5. How to Use rename to Delete Part of a Filename
    6. How to Use rename to Rename Files With Similar Names
    7. How to Use rename to Convert Filenames to Uppercase
    8. How to Use rename to Convert Filenames to lowercase
  3. Conclusion
  4. FAQs

Let’s start with the prerequisites:

Prerequisites

You need the following to apply the techniques discussed in this tutorial:

  • A system running a Linux OS
  • A user account with root access

Important: Always backup your data before trying the steps outlined in this tutorial

How to Rename Files in Linux

We’ll now discuss two ways of renaming files in Linux.

Method #1: Rename Files with the mv Command

The mv (short for move) command has been a time-honored way of renaming and moving files. The simplicity and ubiquity of this command make it a go-to tool for many Linux users.

The Basic Syntax

The basic syntax of the mv command for renaming a file is:

mv [current_filename] [new_filename]

How to Use mv to Rename a Single File

Consider the following command that renames a file (oldfile.txt) to newfile.txt:

mv oldfile.txt newfile.txt

The command doesn’t provide any feedback about the success or failure of the operation. So, to check whether the rename operation was a success, use the ls -l command:

ls -l

How to Use mv to Rename a Single File

You can also include the path to the correct directory if you are not in the right directory.

During the command’s execution, when you specify a directory as the destination, the source file will be relocated to that directory. However, if the destination has a different file name, the mv command will change the name of the source file to match the new name.

How to Use mv to Rename Multiple Files

Let’s say you want to change all .txt files to .bak. This task is more complex and typically requires pairing mv with other commands, such as for loops or rename.

Here’s a simple loop example:

!/bin/bash
for f in *.txt; do
mv -- "$f" "${f%.txt}.pdf"
done

In this script, the first line scans the current directory for all files with the .txt extension. The second line uses the mv command to alter the extension of each discovered file from .txt to .pdf. The final line ends the loop section.

To use this script, add it to a file and save it as rename.sh. Next, use the sh command in the terminal to run it:

sh rename.sh

While the mv command may look simple in these examples, it is a robust utility with several options and parameters. We recommend checking the man pages with the man mv command for a deeper dive into its capabilities.

Method #2: Rename Files with the rename Command

The mv command is a versatile option for simple file renaming. However, sometimes you require a more specialized tool, especially when you need to use patterns or batch renaming.

That’s where the rename command shines.

The rename command in Linux allows you to rename multiple files and directories. Compared to the mv command, it provides additional capabilities. However, this robustness comes at the cost of complexity because the rename command uses Perl expressions.

Install the rename Command

The rename command is often not available by default on most Linux distributions.

So, the first step is to install it on your system:
If you’re on a Debian-based distribution, use the following command:

sudo apt install rename

On RHEL-based distributions, use:

sudo yum install prename

The Basic Syntax

The rename command uses a Perl-based syntax for renaming files. The basic format is:

rename 's/[old_pattern]/[new_pattern]/' [files]

In this syntax, ‘s’ stands for substitution, [old_pattern] is what you’re looking to replace, and [new_pattern] is what you’re replacing it with.

How to Use rename to Rename File Extensions

We’ll use the rename.sh script from the mv command section to illustrate this command that converts the file extension from .txt to .pdf:

rename -v 's/.txt/.pdf/' *.txt

How to Use rename to Rename File Extensions

How to Use rename to Replace a Part of a Filename

The standard syntax remains consistent when you use the rename command to modify other portions of the filename. For instance, use the following command to change the names of example1.txt, example2.txt, and example3.txt to test1.txt, test2.txt, and test3.txt:

rename -v 's/sample/test/' *.txt

How to Use rename to Replace a Part of a Filename

How to Use rename to Delete Part of a Filename

You can use the rename command to remove a portion of the filename by leaving out the replacement segment of the expression.

rename -v 's/ample//' *.txt

How to Use rename to Delete Part of a Filename

How to Use rename to Rename Files With Similar Names

A great use case for the rename command is the requirement to rename files with similar names. For example, if you wish to rename files containing examples and sample in their names to test, use the following command:

rename -v 's/(ex|s)ample/test/' *.txt

How to Use rename to Rename Files With Similar Names

How to Use rename to Convert Filenames to Uppercase

If you wish to convert the filenames from lowercase to uppercase, use the following command:

rename -v 'y/a-z/A-Z/' *.txt

How to Use rename to Convert Filenames to Uppercase

How to Use rename to Convert Filenames to lowercase

On the flip side, switching the order of uppercase and lowercase characters in the expression yields the opposite result:

rename -v 'y/A-Z/a-z/' *.TXT

How to Use rename to Convert Filenames to lowercase

The rename command provides a flexible way to rename files, especially when dealing with patterns or multiple files simultaneously. It’s a powerful tool that requires a deeper understanding of Perl expressions.

We strongly recommend that you back up your files or test changes first to ensure you have the option to roll back changes.

Conclusion

Knowing how to rename files in Linux is a deceptively robust operation.

Whether you’re using the simple mv command for quick renames or the rename command for changes involving patterns, understanding the command syntax is essential.

Are you looking for the most economical dedicated server pricing? Look no further than RedSwitches instant dedicated servers, available in various configurations, including 10Gbps dedicated servers and bare metal servers. We’re your trusted hosting partner, providing the right dedicated server for your needs.

FAQs

Q. How can I rename a single file in Linux?

To rename a single file in Linux, use the following command:

mv old_filename new_filename

Q. How do I rename multiple files in Linux?

To rename multiple files in Linux, use the following command:

mv old_filename1 old_filename2 new_filename

Q. Can I rename files in subdirectories?

Yes, you can rename files in subdirectories by specifying the subdirectory’s path along with the filename in the mv command.

Q. Is there a way to undo file renaming in Linux?

Unfortunately, there is no built-in way to undo file renaming in Linux. That’s why we strongly urge you to take a backup of your files before renaming them.

Q. How can I move files while renaming them?

To move files while renaming them in Linux, use the mv command followed by the path to the file and the new destination.

Q. Can I rename files using a bash script?

Yes, you can create a bash script to automate the file renaming process in Linux. Simply write a script that includes the necessary mv commands.

Q. What is the difference between renaming and moving a file in Linux?

Renaming a file changes its name while keeping it in the same location, while moving a file changes its location while keeping the same name.

Q. How can I easily rename multiple files in Linux?

To easily rename multiple files in Linux, use the rename command followed by a pattern and the desired new name.

Q. Are there any advanced techniques for file renaming in Linux?

Yes, Linux offers several advanced techniques for file renaming, including using regular expressions, the find command, and support for scripting languages like Python.

Try this guide with our instant dedicated server for as low as 40 Euros