In Linux, a command-line terminal is an essential tool for server administration. It provides users a wide range of productivity tools while saving machine resources. To make the most out of the operating system, it’s crucial to have a strong foundation in the fundamentals of simple Linux commands, such as renaming existing files and folders. This tutorial will focus on how to rename files in Linux.

Renaming Files in Linux with the ‘mv’ Command

The ‘mv‘ command, which stands for “move,” is one of the easiest commands. It can perform two basic but critical functions when handling files on Linux. The first is moving files from one location to another, and the second is renaming one or more files through the terminal.

To rename files using ‘mv,’ access the server through the command line using SSH. Open the terminal from the main menu if you’re using a local computer instead of a server. If you need clarification on SSH and want to learn more, here’s a helpful tutorial.

Before we proceed, it’s essential to understand how the ‘mv’ command works. 

To do this, type the following into your terminal: 

mv --help

The basic use of the ‘mv‘ command is as follows:

mv [option] [SOURCE]...[DIRECTORY]

Here are some of the most popular ‘mv’ options:

-f: Shows no message before overwriting a file. 

-i: Displays warning messages before overwriting a file. 

-u: Only moves a file if it’s new or doesn’t exist in the destination. 

-v: Shows what the command does.

The parameters are: 

[SOURCE]: The source destination of the file 

[DESTINATION]: The destination directory

To rename a file using ‘mv,’ type the following: 

mv oldnamefile1 newnamefile1

Suppose we are in the directory, and there is a file called ‘file1.txt,’ and we want to change its name to ‘file2.txt.’ In that case, we will need to type the following: 

mv file1.txt file2.txt

However, you must type more if you are outside the directory. 

For example: cd /home/user/docs/files mv file1.txt file2.txt

Renaming Multiple Files with the ‘mv’ Command

The ‘mv’ command can only rename one file but can be used with other commands to rename multiple files. For example, to change all files in your current directory from .txt extension to .pdf extension, use the following command:

for f in *txt; do 

mv — “$f” “${f%.txt}.pdf” 

done

This creates a loop (‘for’) that looks through the list of files with the extension .txt. It replaces each .txt extension with .pdf and ends the loop (‘done’).

Renaming Files in Linux Using the ‘rename’ command

The ‘rename’ command offers more control than ‘mv.’ Many Linux configurations include it by default. If you don’t have it installed, you can install it in just a minute with a simple command.

For Debian, Ubuntu, Linux Mint, and derivatives: 

sudo apt install rename

For CentOS 7 or RHEL: 

sudo yum install rename

For Arch Linux: 

yay perl-rename ## or yaourt -S perl-rename

The basic syntax of the ‘rename’ command is: 

rename 's/old-name/new-name/' files

To illustrate how to use the ‘rename’ command, we will create a new folder called ‘filetorename’ and use the ‘touch’ command to create five files.

mkdir filetorename

cd filetorename

touch file{1..5}.txt

ls

With the last ls command, you can view the files you created.

If we want to rename a single file called file1.txt, it will be like this:

rename ‘s/file1/newfile1/’ file1.txt

If we wanted to change the extension to all files, such as .txt to .php. It can be done in this way:

rename ‘s/.txt/.php/’ *.txt
ls

We can also specify another directory for the files you want to rename.

rename 's/.txt/.php/' FILE/PATH

Rename uses a regular expression of Perl, meaning this command has extensive possibilities.

Finally, it is a good idea to check all the command options. You can view them in the terminal by executing the following:

rename –help

Removing the Rename Command 

If you no longer need to use the rename command on your system, you can uninstall it using the software manager or the terminal.

To remove rename on Debian, Ubuntu, Linux Mint and derivatives, enter the following command in the terminal:

sudo apt remove rename

For CentOS and RHEL, use the following command:

sudo yum remove rename

This will remove the rename command from your Linux machine.

Conclusion 

Renaming files using the terminal in Linux is a straightforward and practical task essential for server managers. As we have seen, two commands can accomplish this task. While one is simpler than the other, both are effective.

We encourage you to continue exploring these commands and improving your workflow to enhance productivity.

Kirti S

Kirti S