Common Linux commands (6)-mv

Source: Internet
Author: User

Common Linux commands (6)-mv

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

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: This option is applicable when multiple source files are moved to one directory. In this case, the target directory is located first and the source file is located later.

4.Command instance:

Instance 1:Rename a file

Command: mv test. log test1.txt

Rename test.logas test1.txt

[root@localhost test]# lldrwxrwxrwx 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-rw-r--r-- 1 root root   16 10-28 06:04 test.log[root@localhost test]# mv test.log test1.txt[root@localhost test]# ll-rw-r--r-- 1 root root   16 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 2:Move a file to a folder

Command: mv test1.txt test3

Move the test1.txt file to the test3 directory.

[root@localhost test]# ll-rw-r--r-- 1 root root   29 10-28 06:05 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[root@localhost test]# mv test1.txt test3[root@localhost test]# lldrwxrwxrwx 2 root root 4096 10-28 06:09 test3drwxr-xr-x 2 root root 4096 10-25 17:56 test4drwxr-xr-x 3 root root 4096 10-25 17:56 test5[root@localhost test]# cd test3[root@localhost test3]# ll-rw-r--r-- 1 root root 29 10-28 06:05 test1.txt[root@localhost test3]#
Example 3:Move multiple files to a folder

Command 1: mv log1.txt log2.txt log3.txt test3 move the log1.txt log2.txt file to the test3 directory

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

[root@localhost test]# ll-rw-r--r-- 1 root root    8 10-28 06:15 log1.txt-rw-r--r-- 1 root root   12 10-28 06:15 log2.txt-rw-r--r-- 1 root root   13 10-28 06:16 log3.txtdrwxrwxrwx 2 root root 4096 10-28 06:09 test3[root@localhost test]# mv log1.txt log2.txt log3.txt test3[root@localhost test]# lldrwxrwxrwx 2 root root 4096 10-28 06:18 test3[root@localhost test]# cd test3/[root@localhost test3]# ll-rw-r--r-- 1 root root  8 10-28 06:15 log1.txt-rw-r--r-- 1 root root 12 10-28 06:15 log2.txt-rw-r--r-- 1 root root 13 10-28 06:16 log3.txt-rw-r--r-- 1 root root 29 10-28 06:05 test1.txt[root@localhost test3]#[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-rw-r--r-- 1 root root  8 10-28 06:15 log1.txt-rw-r--r-- 1 root root 12 10-28 06:15 log2.txt-rw-r--r-- 1 root root 13 10-28 06:16 log3.txt[root@localhost test4]#

Example 4:Rename file1 to file2. If file2 exists, it is overwritten directly.

Command: mv-f log3.txt log2.txt

The content of log3.txt is directly covered by the content of log2.txt.-f is a very dangerous option. It is generally best not to use it.

Example 5:Directory Movement

Command: mv test3 test4

Move test3 to test4. If test4 does not exist, rename test3 to test4.

Lab 6:Move all files in the current folder to the upper-level directory

Command: mv *../

Experiment 7:Move the files in one sub-directory of the current directory to another sub-directory

Command: mv test3/*. txt test5

Move the file suffixed with txt in test3 to the test5 directory.

Lab 8:Make a simple backup before the file is overwritten, and add the parameter-B.

Command: mv log1.txt-B log2.txt

[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 [root @ localhost test5] # mv log1.txt-B log2.txtmv: 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 ~ [Root @ localhost test5] #

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.