A Linux command a day (7): mv command

post thumb
Devops
by Admin/ on 19 Sep 2021

A Linux command a day (7): mv command


mv command is the abbreviation of move, which can be used to move files or rename files (move (rename) files), and is a common command under Linux system, often used to backup files or directories.

1. Command format.

mv [option ] source file or directory destination file or directory

2. Command function.

Depending on the type of the second argument in the mv command (whether it is a target file or a target directory), the mv command renames the file or moves it to a new directory. When the second argument type is file, the mv command completes file renaming, when there can be only one source file (or source directory name), and it renames the given source file or directory to the given target file name. When the second argument is the name of an existing directory, the source file or directory argument can have more than one, and the mv command moves all the source files specified by each argument to the target directory. When moving files across file systems, mv will first copy and then delete the original file, and the link to the file will be lost.

3. Command parameters.

-b : If you need to overwrite a file, backup it before overwriting.

-f :force to force, if the target file already exists, it will not ask and overwrite directly.

-i :If the target file (destination) already exists, it will ask whether to overwrite or not!

-u : update if the destination file already exists and the source is relatively new

-t:-target-directory=DIRECTORY move all SOURCE arguments into DIRECTORY, that is, specify the target directory of mv, this option is suitable for moving multiple source files to one directory, the target directory is in front, the source files are behind.

4. Examples of commands.

Example 1: File renaming

Command:

mv test.log test1.txt  

output:

[root@localhost test]# 11  
Total 20  
drwxr-xr-x 6 root root 4096 10-27 01:58 scf  
drwxrwxrwx 2 root root 4096 10-25 17:46 test3  
drwxr-xr-x 2 root root 4096 10-25 17:56 test4  
drwxr-xr-x 3 root root 4096 10-25 17:56 test5  
-rw-r--r-- 1 root root 16 10-28 06:04 test.log  
[root@localhost test]# mv test.log test1.txt  
[root@localhost test]# 11  
Total 20  
drwxr-xr-x 6 root root 4096 10-27 01:58 scf  
-rw-r--r-- 1 root root 16 10-28 06:04 test1.txt  
drwxrwxrwx 2 root root 4096 10-25 17:46 test3  
drwxr-xr-x 2 root root 4096 10-25 17:56 test4  
drwxr-xr-x 3 root root 4096 10-25 17:56 test5  

Description:

Rename the file test.log to test1.txt

Example 2: Moving files

Commands:

mv test1.txt test3  

output:

[root@localhost test]# 11  
Total 20  
drwxr-xr-x 6 root root 4096 10-27 01:58 scf  
-rw-r--r-- 1 root root 29 10-28 06:05 test1.txt  
drwxrwxrwx 2 root root 4096 10-25 17:46 test3  
drwxr-xr-x 2 root root root 4096 10-25 17:56 test4  
drwxr-xr-x 3 root root 4096 10-25 17:56 test5  
[root@localhost test]# mv test1.txt test3  
[root@localhost test]# 11  
Total 16  
drwxr-xr-x 6 root root 4096 10-27 01:58 scf  
drwxrwxrwx 2 root root 4096 10-28 06:09 test3  
drwxr-xr-x 2 root root 4096 10-25 17:56 test4  
drwxr-xr-x 3 root root 4096 10-25 17:56 test5  
[root@localhost test]# cd test3  
[root@localhost test3]# 11  
Total 4  
-rw-r--r-- 1 root root 29 10-28 06:05 test1.txt  
[root@localhost test3]#  

Description.

Move the test1.txt file to the directory test3

Example 3: Move the files log1.txt,log2.txt,log3.txt to the directory test3.

Command:

mv log1.txt log2.txt log3.txt test3

mv -t /opt/soft/test/test4/ log1.txt log2.txt log3.txt

output:

[root@localhost test]# 11  
Total 28  
-rw-r--r-- 1 root root 8 10-28 06:15 log1.txt  
-rw-r--r-- 1 root root 12 10-28 06:15 log2.txt  
-rw-r--r-- 1 root root 13 10-28 06:16 log3.txt  
drwxrwxrwx 2 root root 4096 10-28 06:09 test3  
[root@localhost test]# mv log1.txt log2.txt log3.txt test3  
[root@localhost test]# 11

Total 16  
drwxrwxrwx 2 root root 4096 10-28 06:18 test3  
[root@localhost test]# cd test3/  
[root@localhost test3]# 11

Total 16  
-rw-r--r-- 1 root root 8 10-28 06:15 log1.txt  
-rw-r--r-- 1 root root 12 10-28 06:15 log2.txt  
-rw-r--r-- 1 root root 13 10-28 06:16 log3.txt  
-rw-r--r-- 1 root root 29 10-28 06:05 test1.txt  
[root@localhost test3]#  
[root@localhost test3]# 11

