Linux Basics Command (7) MV command

Source: Internet
Author: User
Tags file system

MV command is the abbreviation for move, which can be used for moving files or renaming files (rename), is a commonly used command in Linux systems and is often used to back up files or directories.

1. Command format:

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

2. Command function:

The MV command renames the file or moves it to a new directory, depending on the type of the second parameter in the video MV Command, which is the target file or the target directory. When the second parameter type is a file, the MV command completes the file rename, at which point the source file can have only one (or a source directory name) that renames the given source file or directory to the given target filename. When the second argument is a directory name that already exists, the source file or directory parameter can have multiple, 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 is copied first, the original file is deleted, and the link to the file is lost.

3. Command parameters:

-B: Overwrite the file before overwriting.

-f:force the meaning of coercion, if the target file already exists, will not inquire and directly cover;

-I: If the target file (destination) already exists, it will ask if the overwrite!

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

4. Command instance:

Example one: file name change

Command:

MV Test.log Test1.txt

Output:

[Root@localhost 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 10-28 06:04 test.log

[Root@localhost test]# MV Test.log test1.txt

[Root@localhost test]# LL

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

-rw-r--r--1 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 a file

Command:

MV Test1.txt Test3

Output:

[Root@localhost test]# LL

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

-rw-r--r--1 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

[Root@localhost test]# MV Test1.txt test3

[Root@localhost 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

[Root@localhost test]# CD Test3

[Root@localhost test3]# LL

Total 4

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

[Root@localhost 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:

[Root@localhost test]# LL

Total 28

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

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

-rw-r--r--1 root 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]# LL

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

[Root@localhost test]# CD test3/

[Root@localhost test3]# LL

Total 16

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

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

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

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

[Root@localhost test3]#

[Root@localhost test3]# LL

Total 20

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

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

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

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

-rw-r--r--1 root 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--r--1 root 8 10-28 06:15 log1.txt

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

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

[Root@localhost test4]#

Description

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

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.