How to Rename Files and Directories in Linux?

Posted in

How to Rename Files and Directories in Linux?
maazbinasad

Maaz Bin Asad
Last updated on November 21, 2024

    You can either use a GUI-based file manager or a command-line to manage your day-to-day operations related to files, such as copying, creating, renaming, deleting, updating, etc. While it's true that a GUI-based file manager allows you to perform some operations quickly, having a grip on the basic Linux commands related to file and directories will be quite useful. It is so because you get quite limited customization options with GUI-based file managers. To filter out results in the desired format, perform an efficient search, and use several other options, the command-line is always a better option. Renaming files is one of the most common operations when it comes to manipulating files. Renaming files using a file manager is simple. Just right-click on the file, click on the rename option, type the new name, and you are good to go. But performing renaming operations with the command line is tricky, especially if you want to rename multiple files together. However, you need not worry as we have got your back. In this comprehensive guide, we will walk you through different methods that you can use to easily rename files and directories through the Linux command line. We will discuss two main methods or commands called mv and rename to rename files and directories in Linux.

    Prerequisites

    To move forward with renaming multiple files at a time, you need to understand for-loops in bash scripting. If you have any coding experience, you might already know how loops work. Also, you need access to a Linux machine, a command shell, and sudo privileges to rename files and directories. Keep in mind that working with sudo privileges is risky as you might end up messing with your system. So, you need to use these commands carefully.

    Rename Files and Directories with the Move (mv) Command

    Although the mv (move) command is ideal for relocating or transferring files from one path to another, we can also use it to rename files and folders. The syntax of the mv command is as follows:

    $ mv [options] {source} {destination}

    We can include multiple files or directories in the source field. However, in the destination field, we should include only a single directory or file. Following are the different scenarios for using :

    1. If we include more than one file as the source, then our destination has to be a directory. This is so because all the files defined in the source will be moved to this directory.
    2. If we include a single file in the source field and define a path to a directory in the destination field, then the single file will be moved to this directory.
    3. In the third case, if we specify a single file in both the source and destination fields and there is no file named as the one we have mentioned in the destination, then the source file gets renamed. Let’s try to rename a file using the mv command in Linux with the following command:
    $ mv ~/sample/file1.txt ~/sample/file2.txt

     $ mv ~/sample/file1.txt ~/sample/file2.txt

    Similarly, you can mention a directory name in both the source and destination to rename the source directory to the one mentioned in the destination.

    $ mv ~/sample/folder1 ~/sample/folder2

     $ mv ~/sample/folder1 ~/sample/folder2

    Rename Multiple Files with mv Command

    Although the mv command can rename only a single file once, we can use it along with other techniques such as loops (for or while in bash) or find command. Let’s use a simple for loop to rename all the text files in a directory to change their extensions to .cpp.

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

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

    The first line “ for f in *.txt; do ”, asks the command to loop through all the files having a .txt extension and perform the operation mentioned in the next line on each of them. Now, the next line in the specified command is “mv -- "$f" "${f%.txt}.cpp"”. It asks the command to perform the move operation on each selected file and change their extensions. The last line, i.e., done, denotes the completion of the loop. You can see that the extension of all the text files has been changed successfully. You can customize for loop according to your requirements. We can also use the mv command along with the find command to get the same output.

    $ find . -depth -name "*.txt" -exec sh -c 'file="{}"; mv -- "$file" "${file%.txt}.cpp"' \;

    In the above program, the find command will select each file with the .txt extension one by one and feed it to the move command using the -exec option which will change its extension. $ find . -depth -name "*.txt" -exec sh -c 'file="{}"; mv -- "$file" "${file%.txt}.cpp"' \;

    Rename Files and Directories with the Rename Command

    We can use the rename command provided by a package called rename in almost all popular Linux distros to change the name of more than one directory or file simultaneously. To use this command, you need to have a basic knowledge of regex . For our example, we are going to install the rename package that can utilize Perl regex syntax. You can download the package using the below command if you have Ubuntu or any other Debian-based Linux distribution:

    $ sudo apt install rename

    The syntax for the rename command is as follows:

    $ rename [options] 's/old/new/' {all files}

    Let’s try to rename all the .txt files to .html files.

    $ rename 's/.txt/.html/' *.txt

     $ rename 's/.txt/.html/' *.txt

    You can also use the -n option to preview files that will be renamed along with their old and new names without actually performing the rename operation.

    $ rename -n 's/.html/.txt/' *.html

     $ rename -n 's/.html/.txt/' *.html

    Wrapping Up

    In this tutorial, we skimmed through some reasons why we should use the command-line to perform file-based operations rather than using GUI-based file managers. We also discussed a few commands to change the name of files and directories in Linux. We started with renaming a single file and a single directory using the move command and then discussed how to change the name of multiple files using for loop and find command in conjunction with the mv command. Moving ahead, we also mentioned how to use the rename package along with perlexpr regular expressions to rename files and directories in Linux. We hope that now you have a better understanding of the different renaming methods available in Linux. Moreover, through the examples that we highlighted, you now have hands-on experience working with these commands as well. People are also reading:

    Leave a Comment on this Post

    0 Comments