loader

mv Command

To move files or directories from a source to a destination in Linux, use the “mv” command. This command has got two functions. First, moving files or directories, and second, renaming a file or directory. It can also move multiple files or directories. “mv” command, as we explained in the “cp” command, works silently without any prompt unless we put an option.

Syntax

mv [option] source destination

Examples:

mv source destination: it moves the source file or directory to the destination. If the source and destination paths were the same, then the “mv” command will rename the file or directory.
-i: If the destination file exists, the mv command overwrites it. So to make the mv command ask first, before overwriting a file, use this option.
-n: this option prevents the mv command from overwriting the file if the destination file exists.
-u: to move only newer files than the destination file, use this option. The mv command won’t move if the source file is not newer than a file with the same name in the destination.

Practice 1

1-Create “foo” and “bar” directories in the current working directory and then create “first.txt” in the “foo” with ” I LOVE LINUX ” content.

2-Rename “first.txt” to “second.txt.”

3-Create “barNew” file with “move files between directories” content in the “bar” directory and then move it to the “foo” directory with the new name “fooNew.”

4-Rename the “fooNew” to the “second.txt.” Check not to overwrite it.
Note: Because the “Second.txt” already exists, renaming “fooNew” to “second.txt” will overwrite the “second.txt” content. So press “n” to stop the execution.

5-Do the previous practice, but consider making a condition that prevents overwriting the destination file if it exists.

6-In the “bar” directory, create the “fooNew” file with ” mv -u test” content. So there is a “fooNew” file in both the “bar” and the “foo” directory. First, compare the timestamp of each “fooNew” file. Then, move the /foo/fooNew file to the “bar” directory if it is newer than the /bar/fooNew file.

7- move the /bar/fooNew file to the “foo” directory if it is newer than the /foo/fooNew file.

				
					*******************Answers**************************
*********************P1*****************************
[root@Lpic1-CentOs9 ~]# mkdir foo bar
[root@Lpic1-CentOs9 ~]# cd ./foo/
[root@Lpic1-CentOs9 foo]# echo " I Love Linux " > first.txt
[root@Lpic1-CentOs9 foo]# tree
.
└── first.txt
*********************P2*****************************
[root@Lpic1-CentOs9 ~]# mv ./foo/first.txt ./foo/second.txt
[root@Lpic1-CentOs9 ~]# tree
.
├── bar
├── foo
│   └── second.txt
[root@Lpic1-CentOs9 ~]# cat ./foo/second.txt
 I Love Linux
*********************P3*****************************
[root@Lpic1-CentOs9 ~]# cd ./bar/
[root@Lpic1-CentOs9 bar]# echo "move files between directories" > barNew
[root@Lpic1-CentOs9 bar]# tree
.
└── barNew
[root@Lpic1-CentOs9 bar]# mv barNew /root/foo/fooNew
[root@Lpic1-CentOs9 bar]# cd ~
[root@Lpic1-CentOs9 ~]# tree
.
├── bar
├── foo
│   ├── fooNew
│   └── second.txt
*********************P4*****************************
[root@Lpic1-CentOs9 ~]# mv -i  ./foo/fooNew ./foo/second.txt
mv: overwrite './foo/second.txt'? n
[root@Lpic1-CentOs9 ~]# tree ./foo/
./foo/
├── fooNew
└── second.txt
*********************P5*****************************
[root@Lpic1-CentOs9 ~]# mv -n  ./foo/fooNew ./foo/second.txt
[root@Lpic1-CentOs9 ~]# tree ./foo/
./foo/
├── fooNew
└── second.txt
*********************P6*****************************
[root@Lpic1-CentOs9 ~]# echo "mv -u test" > ./bar/fooNew
[root@Lpic1-CentOs9 ~]# ls -lah ./bar/fooNew
-rw-r--r--. 1 root root 11 Jun  4 05:57 ./bar/fooNew
[root@Lpic1-CentOs9 ~]# ls -lah ./foo/fooNew
-rw-r--r--. 1 root root 31 Jun  4 02:32 ./foo/fooNew
[root@Lpic1-CentOs9 ~]# mv -u ./foo/fooNew ./bar/fooNew
[root@Lpic1-CentOs9 ~]# cat ./bar/fooNew
mv -u test
*********************P7*****************************
[root@Lpic1-CentOs9 ~]# mv -u ./bar/fooNew ./foo/fooNew
mv: overwrite './foo/fooNew'? y
[root@Lpic1-CentOs9 ~]# cat foo/fooNew
 mv -u test
[root@Lpic1-CentOs9 ~]# tree ./bar
./bar
0 directories, 0 files

				

Note:As practice six shows, the ” mv -u ” command didn’t move the “/foo/fooNew” file because it is not newer than the “/bar/fooNew.” So we reversed the practice to move “/bar/fooNew” to the “foo” directory. The “-u” option prompts before overwriting. Then, it moves the file from the “bar” directory and overwrites the destination file.

Leave a Reply

Your email address will not be published. Required fields are marked *