Linux mv | Linux Move File

Here ‘mv’ stands for move and is used to move the existing file or directory from one location to another in the system. It can also be used to rename a file or directory. It is better to use ‘mv’ option if you want to rename a single directory or file.

How To Rename a File

The inode number of both files will still remain the same while renaming a file.

codes@w3schools:~/Downloads$ ls -i -l
 
total 4
 
657983 -rw-rw-r-- 1 codes codes 0 may 26 14:15 docc
 
657953 -rw-rw-r-- 1 codes codes 0 may 26 14:01 file1.txt
 
658816 -rw-rw-r-- 1 codes codes 0 may 26 10:15 file2.txt~
 
657891 drwxrwxr-x 2 codes codes 4096 may 26 13:58 imp
 
codes@w3schools:~/Downloads$
 
codes@w3schools:~/Downloads$ mv docc document
 
codes@w3schools:~/Downloads$
 
codes@w3schools:~/Downloads$ -i -l
 
total 4
 
657983 -rw-rw-r-- 1 codes codes 0 may 26 14:15 document
 
657953 -rw-rw-r-- 1 codes codes 0 may 26 14:01 file1.txt
 
658816 -rw-rw-r-- 1 codes codes 0 may 26 10:15 file2.txt~
 
657891 drwxrwxr-x 2 codes codes 4096 may 26 13:58 imp
 
codes@w3schools:~/Downloads$

Let’s see the above example; we have renamed the file named ‘docc’ into ‘document’. But the inode number of both the files are still the same.

How To Rename a Directory

It can be renamed the same way as the files and the inode number will remain the same in this case too.

codes@w3schools:~/Downloads$ ls -i -l
 
total 12
 
657951 drwxrwxr-x 2 codes codes 4096 may 26 15:28 dir
 
658816 -rw-rw-r-- 1 codes codes 0 may 26 10:15 file2.txt~
 
658812 drwxrwxr-x 2 codes codes 4096 may 26 15:28 fixed
 
657891 drwxrwxr-x 2 codes codes 4096 may 26 13:58 imp
 
codes@w3schools:~/Downloads$
 
codes@w3schools:~/Downloads$ mv dir tmp
 
codes@w3schools:~/Downloads$
 
codes@w3schools:~/Downloads$ ls -i -l
 
total 12
 
658816 -rw-rw-r-- 1 codes codes 0 may 26 10:15 file2.txt~
 
658812 drwxrwxr-x 2 codes codes 4096 may 26 15:28 fixed
 
657891 drwxrwxr-x 2 codes codes 4096 may 26 13:58 imp
 
657951 drwxrwxr-x 2 codes codes 4096 may 26 15:28 imp
 
codes@w3schools:~/Downloads$

mv Option

Option Function
mv -i This option asks for permission to over write.
mv * This is used to move multiple files to a specific directory.
mv –suffix It is used to take backup before over writing.
mv -u This option only moves those files that don’t exist.

 

Please follow and like us:
Content Protection by DMCA.com