10 Examples of mv Command

‘mv’ Command in Linux/Unix: Move (rename) files

“mv” Command is used to move any type of file and directory from one file system to another (or one location to another), And it also used to rename a file or directory.

Previous article we learnt about “cp” command. “mv” command is equivalent to “cp” command,but it does not creates any copy and duplicate files or directories and Apart from this it also used to rename a files or directory.

mv command syntax;
     mv [OPTION]... SOURCE DEST
     mv [OPTION]... SOURCE... DIRECTORY
     mv [OPTION]...  DIRECTORY SOURCE...

mv command options:

“-i”, –interactive              Prompt whether to overwrite each existing destination file,regardless of its permissions.

“-S”, –suffix=SUFFIX     Override the usual backup suffix
“-t”, –target-directory=DIRECTORY   Specify the destination Directory.
“-T”, –no-target-directory    Treat destination as a normal file.
“-u”, –update                 Move only when the SOURCE file is newer than the destination file or when the         destination file is missing
“-v”, –verbose                Print the name of each file before moving it.

1) Perform mv command without any option.

As below Example, We move “samplefile1” file from current directory to another directory.

[root@server1 linux]# touch samplefile{1..5}.txt
[root@server1 linux]# mv samplefile1.txt /tmp/
[root@server1 linux]# ls /tmp/
samplefile1.txt                                                       
[root@server1 linux]# ls
Application  samplefile2.txt  samplefile3.txt  samplefile4.txt  samplefile5.txt
[root@server1 linux]#

As above example, When we move “samplefile1” file, the file from previous directory is removed.

Remember: When you move the file or directory to a different filesystem, the inode number will be different, refer below example.

Note: Moving file or directory the location of source file must be different with the files destination location, refer below error. When you move the file on same location.

[root@server1 linux]# mv samplefile3.txt samplefile3.txt 
mv: ‘samplefile3.txt’ and ‘samplefile3.txt’ are the same file
[root@server1 linux]#

As seen above examples, Similar command used for directory to move from current directory to another directory, It look like this.

[root@server1 linux]# ls
Application  samplefile3.txt  samplefile4.txt  samplefile5.txt
[root@server1 linux]# mv Application/ /tmp/
[root@server1 linux]# ll /tmp/
total 8
drwxr-xr-x. 4 root root   32 Sep 23 10:59 Application
-rw-r--r--. 1 root root    0 Oct 18 00:48 samplefile1.txt
-rw-r--r--. 1 root root    0 Oct 18 00:48 samplefile2.txt
[root@server1 linux]#

2) Renaming a file and directory.

As mentioned above, We also use “mv” command to rename file and directory.

# mv <OldFilename> <Newfilename>

[root@server1 linux]# ls
samplefile3.txt  samplefile4.txt  samplefile5.txt
[root@server1 linux]# mv samplefile3.txt Newfile3.txt
[root@server1 linux]# ls
Newfile3.txt  samplefile4.txt  samplefile5.txt
[root@server1 linux]#

You can also use absolute path, like;

 [root@server1 linux]# ls
 Newfile3.txt  samplefile4.txt  samplefile5.txt
 [root@server1 linux]# mv /root/linux/samplefile4.txt /root/linux/Newfile4.txt
 [root@server1 linux]# ls
 Newfile3.txt  Newfile4.txt  samplefile5.txt
 [root@server1 linux]#

As seen above example, Similar command used for director.

#mv <OldDirectoryName> <NewDirectoryName>

[root@server1 tmp]# ls
Application      samplefile2.txt       samplefile1.txt                                                  
[root@server1 tmp]# mv Application/ Network/
[root@server1 tmp]# ls
Network          samplefile2.txt     samplefile1.txt                                                     
[root@server1 tmp]#

When we renaming the file or directory. The inode number for file or directory is same even after remaining it.

[root@server1 tmp]# ls -ldi Network/
26240492 drwxr-xr-x. 2 root root 6 Oct 18 01:18 Network/
[root@server1 tmp]# mv Network/ Storage
[root@server1 tmp]# ls -ldi Storage/
26240492 drwxr-xr-x. 2 root root 6 Oct 18 01:18 Storage/
[root@server1 tmp]#

Note: When we  Renaming a file and directory the destination location must be the same with the source location.

3) Move Multiple files.

To move multiple file from source  or destination location, refer below syntax.

#mv <filename1> <filename2> <Destination location>

[root@server1 linux]# mv Newfile3.txt Newfile4.txt /tmp/
[root@server1 linux]# ll /tmp/
total 8
-rw-r--r--. 1 root root    0 Oct 18 00:48 Newfile3.txt
-rw-r--r--. 1 root root    0 Oct 18 00:48 Newfile4.txt
-rw-r--r--. 1 root root    0 Oct 18 00:48 samplefile1.txt
-rw-r--r--. 1 root root    0 Oct 18 00:48 samplefile2.txt
[root@server1 linux]#

