One linux Command every day (7): mv command link: One linux Command every day (1): ls command http://www.bkjia.com/ OS /201210/163049.html#linuxlinuxcommand every day (2): cd command serial (3 ): pwd command Export (4): mkdir command http://www.bkjia.com/ OS /201210/1620.3.html#a linuxcommand every day (5): rm command Export (6): rmdir command http://www.bkjia.com/ OS /201210/164017.ht The ml mv command is short for moving files. It can be used to move files or rename files. It is a common command in Linux and is often used to back up files or directories. 1. command Format: mv [Option] source file or directory target file or directory 2. command function: depending on the type of the second parameter in the mv command (target file or directory), the mv command renames the file or moves it to a new directory. When the second parameter type is file, the mv command renames the file. At this time, only one source file (or the source directory name) can be used ), it renames the given source file or directory to the given target file name. When the second parameter is an existing directory name, there may be multiple source files or directory parameters. The mv command moves the source files specified by each parameter to the target directory. When a file is moved across file systems, the mv copies the file first, and then deletes the original file. The link to the file will also be lost. Www.2cto.com 3. Command Parameter:-B: If you want to overwrite the file, back up the file before overwriting. -F: force indicates that if the target file already exists, the file will not be directly overwritten.-I: If the destination file already exists, the file will be overwritten! -U: update is only available if the target file already exists and the source file is updated. command instance: instance 1: file renaming command: mv test. log test1.txt output: [root @ localhost test] # ll total 20drwxr-xr-x 6 root 4096 10-27 scf drwxrwxrwx 2 root 4096 10-25 test3 drwxr-xr-x 2 root 4096 10- 25 test4 drwxr-xr-x 3 root 4096 10-25 test5-rw-r -- 1 root 16 10-28 test. log [root @ localhost test] # mv test. log test1.txt [root @ loca Lhost test] # ll total 20drwxr-xr-x 6 root 4096 10-27 0:58 scf-rw-r -- 1 root 16 10-28 06:04 test1.txt drwxrwxrwx 2 root 4096 10-25 test3 drwxr-xr-x 2 root 4096 10-25 test4 drwxr-xr-x 3 root 4096 10-25 test5 description: rename file test.logas test1.txt instance 2: Move file command: mv test1.txt test3 output: [root @ localhost test] # ll total 20drwxr-xr-x 6 root 4096 10-27 0scf-rw-r -- 1 root 29 10-28 test1.txt drwxrwxrwx 2 root 4096 10-25 test3 drwxr-xr-x 2 root 4096 10-25 test4 drwxr-xr-x 3 root root 4096 10-25 test5 [root @ localhost test] # mv test1.txt test3 [root @ localhost test] # ll total 16drwxr-xr-x 6 root 4096 10-27 058 scf drwxrwxrwx 2 root 4096 10-28 test3 drwxr-xr-x 2 root 4096 10-25 test4 drwxr-xr-x 3 root Root 4096 10-25 test5 [root @ localhost test] # cd test3 [root @ localhost test3] # ll total 4-rw-r -- 1 root 29 10-28 06:05 test1.txt [root @ localhost test3] # description: move the test1.txt file to the test3 directory. Example 3: Move log1.txt, log2.txt, and log3.txt to the test3 directory. 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] # ll total 28-rw-r -- 1 root 8 10-28 06:15 log1.txt-rw-r -- 1 root 12 10-28 log2.txt-rw-r -- 1 root 13 10-28 log3.txt drwxrwxrwx 2 root 4096 10-28 test3 [root @ localhost test] # mv log1.txt log2.txt log3.txt test3 [root @ localhost test] # ll total 16 drwxrwxrwx 2 root 4096 10-28 test3 [root @ localhost test] # cd test3/[root @ localhost test3] # ll total 16-rw-r -- 1 root 8 10-28 log1.txt-rw-r -- 1 root 12 10-28 15 log2.txt-rw-r -- 1 root 13 10-28 log3.txt -rw-r -- 1 root 29 10-28 06:05 test1.txt [root @ localhost test3] # [root @ localhost test3] # ll total 20-rw-r -- 1 root 8 10-28 log1. Txt-rw-r -- 1 root 12 10-28 log2.txt-rw-r -- 1 root 13 10-28 log3.txt drwxr-xr-x 2 root root 4096 10-28 21 logs-rw-r -- 1 root 29 10-28 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] # ll Total 12-rw-r -- 1 root 8 10-28 l Og1.txt-rw-r -- 1 root 12 10-28 log2.txt-rw-r -- 1 root 13 10-28 log3.txt [root @ localhost test4] # note: mv log1.txt log2.txt log3.txt test3 command to move log1.txt, log2.txt, and log3.txt to the test3 directory, the mv-t/opt/soft/test/test4/log1.txt log2.txt log3.txt command moves the three files to the test4 directory to go to www.2cto.com instance 4: Change the file file1 to file2, if file2 already exists, ask whether to overwrite the command: mv-I log1.txt log2.txt output: [root @ localhost test4] # Ll Total 12-rw-r -- 1 root 8 10-28 log1.txt-rw-r -- 1 root 12 10-28 log2.txt-rw-r -- r -- 1 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: overwrite “log2.txt "? Y [root @ localhost test4] # cat log2.txt odfdfs [root @ localhost test4] # instance 5: Rename file file1 to file2, even if file2 exists, it is overwritten directly. Command: mv-f log3.txt log2.txt output: [root @ localhost test4] # ll total 8-rw-r -- 1 root 8 10-28 06:15 log2.txt-rw-r -- 1 root 13 10-28 06:16 log3.txt [root @ localhost test4] # cat log2.txt odfdfs [root @ localhost test4] # cat log3 cat: log3: no such file or directory [root @ localhost test4] # ll total 8-rw-r -- 1 root 8 10-28 log2.txt-rw-r -- 1 root root 13 10-28 06:16 log3.txt [root @ localhost test4] # c At 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 dig [root @ localhost test4]] # ll total 4-rw-r -- 1 root 13 10-28 log2.txt [root @ localhost test4] # description: the content of log3.txtdirectly overwrites log2.txt.-f is a dangerous option. You must keep your mind clear when using it. In general, it is best not to add it. Example 6 of www.2cto.com: Command for moving directories: mv dir1 dir2 output: [root @ localhost test4] # ll-rw-r -- 1 root 13 10-28 06:16 log2.txt [root @ localhost test4] # ll-rw-r -- 1 root 13 10-28 06:16 log2.txt [root @ localhost test4] # cd .. [root @ localhost test] # ll drwxr-xr-x 6 root 4096 10-27 scf drwxrwxrwx 3 root 4096 10-28 test3 drwxr-xr-x 2 root 4096 10-28 06:48 test4 drwxr-xr-x 3 root ro Ot 4096 10-25 test5 [root @ localhost test] # cd test3 [root @ localhost test3] # ll drwxr-xr-x 2 root 4096 10-28 logs-rw -r -- 1 root 29 10-28 06:05 test1.txt [root @ localhost test3] # cd .. [root @ localhost test] # mv test4 test3 [root @ localhost test] # ll drwxr-xr-x 6 root 4096 10-27 0:58 scf drwxrwxrwx 4 root 4096 10-28 test3 drwxr-xr-x 3 root 4096 10-25 tes T5 [root @ localhost test] # cd test3/[root @ localhost test3] # ll drwxr-xr-x 2 root 4096 10-28 06:21 logs-rw-r -- 1 root 29 10-28 test1.txt drwxr-xr-x 2 root 4096 10-28 test4 [root @ localhost test3] # Note: if the dir2 directory does not exist, rename dir1 to dir2; otherwise, move dir1 to dir2. Instance 7: Move all files in the current folder to the upper-level directory. Command: mv *.. /output: [root @ localhost test4] # ll-rw-r -- 1 root 25 10-28 07:02 log1.txt-rw-r -- 1 root 13 10-28 06:16 log2.txt [root @ localhost test4] # mv *.. /[root @ localhost test4] # ll [root @ localhost test4] # cd .. [root @ localhost test3] # ll-rw-r -- 1 root 25 10-28 07:02 log1.txt-rw-r -- 1 root 13 10-28 06:16 log2.txt drwxr-xr-x 2 root 4096 10-2 8 logs-rw-r -- 1 root 29 10-28 05 test1.txt drwxr-xr-x 2 root 4096 10-28 test4 www.2cto.com instance 8: run the following command to move the files in one sub-directory of the current directory to another sub-directory: mv test3 /*. txt test5 output: [root @ localhost test] # ll drwxr-xr-x 6 root 4096 10-27 0:58 scf drwxrwxrwx 4 root 4096 10-28 07:02 test3 drwxr-xr-x 3 root 4096 10-25 17:56 test5 [root @ localhost test] # cd test3 [root @ localhost test3] # ll-rw -R -- 1 root 25 10-28 07:02 log1.txt-rw-r -- 1 root 13 10-28 06:16 log2.txt drwxr-xr-x 2 root 4096 10 -28 logs-rw-r -- 1 root 29 10-28 05 test1.txt drwxr-xr-x 2 root 4096 10-28 test4 [root @ localhost test3] # cd .. [root @ localhost test] # mv test3 /*. txt test5 [root @ localhost test] # cd test5 [root @ localhost test5] # ll-rw-r -- 1 root 25 10-28 07:02 log 1. txt-rw-r -- 1 root 13 10-28 06:16 log2.txt-rw-r -- 1 root 29 10-28 06:05 test1.txt drwxr-xr-x 2 root root 4096 10-25 test5-1 [root @ localhost test5] # cd .. [root @ localhost test] # cd test3/[root @ localhost test3] # ll drwxr-xr-x 2 root 4096 10-28 21 logs drwxr-xr-x 2 root 4096 10-28 07:02 test4 [root @ localhost test3] # instance 9: make a simple backup before the file is overwritten. Add the following parameter-B command: mv log1.txt-B log2.t. Xt output: [root @ localhost test5] # ll-rw-r -- 1 root 25 10-28 07:02 log1.txt-rw-r -- 1 root 13 10-28 06:16 log2.txt -rw-r -- 1 root 29 10-28 test1.txt drwxr-xr-x 2 root 4096 10-25 test5-1 [root @ localhost test5] # mv log1.txt- B log2.txt mv: overwrite “log2.txt "? Y [root @ localhost test5] # ll-rw-r -- 1 root 25 10-28 log2.txt-rw-r -- 1 root 13 10-28 log2.txt ~ -Rw-r -- 1 root 29 10-28 test1.txt drwxr-xr-x 2 root 4096 10-25 test5-1 [root @ localhost test5] # description: -B does not accept the parameter. The mv reads the environment variable VERSION_CONTROL as the backup policy. -- Backup this option specifies the action when the target file exists. There are four backup policies: www.2cto.com 1. CONTROL = none or off: no backup. 2. CONTROL = numbered or t: digital backup 3. CONTROL = existing or nil... n: Before executing MVPs, there is a file named "log2.txt. ~ 1 ~, The next execution will commit log2.txt ~ 2 ~, And so on. If there is no file numbered by number, use the simple backup described below. 4. CONTROL = simple or never: use simple backup: A simple backup is performed before it is overwritten. A simple backup can only have one copy. If it is overwritten again, a simple backup will also be overwritten.