Total 20  
-rw-r--r-- 1 root root 8 10-28 06:15 log1.txt  
-rw-r--r-- 1 root root 12 10-28 06:15 log2.txt  
-rw-r--r-- 1 root root 13 10-28 06:16 log3.txt  
drwxr-xr-x 2 root root 4096 10-28 06:21 logs  
-rw-r--r-- 1 root root 29 10-28 06:05 test1.txt  
[root@localhost test3]# mv -t /opt/soft/test/test4/ log1.txt log2.txt log3.txt   
[root@localhost test3]# cd .  
[root@localhost test]# cd test4/  
[root@localhost test4]# 11

Total 12  
-rw-r--r-- 1 root root 8 10-28 06:15 log1.txt  
-rw-r--r-- 1 root root 12 10-28 06:15 log2.txt  
-rw-r--r-- 1 root root 13 10-28 06:16 log3.txt  
[root@localhost test4]#

Description:

The command mv log1.txt log2.txt log3.txt test3 moves log1.txt, log2.txt, and log3.txt to the test3 directory, and the command mv -t /opt/soft/test/test4/ log1.txt log2.txt log3.txt moves the three files to the test3 directory. command to move three more files to the test4 directory.

Example 4: Rename the file file1 to file2, and ask whether to overwrite it if file2 already exists

Command: mv -i log1.txt log2.txt

Output.

[root@localhost test4]# 11  
Total 12  
-rw-r--r-- 1 root root 8 10-28 06:15 log1.txt  
-rw-r--r-- 1 root root 12 10-28 06:15 log2.txt  
-rw-r--r-- 1 root root 13 10-28 06:16 log3.txt  
[root@localhost test4]# cat log1.txt   
odfdfs  
[root@localhost test4]# cat log2.txt   
ererwerwer  
[root@localhost test4]# mv -i log1.txt log2.txt   
mv: does it overwrite "log2.txt"? y  
[root@localhost test4]# cat log2.txt   
odfdfs  
[root@localhost test4]#  

Example 5: rename the file file1 to file2, even if file2 exists, it is directly overwritten.

Command:

mv -f log3.txt log2.txt

output:

[root@localhost test4]# 11  
Total 8  
-rw-r--r-- 1 root root 8 10-28 06:15 log2.txt  
-rw-r--r-- 1 root root 13 10-28 06:16 log3.txt  
[root@localhost test4]# cat log2.txt   
odfdfs  
[root@localhost test4]# cat log3  
cat: log3: there is no such file or directory  
[root@localhost test4]# 11  
Total 8  
-rw-r--r-- 1 root root 8 10-28 06:15 log2.txt  
-rw-r--r-- 1 root root 13 10-28 06:16 log3.txt  
[root@localhost test4]# cat log2.txt   
odfdfs  
[root@localhost test4]# cat log3.txt   
dfosdfsdfdss  
[root@localhost test4]# mv -f log3.txt log2.txt   
[root@localhost test4]# cat log2.txt   
dfosdfsdfdss  
[root@localhost test4]# 11  
Total 4  
-rw-r--r-- 1 root root 13 10-28 06:16 log2.txt  
[root@localhost test4]#  

Description.

The content of log3.txt directly overwrites the content of log2.txt. -f This is a dangerous option, so be sure to keep your head clear when using it, and it is generally best not to add it.

Example 6: Moving directories

Commands:

mv dir1 dir2

Output:

[root@localhost test4]# 11  
-rw-r--r-- 1 root root 13 10-28 06:16 log2.txt  
[root@localhost test4]# 11  
-rw-r--r-- 1 root root 13 10-28 06:16 log2.txt  
[root@localhost test4]# cd .  
[root@localhost test]# 11  
drwxr-xr-x 6 root root 4096 10-27 01:58 scf  
drwxrwxrwx 3 root root 4096 10-28 06:24 test3  
drwxr-xr-x 2 root root 4096 10-28 06:48 test4  
drwxr-xr-x 3 root root 4096 10-25 17:56 test5  
[root@localhost test]# cd test3  
[root@localhost test3]# 11  
drwxr-xr-x 2 root root 4096 10-28 06:21 logs  
-rw-r--r-- 1 root root 29 10-28 06:05 test1.txt  
[root@localhost test3]# cd .  
[root@localhost test]# mv test4 test3  
[root@localhost test]# 11  
drwxr-xr-x 6 root root 4096 10-27 01:58 scf  
drwxrwxrwx 4 root root 4096 10-28 06:54 test3  
drwxr-xr-x 3 root root 4096 10-25 17:56 test5  
[root@localhost test]# cd test3/  
[root@localhost test3]# 11  
drwxr-xr-x 2 root root 4096 10-28 06:21 logs  
-rw-r--r-- 1 root root 29 10-28 06:05 test1.txt  
drwxr-xr-x 2 root root 4096 10-28 06:48 test4  
[root@localhost test3]#  

