<>Linux command - mv command

  mv yes move Abbreviation of , Can be used to move files or rename files (move (rename) files),Linux in mv Commands are often used to back up files or directories .

<>1. grammar :

mv [ option ] Source file or directory Target file or directory

<>2. function :

   regard mv Different types of the second parameter in the command ( Target file or target directory ),mv Command to rename or move a file to a new directory . When the second parameter type is file ,mv Command complete file rename , here , There can only be one source file ( It can also be the source directory name ), It renames the given source file or directory to the given destination file name . When the second parameter is an existing directory name , Source file or directory parameters can have multiple ,mv The command moves the source files specified by each parameter to the target directory . When moving files across file systems ,mv Copy first , Then delete the original file , The link to the file will also be lost . Namely mv Removable files or directories , Or change the name of a file or directory .

<>3. parameter :
-b or --backup   To overwrite a file , Backup before overwriting . -f or --force
  If the target file or directory duplicates an existing file or directory , Directly overwrite the existing file or directory without asking .-i or --interactive   Ask the user before overwriting . -n or --no-
clobber   Do not overwrite existing files , If you specify -i,-f,-n Multiple in , Last effective only . -S< Additional suffix > or --suffix=< Additional suffix >   And -
b Parameters , You can specify the suffix to attach to the backup file , That is, replace the backup file suffix .-t or --target-directory=DIRECTORY
Move all the source files or directories specified by the parameters to the specified directory .-T or --no-target-directory Treat the target file as a normal file . -u or --update
  When moving or changing file names , If the target file already exists , And its file date is newer than the source file , Do not overwrite the target file .-v or --verbose   Display detailed information during execution . -V=< method > or --
version-control=< method >   And -b Parameters , Method of backup can be specified . --help Online help . View the command functions and parameters, etc .
<>4. Common examples :

Example 1 : files renaming

command :mv test.log test1.txt

   File test.log Rename to test1.txt.
[root@localhost test]# ll Total consumption 0 drwxr-xr-x. 2 root root 6 5 month 14 10:40 test11 -
rw-r--r--. 1 root root 0 5 month 14 10:40 test.log [root@localhost test]# mv test
test11/ test.log [root@localhost test]# mv test.log test1.txt [root@localhost
test]# ll Total consumption 0 drwxr-xr-x. 2 root root 6 5 month 14 10:40 test11 -rw-r--r--. 1 root
root 0 5 month 14 10:40 test1.txt
Example 2 : move file

command :mv test1.txt test11

   take test1.txt Move files to directory test11 in .
[root@localhost test]# ll Total consumption 0 drwxr-xr-x. 2 root root 6 5 month 14 10:40 test11 -
rw-r--r--. 1 root root 0 5 month 14 10:40 test1.txt [root@localhost test]# mv
test1.txt test11 [root@localhost test]# ll Total consumption 0 drwxr-xr-x. 2 root root 23 5 month
14 10:43 test11[root@localhost test]# cd test11/ [root@localhost test11]# ll
Total consumption 0-rw-r--r--. 1 root root 0 5 month 14 10:40 test1.txt
Example 3 : Move multiple files to the specified directory

command :mv log1.txt log2.txt log3.txt test22

   take log1.txt ,log2.txt, log3.txt Three files moved to test22 In the directory .
[root@localhost test]# ll Total consumption 0 -rw-r--r--. 1 root root 0 5 month 14 10:57 log1.txt
-rw-r--r--. 1 root root 0 5 month 14 10:57 log2.txt -rw-r--r--. 1 root root 0 5 month 14
10:58 log3.txt drwxr-xr-x. 2 root root 23 5 month 14 10:43 test11 drwxr-xr-x. 2 root
root 6 5 month 14 10:58 test22[root@localhost test]# mv log1.txt log2.txt log3.txt
test22 [root@localhost test]# ll Total consumption 0 drwxr-xr-x. 2 root root 23 5 month 14 10:43
test11 drwxr-xr-x. 2 root root 54 5 month 14 11:00 test22 [root@localhost test]# cd
test22/ [root@localhost test22]# ll Total consumption 0 -rw-r--r--. 1 root root 0 5 month 14 10:57
log1.txt -rw-r--r--. 1 root root 0 5 month 14 10:57 log2.txt -rw-r--r--. 1 root root
0 5 month 14 10:58 log3.txt
command :mv -t /usr/game/test/ log1.txt log2.txt log3.txt

   Move three files to test In the directory .
