This post will quickly guide about how to rename file Linux. Basically to rename file in Linux you need to use mv command. This will be used to rename file as well as directories in Linux.
Basic Syntax for Rename file Linux:
mv [Options] <Old file> <New file> mv <Old file> <New file>
Examples for Rename file Linux:
In the basic form you need to give the old file name followed by the new file name in the mv command in order to rename file in Linux. Lets say you have a file called “uxtechno.txt” and you want to rename it to “Linuxroutes.txt”. Then use below command to do the same.
[root@rhel1]# mv uxtechno.txt Linuxroutes.txt
The above method is called as relative method since we are giving up an command from the directories where files are located. In case you don’t want to go inside the directories then can use below command with full path of file name. This method is called as absolute path.
[root@rhel1]# mv /tmp/mann/uxtechno.txt /tmp/mann/Linuxroutes.txt
Verbose output for Rename file Linux
In case you want to have details of what is being renamed. Then you must use -v option along with mv command as below:
[root@rhel1]# mv -v uxtechno.txt Linuxroutes.txt `uxtechno.txt' -> `Linuxroutes.txt' [root@rhel1]#
In the above output command displays the detail which confirms that, the file “uxtechno.txt” being renamed to “Linuxroutes.txt”.
Interactive rename file Linux
You can make rename file operation interactive meaning that system asks for confirmation before going ahead for renaming a file in Linux in case file already exist at the destination.
[root@rhel1]# mv -i uxtechno.txt Linuxroutes.txt mv: overwrite `Linuxroutes.txt'? y [root@rhel1]#
Renaming or moving the file only when source file is updated:
Suppose you have same file at source and destination and you want to move the file from source location to destination only when source is updated than destination or destination file does not exits. In that case you need to use option “-u along with mv command:
mv -u /tmp/Linuxroutes.dat /tmp/mann/Linuxroutes.dat
Renaming Multiple files:
In case you want to rename multiple files in one go then you must use rename command. Lets say you have files with “.txt” extension and you want to rename it to “.dat” extension.
[root@rhel1]# ll total 20 -rw-r--r-- 1 root mmadmin 18 May 24 12:24 1.txt -rw-r--r-- 1 root mmadmin 18 May 24 12:24 2.txt -rw-r--r-- 1 root mmadmin 18 May 24 12:24 3.txt -rw-r--r-- 1 root mmadmin 18 May 24 12:25 4.txt -rw-r--r-- 1 root mmadmin 18 May 24 12:22 Linuxroutes.txt
[root@rhel1]# rename .txt .dat *.txt [root@rhel1]# ll total 20 -rw-r--r-- 1 root mmadmin 18 May 24 12:24 1.dat -rw-r--r-- 1 root mmadmin 18 May 24 12:24 2.dat -rw-r--r-- 1 root mmadmin 18 May 24 12:24 3.dat -rw-r--r-- 1 root mmadmin 18 May 24 12:25 4.dat -rw-r--r-- 1 root mmadmin 18 May 24 12:22 Linuxroutes.dat [root@rhel1]#
Leave a Reply