Description:

If the directory dir2 does not exist, rename the directory dir1 to dir2; otherwise, move dir1 to dir2.

Example 7: Move all files in the current folder to a higher directory

Command:

mv * ... /

** Output:**

[root@localhost test4]# 11  
-rw-r--r-- 1 root root 25 10-28 07:02 log1.txt  
-rw-r--r-- 1 root root 13 10-28 06:16 log2.txt  
[root@localhost test4]# mv * . /  
[root@localhost test4]# 11  
[root@localhost test4]# cd ...  
[root@localhost test3]# 11  
-rw-r--r-- 1 root root 25 10-28 07:02 log1.txt  
-rw-r--r-- 1 root root 13 10-28 06:16 log2.txt  
drwxr-xr-x 2 root root 4096 10-28 06:21 logs  
-rw-r--r-- 1 root root 29 10-28 06:05 test1.txt  
drwxr-xr-x 2 root root 4096 10-28 07:02 test4  

Example 8: Move a file from one subdirectory of the current directory to another subdirectory

Command:

mv test3/*.txt test5

Output:

[root@localhost test]# 11  
drwxr-xr-x 6 root root 4096 10-27 01:58 scf  
drwxrwxrwx 4 root root 4096 10-28 07:02 test3  
drwxr-xr-x 3 root root 4096 10-25 17:56 test5  
[root@localhost test]# cd test3  
[root@localhost test3]# 11  
-rw-r--r-- 1 root root 25 10-28 07:02 log1.txt  
-rw-r--r-- 1 root root 13 10-28 06:16 log2.txt  
drwxr-xr-x 2 root root 4096 10-28 06:21 logs  
-rw-r--r-- 1 root root 29 10-28 06:05 test1.txt  
drwxr-xr-x 2 root root 4096 10-28 07:02 test4  
[root@localhost test3]# cd .  
[root@localhost test]# mv test3/*.txt test5  
[root@localhost test]# cd test5  
[root@localhost test5]# 11  
-rw-r--r-- 1 root root 25 10-28 07:02 log1.txt  
-rw-r--r-- 1 root root 13 10-28 06:16 log2.txt  
-rw-r--r-- 1 root root 29 10-28 06:05 test1.txt  
drwxr-xr-x 2 root root 4096 10-25 17:56 test5-1  
[root@localhost test5]# cd .  
[root@localhost test]# cd test3/  
[root@localhost test3]# 11  
drwxr-xr-x 2 root root 4096 10-28 06:21 logs  
drwxr-xr-x 2 root root 4096 10-28 07:02 test4  
[root@localhost test3]#  

Example 9: Make a simple backup before the file is overwritten, preceded by the parameter -b

Command:

mv log1.txt -b log2.txt

output:

[root@localhost test5]# 11  
-rw-r--r-- 1 root root 25 10-28 07:02 log1.txt  
-rw-r--r-- 1 root root 13 10-28 06:16 log2.txt  
-rw-r--r-- 1 root root 29 10-28 06:05 test1.txt  
drwxr-xr-x 2 root root 4096 10-25 17:56 test5-1  
[root@localhost test5]# mv log1.txt -b log2.txt  
mv: does it overwrite "log2.txt"? y  
[root@localhost test5]# 11  
-rw-r--r-- 1 root root 25 10-28 07:02 log2.txt  
-rw-r--r-- 1 root root 13 10-28 06:16 log2.txt~  
-rw-r--r-- 1 root root 29 10-28 06:05 test1.txt  
drwxr-xr-x 2 root root 4096 10-25 17:56 test5-1  
[root@localhost test5]#  

Description:

-b does not accept arguments, mv will go and read the environment variable VERSION_CONTROL for the backup policy.

--backup This option specifies the action to take if the target file exists. There are four backup policies.

  1. CONTROL=none or off : no backup

  2. CONTROL=numbered or t: numbered backup

  3. CONTROL=existing or nil: If a numbered backup exists, continue with the numbered backup m+1…. .n.

If a numbered file log2.txt.~1~ existed before the mv operation, then executing it again will produce log2.txt~2~, and so on. If there is no numbered file before, use the simple backup described below.

  1. CONTROL=simple or never: Use simple backup: A simple backup was made before it was overwritten. There can be only one copy of the simple backup, and when it is overwritten again, the simple backup will also be overwritten.

Reference:

www.cnblogs.com/peida/archive/2012/10/26/2743022.html

comments powered by Disqus