Linux Rename a File, Folder or Directory 2022 Till 2050 [Updated]

How to Rename Linux File, Folder, or Directory; Easy Way to Rename Linux Files


Linux Rename uses the mv (more) command in order to rename either a folder (directory) or a file.

mv old_folder_name new_folder_name

mv also has the ability to move several files into a destination folder, or rename a single file.

Rename a file

mv old_file_name new_file_name

Rename a directory.

mv file_name destination_directory

The renamed file will still retain the attributes and the date of creation as the original file/folder.

Linux Rename a File, Folder or Directory 2022 Till 2050 [Updated]

File Rename in Linux: Method Explained in Detail Below

Renaming files and directories are one of the most basic tasks you need to do on your Linux system. You can rename the file using the GUI file manager or using the command line terminal.

Renaming a file is easy, but renaming multiple files at once can be difficult, especially for users who are new to Linux.

This tutorial shows you how to use mv and rename commands to rename files and directories.

Renaming Files with the mv Command

The mv command (move) is used to rename or move a file from one location to another. The syntax for the mv command is:

mv [OPTIONS] source destination

The source can be one or more files or directories and the destination can be a single file or directory.

  • If you define multiple files as a source, the destination must be a directory. In this case, the source files will be moved to the destination directory.
  • If you specify a single file as the source and the destination is an existing directory, the file will be moved to the specified directory.
  • To rename the file, you must specify a single file as the source and a single file as the destination.

As an example, to rename the file file1.txt as file2.txt you need to run:

mv file1.txt file2.txt

Renaming multiple Linux files Using mv Command

For mv command it is only possible for it to rename one file at a time, but if it is set to be used with other commands such as find or inside bash for or while loops can be used to rename multiple files.

Here in these examples, you can see how to use the Bash for loop to rename all .html files that are present in the current directory by changing the .html extension to .php.

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

Let’s analyze the code in detail.

  • The first line creates a loop of for and repeats the list of all the files enclosed in .html.
  • The second line applies to each item in the list, moving the file to the new one and replacing .html with .php. The $ {file% .html} section uses a shell parameter extension to remove the .html section from the file name.
    complete marks the end of the loop section.

In the following example there is the use of mv in combination with find to achieve the same as above:

find . -depth -name "*.html" -exec sh -c 'f="{}"; mv -- "$f" "${f%.html}.php"' \;
  • The Find command uses the option to transfer all the .html files to the current directory to go one by one. The.. String is the name of the file being processed
  • As you can see from the example above, renaming multiple files using the mv command is not a trivial task as it requires a good knowledge of Bash scripts.

Renaming multiple Linux files with the rename Command

The rename command is used to rename multiple files. This command is more advanced than mv because it requires basic knowledge of standard expressions.

There are two versions of the renaming command with different syntax. This tutorial uses the Perl version of the renaming command. If this version is not installed on your system, you can easily install it using the package manager of your distribution.

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

The syntax for the command of rename is as follows:

rename [OPTIONS] perlexpr files

The command of rename will rename the files in accordance with the specified perlexpr regular expression. 

The command below will change all files with the extension .html to .php:

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

It is possible to use the -n option to print names of files that are to be renamed, without renaming them.

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

The output will appear as something like:

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 the command does not overwrite the existing files. If you wish to do that then pass the -f option:

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

More examples of how to use the rename command:

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

Linux Rename a File, Folder, or Directory

Now you know how to use both the mv and rename commands to rename Linux files.

There are also other commands to rename files in Linux, which include mmv. If you are new to Linux and these command lines seem intimidating then you can turn to the GUI batch rename tools.

Download Now


Leave a Reply

Your email address will not be published. Required fields are marked *