How To Rename A File In Linux?

Being a Linux user, we all want to update ourselves with basic commands that help us in operating the system easily. Commands like how to rename a file in Linux or rename multiple files in Linux are mostly used.

To rename a file in Linux is not a difficult task. Once you get to know how to use this command, you can easily perform this function. Linux users know that there are several commands to rename a file like using CLI (command line interface) or by using the standalone application.

In the coming sections, we will discuss how to rename a file in the Linux terminal or how to use the move command to rename directories or files.

How To Use Move Command In Linux?

A very simple way to move or rename files and folders is using the mv command (Short form of move). The main function of this command is to move files or folder, along with renaming them.

The syntax for    mv     command is as follows:

mv [OPTIONS] source destination

And the syntax used to rename a file in Linux:

mv old-file-name new-file-name
mv [options] old-file-name new-file-name
mv file1 file2

Renaming A File Using ‘mv’ Command:

Let us understand using an example. We have to rename a file called “xyz.pdf” to “abc.pdf”.  Now open a command line terminal and follow these steps.  Select Applications > Accessories > Terminal, then enter the following syntax:

mv xyz.pdf abc.pdf

If xyz.pdf is located in /home/user/docs/files directory types:

cd /home/user/docs/files
mv xyz.pdf abc.pdf

Or you can use:

mv /home/user/docs/files/xyz.pdf
/home/user/docs/files/abc.pdf

Now, to view this file use syntax:

ls -l file1
ls -l file1 file2
ls -l /home/user/docs/files/*.pdf
ls -l *.pdf

NOTE:  The mv command always asks permission for the folder that contains files.  For system files and folders you may need to get root permissions to rename a file by prepending mv with sudo or su. There is also an advance protection layer that is provided by interactive  ( -i ) which asks you to confirm the file rename before applied.

All the changes that you made using the mv command can also be listed by using   -v (verbose) option.

How To Rename A File In Linux?

As discussed earlier, we can get a verbose output which means that the mv command will explain what is being done using the following syntax.

mv -v file1 file2

The Sample output will be:

`file1' -> `file2'

Making mv interactive to pass the    -i    option.  This option will come just before overwriting files and recommended for new users. Let just see the following syntax:

mv -i file1 file2

Now, the output will come as:

mv: overwrite `file2'? y

Rename Multiple Files In Linux

This is the advanced version of mv command. Here you can change the name of multiple files and folders. Let us discuss how to rename a folder in Linux with the following commands.

Here’s the syntax of rename command:

rename 's/oldname/newname/' files
rename (option) 's/oldname/newname/' 

Understand this with the help of the following examples to rename all text files (*.text) to *.txt, enter:

rename 's/text/txt/' *.text

Here, the attribute “s” stands for “substitute”, which is an important part of the regular expression. More options are:

  • -v (verbose: shows the list of all renamed files with their new names)
  • -n (no action: a kind of test more which shows some files that will be changed without touching them)
  • -f ( original files that are forced overwrite)

The rename command also uses wildcards to rename multiple files that are of the same type and work over their extensions too. The above example explains this.  The wildcard symbol (*) means all the files in the folder will be affected.

You can also use the syntax:

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

Here is the sample output:

'war.text' renamed to 'war.txt'
'food.text' renamed to 'food.txt'
'input.text' renamed to 'input.txt'
'output.text' renamed to 'output.txt'
'test.text' renamed to 'test.txt'

You can also use the command   -n   , by which users can see the preview of what the renamed files will look, and they don’t need to put their files at any risk.

Now with all the above discussions, you will get to know how to rename a file in Linux. Let us know how you can remove a rename command.

How To Remove Rename Command?

Along with the above-discussed process, you no longer want to have a renamed file installed on your computer. Then you just simply remove it using the software manager or terminal. 

The syntax for Ubuntu, Debian, Linux Mint users:

sudo apt remove rename

And for the CentOD and RHEL users:

sudo yum remove rename
Conclusion:

Rename a file in Linux is an easy task using the terminal, but somehow it is important to do perfectionally. Every Server manager should know how to do this task.  We have discussed two commands in this article, “mv” and “rename” command to rename a file on Linux.

Last but not the least, there are not only two commands that can rename a file or folder. Other commands such as mmv, and those who are new Linux user can use GUI batch rename tool such as Metamorphose, pyRenamer, and so on.

We suggest you to use your better syntax and do more research to find such commands that can increase and improve the quality of your workflow.

We will be happy to hear your thoughts

      Leave a reply

      Get Best Offers For All Web Tools & Services
      Logo