Mv command), mv command

Source: Internet
Author: User

Mv command (conversion) and mv command

Original article: http://www.cnblogs.com/peida/archive/2012/10/27/2743022.html

The 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 functions:

Depending on the type of the second parameter in the mv command (whether it is the target file or the target 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.

3. command parameters:

-B: If you need to overwrite the file, back up the file before overwriting.

-F: force indicates that if the target file already exists, it is overwritten without asking;

-I: If the destination file (destination) already exists, you will be asked if it is overwritten!

-U: update is performed only when the target file already exists and the source file is updated)

-T: -- target-directory = DIRECTORY move all SOURCE arguments into DIRECTORY, that is, the target directory of the specified mv. This option is applicable when multiple SOURCE files are moved to one DIRECTORY, in this case, the target directory is in the front and the source file is in the back.

4. Command instance:

Instance 1: rename a file

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 06:04 test. log

[Root @ localhost test] # mv test. log test1.txt

[Root @ localhost test] # ll

Total 20drwxr-xr-x 6 root 4096 10-27 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

Note:

Rename test.logas test1.txt

 

Example 2: Move a file

Command:

Mv test1.txt test3

Output:

[Root @ localhost test] # ll

Total 20drwxr-xr-x 6 root 4096 10-27 scf

-Rw-r -- 1 root 29 10-28 06:05 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

[Root @ localhost test] # mv test1.txt test3

[Root @ localhost test] # ll

Total 16drwxr-xr-x 6 root 4096 10-27 scf

Drwxrwxrwx 2 root 4096 10-28 test3

Drwxr-xr-x 2 root 4096 10-25 test4

Drwxr-xr-x 3 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] #

 

Note:

Move the test1.txt file to the test3 directory.

Example 3: Move the log1.txt, log2.txt, and log3.txt files 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 06:15 log2.txt

-Rw-r -- 1 root 13 10-28 06:16 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 06:15 log1.txt

-Rw-r -- 1 root 12 10-28 06:15 log2.txt

-Rw-r -- 1 root 13 10-28 06:16 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 06:15 log1.txt

-Rw-r -- 1 root 12 10-28 06:15 log2.txt

-Rw-r -- 1 root 13 10-28 06:16 log3.txt

Drwxr-xr-x 2 root 4096 10-28 logs

-Rw-r -- 1 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] # ll

Total 12

-Rw-r -- 1 root 8 10-28 06:15 log1.txt

-Rw-r -- 1 root 12 10-28 06:15 log2.txt

-Rw-r -- 1 root 13 10-28 06:16 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.

Example 4: Rename the file file1 to file2. If file2 already exists, check whether the file is overwritten.

Command:

Mv-I log1.txt log2.txt

Output:

[Root @ localhost test4] # ll

Total 12

-Rw-r -- 1 root 8 10-28 06:15 log1.txt

-Rw-r -- 1 root 12 10-28 06:15 log2.txt

-Rw-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: Why does the volume overwrite log2.txt "? Y

[Root @ localhost test4] # cat log2.txt

Odfdfs

[Root @ localhost test4] #

 

Instance 5: Rename the file file1 to file2, and overwrite the file even if file2 exists.

Command:

Mv-f log3.txt log2.txt

Output:

[Root @ localhost test4] # ll

Total 8

-Rw-r -- 1 root 8 10-28 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: the file or directory does not exist.

[Root @ localhost test4] # ll

Total 8

-Rw-r -- 1 root 8 10-28 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.txt

Dfosdfsdfdss

[Root @ localhost test4] # mv-f log3.txt log2.txt

[Root @ localhost test4] # cat log2.txt

Dfosdfsdfdss

[Root @ localhost test4] # ll

Total 4

-Rw-r -- 1 root 13 10-28 06:16 log2.txt

[Root @ localhost test4] #

Note:

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: Directory Movement

Command:

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 0scf

Drwxrwxrwx 3 root 4096 10-28 test3

Drwxr-xr-x 2 root 4096 10-28 test4

Drwxr-xr-x 3 root 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 0scf

Drwxrwxrwx 4 root 4096 10-28 test3

Drwxr-xr-x 3 root 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

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-28 logs

-Rw-r -- 1 root 29 10-28 06:05 test1.txt

Drwxr-xr-x 2 root 4096 10-28 test4

Example 8: Move files in one sub-directory of the current directory to another sub-directory

Command:

Mv test3/*. txt test5

Output:

[Root @ localhost test] # ll

Drwxr-xr-x 6 root 4096 10-27 0scf

Drwxrwxrwx 4 root 4096 10-28 test3

Drwxr-xr-x 3 root 4096 10-25 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 06: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 log1.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 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 logs

Drwxr-xr-x 2 root 4096 10-28 test4

[Root @ localhost test3] #

 

Example 9: make a simple backup before the file is overwritten. Add the parameter-B to the front.

Command:

Mv log1.txt-B log2.txt

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 06:05 test1.txt

Drwxr-xr-x 2 root 4096 10-25 test5-1

[Root @ localhost test5] # mv log1.txt-B log2.txt

Mv: Why does the volume overwrite log2.txt "? Y

[Root @ localhost test5] # ll

-Rw-r -- 1 root 25 10-28 07:02 log2.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 4096 10-25 test5-1

[Root @ localhost test5] #

Note:

-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:

1. CONTROL = none or off: no backup.

2. CONTROL = numbered or t: digital backup

3. CONTROL = existing or nil: if there is a backup numbered with numbers, continue the backup numbered m + 1... n:

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.