mv Command in Linux with examples
mv Command
mv command is the basic command in Linux. It permanently moves the files or directories from one location to another and deletes the files in the source.
Basic Syntax
mv <option> <source> <destination>
Here, the file in the source path is removed, after copying to destination path.
To move a file
It moves a single file from one location to another using mv command.
[root@linuxhelp test]# ls –l -rw-r--r-- 1 root root 0 Feb 11 06:09 test1.txt -rw-r--r-- 1 root root 0 Feb 11 06:09 test2.txt [root@linuxhelp test]# mv test1.txt /home/user1/Pictures/ [root@linuxhelp test]# ls –l total 0 -rw-r--r-- 1 root root 0 Feb 11 06:09 test2.txt [root@linuxhelp test]# ls -l /home/user1/Pictures/ -rw-r--r-- 1 root root 0 Feb 11 06:09 test1.txt
Here, the 'test1.txt' file is deleted from 'test' directory.
To move multiple files
More than one file can be moved at the same time using mv command.
[root@linuxhelp test]# ls -l -rw-r--r-- 1 root root 0 Feb 11 06:11 test1.txt -rw-r--r-- 1 root root 0 Feb 11 06:09 test2.txt -rw-r--r-- 1 root root 0 Feb 11 06:11 test3.txt [root@linuxhelp test]# mv test1.txt test2.txt /home/user1/Pictures/ [root@linuxhelp test]# ls -l /home/user1/Pictures/ total 0 -rw-r--r-- 1 root root 0 Feb 11 06:11 test1.txt -rw-r--r-- 1 root root 0 Feb 11 06:09 test2.txt
To move directory
To move a directory from one location to another location there is no need of any options as like in ‘;cp’; command.
[root@linuxhelp test]# ls -l Total 8 drwxr-xr-x 2 root root 4096 Feb 11 06:13 temp 1 drwxr-xr-x 2 root root 4096 Feb 11 06:13 temp2 [root@linuxhelp test]# mv temp1 /home/user1/Pictures/ [root@linuxhelp test]# ls -l /home/user1/Pictures/ total 4 drwxr-xr-x 2 root root 4096 Feb 11 06:13 temp 1
To rename files or directories
mv command is not only used for moving files or directories. It can be used to rename both files and directories.
Syntax:
mv <existing-file> <new-file-name>
[root@linuxhelp test]# ls -l total 12 drwxr-xr-x 2 root root 4096 Feb 11 06:15 temp 1 drwxr-xr-x 2 root root 4096 Feb 11 06:13 temp2 drwxr-xr-x 2 root root 4096 Feb 11 06:16 tempp [root@linuxhelp test]# mv tempp temp3 [root@linuxhelp test]# ls -l total 12 drwxr-xr-x 2 root root 4096 Feb 11 06:15 temp 1 drwxr-xr-x 2 root root 4096 Feb 11 06:13 temp2 drwxr-xr-x 2 root root 4096 Feb 11 06:16 temp3
To track the process
The process can be traced by using '-v' option.
[root@linuxhelp test]# ls -l total 0 -rw-r--r-- 1 root root 0 Feb 11 06:20 test1.txt -rw-r--r-- 1 root root 0 Feb 11 06:20 test2.txt -rw-r--r-- 1 root root 0 Feb 11 06:20 test3.txt -rw-r--r-- 1 root root 0 Feb 11 06:20 test4.txt [root@linuxhelp test]# mv -v *.txt /home/user1/Pictures/ `test1.txt' -> `/home/user1/Pictures/test1.txt' `test2.txt' -> `/home/user1/Pictures/test2.txt' `test3.txt' -> `/home/user1/Pictures/test3.txt' `test4.txt' -> `/home/user1/Pictures/test4.txt [root@linuxhelp test]# ls -l total 0 [root@linuxhelp test]# ls -l /home/user1/Pictures/ total 0 -rw-r--r-- 1 root root 0 Feb 11 06:20 test1.txt -rw-r--r-- 1 root root 0 Feb 11 06:20 test2.txt -rw-r--r-- 1 root root 0 Feb 11 06:20 test3.txt -rw-r--r-- 1 root root 0 Feb 11 06:20 test4.txt
Interactive mode
In this mode, if the destination directory contains the same contents in source or to delete the contents, it will ask the confirmation. User have to answer the question either by pressing 'y' (yes) or 'n' (no).
[root@linuxhelp test]# ls -l -rw-r--r-- 1 root root 0 Feb 11 06:28 test.txt [root@linuxhelp test]# ls -l /home/user1/Pictures/ -rw-r--r-- 1 root root 0 Feb 11 06:28 test.txt [root@linuxhelp test]# mv -i test.txt /home/user1/Pictures/ mv: overwrite `/home/user1/Pictures/test.txt'? Y [root@linuxhelp test]# ls -l /home/user1/Pictures/ -rw-r--r-- 1 root root 0 Feb 11 19:24 test.txt [root@linuxhelp test]# cd /home/user1/Pictures/ [root@linuxhelp Pictures]# ll total 4 -rw-r--r-- 1 root root 0 Feb 11 06:28 test.txt [root@linuxhelp test]# rm test.txt rm: remove regular empty file ‘;test.txt’; ? y [root@linuxhelp Pictures]# ls –l total 0
To move only if source is modified
To move the files which is updated or edited recently.
'-u' option is to be used.
[root@linuxhelp test]# ls -l total 0 -rw-r--r-- 1 root root 0 Feb 11 06:36 file1.txt -rw-r--r-- 1 root root 0 Feb 11 06:33 file2.txt -rw-r--r-- 1 root root 0 Feb 11 06:33 file3.txt [root@linuxhelp test]# ls -l /home/user1/Pictures/ total 0 -rw-r--r-- 1 root root 0 Feb 11 06:36 file1.txt -rw-r--r-- 1 root root 0 Feb 11 06:34 file2.txt -rw-r--r-- 1 root root 0 Feb 11 06:34 file3.txt [root@linuxhelp test]# vim file1.txt [root@linuxhelp test]# mv -vu *.txt /home/user1/Pictures/ `file1.txt' -> `/home/user1/Pictures/file1.txt' [root@linuxhelp test]# ls -l /home/user1/Pictures/ total 4 -rw-r--r-- 1 root root 12 Feb 11 06:37 file1.txt -rw-r--r-- 1 root root 0 Feb 11 06:34 file2.txt -rw-r--r-- 1 root root 0 Feb 11 06:34 file3.txt
To create backup of moving file
The file which is to be moved will create the backup of the same file in the specified location with tilde (~) symbol at the last.
[root@linuxhelp test]# mv -bv *.txt /home/user1/Pictures/ mv: overwrite ‘;/home/user1/Pictures/file1.txt’; ? y `file1.txt' -> `/home/user1/Pictures/file1.txt' (backup: `/home/user1/Pictures/file1.txt~') mv: overwrite ‘;/home/user1/Pictures/file2.txt’; ? y `file2.txt' -> `/home/user1/Pictures/file2.txt' (backup: `/home/user1/Pictures/file2.txt~') mv: overwrite ‘;/home/user1/Pictures/file3.txt’; ? y `file3.txt' -> `/home/user1/Pictures/file3.txt' (backup: `/home/user1/Pictures/file3.txt~') [root@linuxhelp test]# ls -l /home/user1/Pictures/ total 0 -rw-r--r-- 1 root root 0 Feb 11 06:45 file1.txt -rw-r--r-- 1 root root 0 Feb 11 17:33 file1.txt~ -rw-r--r-- 1 root root 0 Feb 11 06:45 file2.txt -rw-r--r-- 1 root root 0 Feb 11 17:33 file2.txt~ -rw-r--r-- 1 root root 0 Feb 11 06:45 file3.txt -rw-r--r-- 1 root root 0 Feb 11 17:33 file3.txt~
Comments ( 0 )
No comments available