[root@localhost test22]# ll Total consumption 0 -rw-r--r--. 1 root root 0 5 month 14 10:57 log1.
txt-rw-r--r--. 1 root root 0 5 month 14 10:57 log2.txt -rw-r--r--. 1 root root 0 5 month
14 10:58 log3.txt [root@localhost test22]# mv -t /usr/games/test/ log1.txt
log2.txt log3.txt [root@localhost test22]# cd ../ [root@localhost test]# ll Total consumption
0-rw-r--r--. 1 root root 0 5 month 14 10:57 log1.txt -rw-r--r--. 1 root root 0 5 month 14
10:57 log2.txt -rw-r--r--. 1 root root 0 5 month 14 10:58 log3.txt drwxr-xr-x. 2
root root 23 5 month 14 10:43 test11 drwxr-xr-x. 2 root root 6 5 month 14 11:07 test22
Example 4 : File log1.txt Renamed log2.txt

command :mv -i log1.txt log2.txt

   If log2.txt Already exists , Then ask whether to overwrite .
[root@localhost test]# ll Total consumption 0 -rw-r--r--. 1 root root 0 5 month 14 10:57 log1.txt
-rw-r--r--. 1 root root 0 5 month 14 10:57 log2.txt -rw-r--r--. 1 root root 0 5 month 14
10:58 log3.txt drwxr-xr-x. 2 root root 23 5 month 14 10:43 test11 drwxr-xr-x. 2 root
root 6 5 month 14 11:07 test22[root@localhost test]# mv -i log1.txt log2.txt mv: Overwrite or not
'log2.txt'? y [root@localhost test]# ll Total consumption 0 -rw-r--r--. 1 root root 0 5 month 14
10:57 log2.txt -rw-r--r--. 1 root root 0 5 month 14 10:58 log3.txt drwxr-xr-x. 2
root root 23 5 month 14 10:43 test11 drwxr-xr-x. 2 root root 6 5 month 14 11:07 test22
command :mv -f log3.txt log2.txt

   even if log2.txt existence , It is also directly covered .-f This is a dangerous option , Be sure to keep your mind clear when using , In general, it is better not to add it .
[root@localhost test]# ll Total consumption 0 -rw-r--r--. 1 root root 0 5 month 14 10:57 log2.txt
-rw-r--r--. 1 root root 0 5 month 14 10:58 log3.txt drwxr-xr-x. 2 root root 23 5 month 14
10:43 test11 drwxr-xr-x. 2 root root 6 5 month 14 11:07 test22 [root@localhost test]
# mv -f log3.txt log2.txt [root@localhost test]# ll Total consumption 0 -rw-r--r--. 1 root
root 0 5 month 14 10:58 log2.txt drwxr-xr-x. 2 root root 23 5 month 14 10:43 test11 drwxr-
xr-x. 2 root root 6 5 month 14 11:07 test22
Example 5 : Movement of directories

command :mv test22 test11

   If directory test11 non-existent , Change directory test22 Renamed test11; otherwise , take test22 Move to test11 in .
[root@localhost test]# ll Total consumption 0 -rw-r--r--. 1 root root 0 5 month 14 10:58 log2.txt
drwxr-xr-x. 2 root root 23 5 month 14 10:43 test11 drwxr-xr-x. 2 root root 6 5 month 14
11:07 test22[root@localhost test]# mv test22 test11 [root@localhost test]# ll
Total consumption 0-rw-r--r--. 1 root root 0 5 month 14 10:58 log2.txt drwxr-xr-x. 3 root root 37
5 month 14 11:29 test11[root@localhost test]# cd test11/ [root@localhost test11]# ll
Total consumption 0-rw-r--r--. 1 root root 0 5 month 14 10:40 test1.txt drwxr-xr-x. 2 root root 6
5 month 14 11:07 test22
command :mv test11 test1
[root@localhost test]# ll Total consumption 0 -rw-r--r--. 1 root root 0 5 month 14 10:58 log2.txt
drwxr-xr-x. 3 root root 37 5 month 14 11:29 test11 [root@localhost test]# mv test11
test1 [root@localhost test]# ll Total consumption 0 -rw-r--r--. 1 root root 0 5 month 14 10:58 log2
.txt drwxr-xr-x. 3 root root 37 5 month 14 11:29 test1 [root@localhost test]# cd
test1/ [root@localhost test1]# ll Total consumption 0 -rw-r--r--. 1 root root 0 5 month 14 10:40
test1.txt drwxr-xr-x. 2 root root 6 5 month 14 11:07 test22
Example 5 : Move all files in the current folder to the upper level directory

