Linux common commands (vi)-MV

Source: Internet
Author: User

The MV command is the abbreviation for move, which can be used to move files or rename files (rename), which is a common command under Linux, often used to back up files or directories.


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: Overwrite the previous backup if you need to overwrite the file.

-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: This option is useful when moving multiple source files to a directory, when the destination directory is in front of the source file.


4. Command instance:

Example one: renaming a file

Command: MV Test.log test1.txt

Rename the file Test.log to Test1.txt

[[email protected] test]# lldrwxrwxrwx 2 root root 4096 10-25 17:46 test3drwxr-xr-x 2 root root 4096 10-25 17:56 test4drwx R-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 Te  St.log Test1.txt[[email protected] test]# ll-rw-r--r--1 root root +   10-28 06:04 test1.txtdrwxrwxrwx 2 root root 4096 10-25 17:46 test3drwxr-xr-x 2 root root 4096 10-25 17:56 test4drwxr-xr-x 3 root root 4096 10-25 17:56 test5

Example Two: move a file to a folder

Command: MV Test1.txt test3

Move the Test1.txt file to the directory Test3

[Email protected] test]# ll-rw-r--r--1 root root   10-28 06:05 test1.txtdrwxrwxrwx 2 root root 4096 10-25 17:46 tes T3drwxr-xr-x 2 root root 4096 10-25 17:56 test4drwxr-xr-x 3 root root 4096 10-25 17:56 test5[[email protected] test]# MV T Est1.txt Test3[[email protected] test]# lldrwxrwxrwx 2 root root 4096 10-28 06:09 test3drwxr-xr-x 2 root root 4096 10-25 1 7:56 test4drwxr-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 06:05 test1.txt[[email protected] test3]#

Example Three: moving multiple files to a folder

Command 1:MV log1.txt log2.txt log3.txt test3 Move file log1.txt log2.txt log3.txt to directory Test3

Command 2:mv-t/opt/soft/test/test4/log1.txt log2.txt log3.txt Move file log1.txt log2.txt log3.txt to test4 directory to go

[[email protected] test]# ll-rw-r--r--1 root root 8 10-28 06:15 log1.txt-rw-r--r--1 root root 10-28 06:15 l og2.txt-rw-r--r--1 root root 10-28 06:16 log3.txtdrwxrwxrwx 2 root root 4096 10-28 06:09 test3[[email protected ] test]# mv Log1.txt log2.txt log3.txt test3[[email protected] test]# lldrwxrwxrwx 2 root root 4096 10-28 06:18 test3 [[email protected] test]# CD test3/[[email protected] test3]# ll-rw-r--r--1 root root 8 10-28 06:15 log1.txt-r w-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 29 10-28 06:05 test1.txt[[email protected] test3]#[[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-rw-r--r--1 root root 8 10-28 06:15 log1.txt-r w-r--r--1 root root 10-28 06:15 log2.txt-rw-r--r--1 root root 10-28 06:16 log3.txt[[email&Nbsp;protected] test4]# 

Example four: renaming the file file1 to File2, even if file2 exists, is directly overwritten.

command: mv-f log3.txt log2.txt

Log3.txt content directly covers the content of Log2.txt,-F is a very dangerous option, generally best not to use.


Example Five: Moving the directory

Command: MV Test3 test4

Move directory test3 to directory test4, rename test3 to test4 if directory test4 does not exist


Experiment Six: move all files under the current folder to the top level directory

Command: MV *. /


Experiment Seven: move files in one subdirectory of the current directory to another subdirectory

Command: MV Test3/*.txt TEST5

Move a file with test3 suffix txt to the TEST5 directory


Experiment Eight: file is overwritten before making a simple backup, preceded by parameter-B

Command: MV Log1.txt-b log2.txt

[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   l Og2.txt[[email protected] test5]# mv Log1.txt-b LOG2.TXTMV: 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   13 10-28 06:16 Log2.txt~[[email protected] test5]#



Linux common commands (vi)-MV

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.