Turn a Linux command every day (7): MV command

Source: Internet
Author: User

1. Command format:

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

2. Command function:

Depending on the second parameter type in the MV command (either the destination file or the destination directory), the MV command renames the file or moves it to a new directory. When the second parameter type is a file, the MV command completes the file rename, at which point the source file can only have one (or the source directory name), which renames the given source file or directory to the specified destination file name. When the second parameter is a directory name that already exists, the source file or directory parameter can have more than one, and the MV command moves the source files specified by each parameter to the destination directory. When you move a file across a file system, the MV copies the original file, and the link to the file is lost.

3. Command parameters:

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

-f:force mandatory Meaning, if the target file already exists, will not be asked and directly covered;

-I: If the target file (destination) already exists, you will be asked to overwrite it!

-U: If the target file already exists and source is newer, update (update)

-T:--target-directory=directory move all source arguments into directory, which is the target directory for the specified MV, which is suitable for moving multiple source files to a directory, when the target directory is in front, The source file is behind.

4. Command instance:

Example one: Renaming a file

Command:

MV Test.log Test1.txt

Output:

[email protected] test]# LL

Total 20drwxr-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 10-28 06:04 Test.log

[Email protected] test]# MV Test.log test1.txt

[email protected] test]# LL

Total 20drwxr-xr-x 6 root root 4096 10-27 01:58 SCF

-rw-r--r--1 root root 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 two: Moving files

Command:

MV Test1.txt Test3

Output:

[email protected] test]# LL

Total 20drwxr-xr-x 6 root root 4096 10-27 01:58 SCF

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

[Email protected] test]# MV Test1.txt test3

[email protected] test]# LL

Total 16drwxr-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

[Email protected] test]# CD TEST3

[email protected] test3]# LL

Total 4

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

[Email protected] test3]#

Description

Move the Test1.txt file to the directory Test3

Example three: Move the file 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:

[email protected] test]# LL

Total 28

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

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

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

DRWXRWXRWX 2 root root 4096 10-28 06:09 test3

[Email protected] test]# mv Log1.txt log2.txt log3.txt test3

[email protected] test]# LL

Total 16drwxrwxrwx 2 root root 4096 10-28 06:18 test3

[Email protected] test]# CD test3/

[email protected] test3]# LL

Total 16

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

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

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

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

[Email protected] test3]#

[email protected] test3]# LL

Total 20

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

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

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

Drwxr-xr-x 2 root root 4096 10-28 06:21 logs

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

[Email protected] test3]# mv-t/opt/soft/test/test4/log1.txt log2.txt log3.txt

[Email protected] test3]# CD.

[Email protected] test]# CD test4/

[email protected] test4]# LL

Total 12

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

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

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

[Email protected] test4]#

Description

MV Log1.txt log2.txt log3.txt test3 command to move Log1.txt, Log2.txt, log3.txt three files to test3 directory, mv-t/opt/soft/test/test4/log1.txt The Log2.txt log3.txt command also moves three files to the Test4 directory.

Example four: Rename the file file1 to File2, if file2 already exists, ask whether to overwrite

Command:

Mv-i Log1.txt Log2.txt

Output:

[email protected] test4]# LL

Total 12

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

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

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

[email protected] test4]# cat Log1.txt

Odfdfs

[email protected] test4]# cat Log2.txt

Ererwerwer

[Email protected] test4]# mv-i log1.txt log2.txt

MV: Do you want to overwrite "Log2.txt"? Y

[email protected] test4]# cat Log2.txt

Odfdfs

[Email protected] test4]#

Example five: The file file1 renamed to File2, even if the file2 exists, is also directly covered out.

Command:

Mv-f Log3.txt Log2.txt

Output:

[email protected] test4]# LL

Total 8

-rw-r--r--1 root root 8 10-28 06:15 log2.txt

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

[email protected] test4]# cat Log2.txt

Odfdfs

[email protected] test4]# cat Log3

Cat:log3: No file or directory

[email protected] test4]# LL

Total 8

-rw-r--r--1 root root 8 10-28 06:15 log2.txt

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

[email protected] test4]# cat Log2.txt

Odfdfs

[email protected] test4]# cat Log3.txt