move all *.txt files in current directory to another location.

[root@server1 linux]# mv *.txt /tmp/

4) Print informative messages.

Using “-v” option you can find out what happen during the moving files file or directory.

[root@server1 linux]# mv -v samplefile5.txt /tmp/
‘samplefile5.txt’ -> ‘/tmp/samplefile5.txt’
[root@server1 linux]#

5)  Prompt before overwrite.

By default When You are moving a file or directory into another location, and that file or directory is there already exist, then  “mv” command will overwrite it. Without any confirmation if the destination file or directory is exist.

To make any notification for overwriting file we using “-i” option. Let take example, We want to move “Newfile6.txt” file to /tmp location. Meantime, “Newfile6.txt” file already exist in /tmp location.

[root@server1 linux]# mv -vi Newfile6.txt /tmp/Newfile6.txt 
mv: overwrite ‘/tmp/Newfile6.txt’? y
‘Newfile6.txt’ -> ‘/tmp/Newfile6.txt’
[root@server1 linux]#

6) Updating file.

Using “-u” option, You can move only when the source file is newer than the destination file or when the destination file is missing.

[root@server1 linux]# ls /tmp/
samplefile1.txt  samplefile2.txt
[root@server1 linux]# ls
samplefile1.txt  samplefile2.txt  samplefile3.txt  samplefile4.txt  samplefile5.txt
[root@server1 linux]# mv -vu *.txt /tmp/
‘samplefile3.txt’ -> ‘/tmp/samplefile3.txt’
‘samplefile4.txt’ -> ‘/tmp/samplefile4.txt’
‘samplefile5.txt’ -> ‘/tmp/samplefile5.txt’
[root@server1 linux]# ls /tmp/
samplefile1.txt  samplefile2.txt  samplefile3.txt  samplefile4.txt  samplefile5.txt
[root@server1 linux]#

7) Override the usual backup suffix.

Using “–suffix=SUFFIX” or “-S” option, you can take the backup of the destination file before overwriting.

[root@server1 linux]# cat samplefile1.txt 
samplefile1
[root@server1 linux]# cat samplefile2.txt 
samplefile2
[root@server1 linux]# mv --suffix=.backup samplefile1.txt samplefile2.txt 
mv: overwrite ‘samplefile2.txt’? y
[root@server1 linux]# cat samplefile2.txt
samplefile1
[root@server1 linux]# cat samplefile2.txt.backup 
samplefile2
[root@server1 linux]#

8) Do not overwrite an existing file.

Using “-n” option, You will not allow to overwrite any existing files.

[root@server1 linux]# cat samplefile1.txt 
Hello Linux
[root@server1 linux]# cat /tmp/samplefile1.txt 
Linux is OpenSourceOS
[root@server1 linux]# mv -n samplefile1.txt /tmp/
[root@server1 linux]# cat /tmp/samplefile1.txt 
Linux is OpenSourceOS
[root@server1 linux]#

As above example, samplefile1.txt file not allow to overwrite to /tmp location by using “-n” option.

9) Make a backup when copying.

Using “-b” option, It will make a backup of destination file before it overwritten by the new one.

[root@server1 linux]# cat samplefile1.txt 
Hello Linux
[root@server1 linux]# cat /tmp/samplefile1.txt 
Linux is OpenSourceOS
[root@server1 linux]# mv -bv samplefile1.txt /tmp/
mv: overwrite ‘/tmp/samplefile1.txt’? y
‘samplefile1.txt’ -> ‘/tmp/samplefile1.txt’ (backup: ‘/tmp/samplefile1.txt~’)
[root@server1 linux]# ll /tmp/
total 8
-rw-r--r--. 1 root root 12 Oct 18 03:13 samplefile1.txt
-rw-r--r--. 1 root root 22 Oct 18 03:15 samplefile1.txt~
[root@server1 linux]# cat /tmp/samplefile1.txt~
Linux is OpenSourceOS
[root@server1 linux]# cat /tmp/samplefile1.txt
Hello Linux
[root@server1 linux]#

As show above example, We have a backup file of destination location, named samplefile1.txt~

Note: Using “-b” option will create a backup file and that marked by a tilde sign (~) at the end of the file.

10) Specify the destination Directory.

Using “–target-directory” option, You can specify the destination location, refer below example.

[root@server1 linux]# ls
samplefile2.txt
[root@server1 linux]# mv  --target-directory=/tmp samplefile2.txt 
[root@server1 linux]# ll /tmp/
total 12
-rw-r--r--. 1 root root 12 Oct 18 03:13 samplefile1.txt
-rw-r--r--. 1 root root 22 Oct 18 03:15 samplefile1.txt~
-rw-r--r--. 1 root root 22 Oct 18 03:36 samplefile2.txt
[root@server1 linux]#

Hope this post will help Linux/Unix beginners. Please share you feedback and Comments.!!!

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz