The Complete Guide: Rename a File on Linux

This is a guide on how to rename a Linux file. Basically, to rename a file on Linux, you need to use the mv command. It will be used to rename files as well as directories in Linux.

Basic syntax for renaming a Linux file:

mv [Options] <Old file> <New file>
mv <Old file> <New file>

Examples for renaming Linux files:

In basic form, you need to give the name of the old file followed by the new file name in the mv command in order to rename the file in Linux. Let’s say you have a file named “andreyex.txt” and you want to rename it to “Linuxroutes.txt”. Use the following command:

[[email protected]]# mv andreyex.txt Linuxroutes.txt

The method described above is called the relative method, since we issue the command from the directories where the files are located. In case you do not want to go to the directory, then you can use the following command specifying the full path of the file. This method is referred to as an absolute path.

[[email protected]]# mv /tmp/mann/andreyex.txt /tmp/mann/Linuxroutes.txt

Full output for renaming a file in Linux

In case you want to know information about renaming. Then you must use the -v option along with the mv command as shown below:

[[email protected]]# mv -v andreyex.txt Linuxroutes.txt
`andreyex.txt' -> `Linuxroutes.txt'
[[email protected]]#

The above command line displays information that confirms that the file “andreyex.txt” has been renamed to “Linuxroutes.txt”.

Renaming files interactively in Linux

You can do the file rename operation interactively, the system asks for confirmation before renaming the file in Linux in case the file already exists in the destination.

[[email protected]]# mv -i andreyex.txt Linuxroutes.txt
mv: overwrite `Linuxroutes.txt'? y
[[email protected]]#

Rename or move a file only when the original file is updated:

Suppose you have the same file in source and destination and you only want to move the file from source to destination when the source is updated. In this case, you need to use the -u option along with the mv command:

mv -u /tmp/Linuxroutes.dat /tmp/mann/Linuxroutes.dat

Renaming multiple files:

In case you want to rename several files at one time, then you must use the rename command. Let’s say you have files with the extension “.txt” and you want to rename it to the extension “.dat”.

[[email protected]]# ll
total 20
-rw-r--r-- 1 root mmadmin 18 May 27 14:36 1.txt
-rw-r--r-- 1 root mmadmin 18 May 27 14:36 2.txt
-rw-r--r-- 1 root mmadmin 18 May 27 14:36 3.txt
-rw-r--r-- 1 root mmadmin 18 May 27 14:37 4.txt
-rw-r--r-- 1 root mmadmin 18 May 27 14:36 Linuxroutes.txt

[[email protected]]#  rename .txt .dat *.txt
[[email protected]]# ll
total 20
-rw-r--r-- 1 root mmadmin 18 May 27 14:36 1.txt
-rw-r--r-- 1 root mmadmin 18 May 27 14:36 2.txt
-rw-r--r-- 1 root mmadmin 18 May 27 14:36 3.txt
-rw-r--r-- 1 root mmadmin 18 May 27 14:37 4.txt
-rw-r--r-- 1 root mmadmin 18 May 24 12:22 Linuxroutes.dat
[[email protected]]#

Sidebar