Dfosdfsdfdss

[Email protected] test4]# mv-f log3.txt log2.txt

[email protected] test4]# cat Log2.txt

Dfosdfsdfdss

[email protected] test4]# LL

Total 4

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

[Email protected] test4]#

Description

Log3.txt content directly covers the log2.txt content,-f This is a dangerous option, use must keep a clear mind, generally it is best not to add it.

Example six: Moving a directory

Command:

MV Dir1 Dir2

Output:

[email protected] test4]# LL

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

[email protected] test4]# LL

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

[Email protected] test4]# CD.

[email protected] test]# LL

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

[Email protected] test]# CD TEST3

[email protected] test3]# LL

Drwxr-xr-x 2 root root 4096 10-28 06:21 logs

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

[Email protected] test3]# CD.

[Email protected] test]# MV Test4 test3

[email protected] test]# LL

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

[Email protected] test]# CD test3/

[email protected] test3]# LL

Drwxr-xr-x 2 root root 4096 10-28 06:21 logs

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

Drwxr-xr-x 2 root root 4096 10-28 06:48 test4

[Email protected] 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 under the current folder to the top level directory

Command:

MV *.. /

Output:

[email protected] test4]# LL

-rw-r--r--1 root root 10-28 07:02 log1.txt

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

[Email protected] test4]# MV *.. /

[email protected] test4]# LL

[Email protected] test4]# CD.

[email protected] test3]# LL

-rw-r--r--1 root root 10-28 07:02 log1.txt

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

Drwxr-xr-x 2 root root 4096 10-28 06:21 logs

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

Drwxr-xr-x 2 root root 4096 10-28 07:02 test4

Example eight: Moving files in one subdirectory of the current directory to another subdirectory

Command:

MV Test3/*.txt TEST5

Output:

[email protected] test]# LL

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

[Email protected] test]# CD TEST3

[email protected] test3]# LL

-rw-r--r--1 root root 10-28 07:02 log1.txt

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

Drwxr-xr-x 2 root root 4096 10-28 06:21 logs

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

Drwxr-xr-x 2 root root 4096 10-28 07:02 test4

[Email protected] test3]# CD.

[Email protected] test]# MV Test3/*.txt TEST5

[Email protected] test]# CD TEST5

[email protected] test5]# LL

-rw-r--r--1 root root 10-28 07:02 log1.txt

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

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

Drwxr-xr-x 2 root root 4096 10-25 17:56 test5-1

[Email protected] test5]# CD.

[Email protected] test]# CD test3/

[email protected] test3]# LL

Drwxr-xr-x 2 root root 4096 10-28 06:21 logs

Drwxr-xr-x 2 root root 4096 10-28 07:02 test4

[Email protected] test3]#

Example nine: file is overwritten before making a simple backup, preceded by parameter-B

Command:

MV Log1.txt-b Log2.txt

Output:

[email protected] test5]# LL

-rw-r--r--1 root root 10-28 07:02 log1.txt

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

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

Drwxr-xr-x 2 root root 4096 10-25 17:56 test5-1

[Email protected] test5]# MV Log1.txt-b log2.txt

MV: Do you want to overwrite "Log2.txt"? Y

[email protected] test5]# LL

-rw-r--r--1 root root 10-28 07:02 log2.txt

-rw-r--r--1 root root 10-28 06:16 log2.txt~

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

Drwxr-xr-x 2 root root 4096 10-25 17:56 test5-1

[Email protected] test5]#

Description

-B does not accept parameters, and MV will read the environment variable Version_control as a backup strategy.

--backup This option specifies a total of four backup strategies if the target file is present:

1.control=none or off: not backed up.

2.control=numbered or T: Digitally numbered backup

3.control=existing or nil: if there is a digitally numbered backup, continue numbering the backup M+1...N:

There is a digitally numbered file log2.txt.~1~ before performing the MV operation, then execution will produce log2.txt~2~, and so on. If you don't have a digitally numbered file before, use the simple backup described below.

4.control=simple or never: Use simple backup: A simple backup is made before being overwritten, a simple backup can only have one copy, and a simple backup will be overwritten when it is overwritten again.

Turn a Linux command every day (7): MV command

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.