command :mv * ../
[root@localhost games]# ll Total consumption 0 drwxr-xr-x. 3 root root 35 5 month 14 11:31 test
[root@localhost games]# cd test/ [root@localhost test]# ll Total consumption 0 -rw-r--r--. 1
root root 0 5 month 14 10:58 log2.txt drwxr-xr-x. 3 root root 37 5 month 14 11:29 test1
[root@localhost test]# mv * ../ [root@localhost test]# ll Total consumption 0 [root@localhost
test]# cd ../ [root@localhost games]# ll Total consumption 0 -rw-r--r--. 1 root root 0 5 month 14
10:58 log2.txt drwxr-xr-x. 2 root root 6 5 month 14 14:16 test drwxr-xr-x. 3 root
root 37 5 month 14 11:29 test1
Example 6 : Move files in one subdirectory of the current directory to another subdirectory

command :mv test1/*.txt test

   take test1 Below with txt End file moved to test inside .
[root@localhost games]# ll Total consumption 0 -rw-r--r--. 1 root root 0 5 month 14 10:58 log2.
txt drwxr-xr-x. 2 root root 6 5 month 14 14:16 test drwxr-xr-x. 3 root root 37 5 month 14
11:29 test1[root@localhost games]# cd test [root@localhost test]# ll Total consumption 0
[root@localhost test]# cd ../ [root@localhost games]# cd test1 [root@localhost
test1]# ll Total consumption 0 -rw-r--r--. 1 root root 0 5 month 14 10:40 test1.txt drwxr-xr-x. 2
root root 6 5 month 14 11:07 test22[root@localhost test1]# cd ../ [root@localhost
games]# mv test1/*.txt test [root@localhost games]# cd test [root@localhost
test]# ll Total consumption 0 -rw-r--r--. 1 root root 0 5 month 14 10:40 test1.txt [root@localhost
test]# cd ../ [root@localhost games]# cd test1 [root@localhost test1]# ll Total consumption 0
drwxr-xr-x. 2 root root 6 5 month 14 11:07 test22
Example 7 : Make a simple backup before the file is overwritten

command :mv log2.txt -b log1.txt

  -b Parameter not accepted ,mv Will read environment variables VERSION_CONTROL As a backup policy .

  -b This option specifies the action if the target file exists , There are four backup strategies :

  1.CONTROL=none or off : Do not back up .

   2.CONTROL=numbered or t: Backup of numeric numbers

  3.CONTROL=existing or nil: If there are numerically numbered backups , Continue numbering backup m+1…n:
implement mv A numbered file already exists before the operation log2.txt.1, Then executing again will generate log2.txt2
, Secondary analogy . If there are no previously numbered documents , Then use the simple backup described below .

   4.CONTROL=simple or never: Use simple backup : Simple backup before being overwritten , There can only be one simple backup , When overwritten again , Simple backups are also overwritten .
[root@localhost games]# ll Total consumption 0 -rw-r--r--. 1 root root 0 5 month 14 14:29 log1.txt
-rw-r--r--. 1 root root 0 5 month 14 10:58 log2.txt drwxr-xr-x. 2 root root 23 5 month 14
14:22 test drwxr-xr-x. 3 root root 20 5 month 14 14:22 test1 [root@localhost games]#
mv log2.txt -b log1.txt mv: Overwrite or not 'log1.txt'? y [root@localhost games]# ll Total consumption 0 -
rw-r--r--. 1 root root 0 5 month 14 10:58 log1.txt -rw-r--r--. 1 root root 0 5 month 14
14:29 log1.txt~ drwxr-xr-x. 2 root root 23 5 month 14 14:22 test drwxr-xr-x. 3 root
root 20 5 month 14 14:22 test1

Technology