How to Easily Rename Files and Directories in Linux Terminal

 Renaming files and directories is one of the most basic tasks that you often need to perform on Linux systems.

Renaming a single file is easy, but renaming multiple files at once can be a challenge, especially for users new to Linux. You can rename files using the File manager in systems that support GUI. Then what if you work in a system that only supports the command line?.



It is expected that you open the terminal console of the Linux distribution to follow the guide on this page to better understand how to change the name of a file or directory through Linux Terminal.

We also recommend you to test any tutorial or any guide that exists on the Internet in a virtual machine (vmware or virtualbox) before applying to a production server, so as not to mess up the system that is actively running when there are errors.

You can see how to install VMware on Ubuntu and CentOS, and VirtualBox on Ubuntu, Fedora, and CentOS

In this tutorial, we will show you how to use the mv command and the rename command to rename files and directories.


Renaming a file with the mv command

The mv command (short for move) is used to rename or move files from one location to another.

The syntax for the mv command is as follows:

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 specify multiple files as the source, the destination must be a directory. In this case, the source files are moved to the target directory.

If you specify a single file as the source, and the destination target is an existing directory then the file will be moved to the specified directory. To rename a file, you need to specify one file as the source, and one file as the destination target.

For example, to rename a file from file1.txt file is 2.txt you can run the following command:

mv file 1.txt file 2.txt


How to rename multiple files with MV command

The mv command can only rename one file at a time, but it can be used in conjunction with other commands, such as the find command or in bash for or while loop scripts to rename multiple files.

The following example shows how to use Bash for loop to rename all files .txt in the current working directory by changing the extension .txt to be .php.

for f in *.txt; do 

        mv -- "$f "" ${f%.txt}.php" 

done

Let's analyze the code line by line:

The first line creates a loop for the For statement, to search all the list of files ending with .txt.

The second line applies to each item in the list and moves the file to the one the new format replaces .txt with .php. Section ${file%.txt} using shell parameter expansion to delete a section .txt of the file name.

done indicates the end of the loop segment.

We can also use the mv command in combination with find to achieve the same result as above.

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

The find command goes through all the files it ends with .txt in the current directory to the mv command one at a time using the-exec switch. String {} is the name of the file to be processed.

As you can see from the example above, renaming some files using the mv command is not an easy task as it requires a good knowledge of Bash scripts.



Rename the file with the rename command

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

There are two versions of the rename command with different syntax. In this tutorial, we will use the perl version of the rename command. If you haven't installed rename on your system, you can easily install it using your distribution's package manager.

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 of the rename command is as follows :

rename [OPTIONS] perlexpr files

The rename command will rename all files according to the specified perlexpr regular expression. You can read more about perl regular expressions here.

For example, the following command will change all files by extension .txt to. py:

rename 's/.txt/.py/' *.txt

You can use the-n argument to print the names of the files to be renamed, without changing their names.

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

Output of the above command :

renaming (10).txt, file-10.py) 

the name(Chapter 11).txt, file-11.py) 

renaming (12).txt, file-12.py) 

the name(Page 13).txt, file-13.py) 

name of the file (14).txt, file-14.py)

By default, the rename command will not overwrite existing files. Use the-f argument to allow existing files to be overwritten.

rename-f 's/.txt/.py/' *.html

Below are some common examples of how to use the rename command :

Change spaces in filenames to underscores (e.g. this_is_file_name)

rename 'y//_/' *

Change the file name to lowercase (all lowercase)

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

Change the file name to uppercase(all uppercase)

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


Conclusion

Now you already have a good understanding of how to use the mv command and the rename command to rename files. Of course there are other commands to rename files in Linux such as mmv. New Linux users who are intimidated by the command line can use batch GUI renaming tools such as metamorphose.

Post a Comment

Previous Post Next Post

Contact Form