One Linux command per day (7): mv command

Keywords: Linux Mobile

mv command is the abbreviation of move. It can be used to move files or rename files. It is a common command in Linux system and is often used to backup files or directories.

1. Command format:

mv [option] source file or directory, target file or directory

2. Command function:

Depending on whether the second parameter type in the mv command 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 a file, the mv command completes the file renaming. At this time, the source file can only have one (or the source directory name), which renames the given source file or directory to the given target file name. When the second parameter is the existing directory name, the source file or directory parameter can have more than one. The mv command moves the source file specified by each parameter to the target directory. When moving files across file systems, mv copies them first, then deletes the original files, and links to the files will be lost.

3. Command parameters:

- b: If you need to overwrite files, backup before overwriting.

- f: force means that if the target file already exists, it will not be queried and overwritten directly.

- i: If the destination already exists, it will ask if it is overwritten!

- u: If the target file already exists and the source is relatively new, it will be updated.

- t: - target-directory=DIRECTORY move all SOURCE arguments into DIRECTORY, which specifies the target directory of mv. This option applies to moving multiple source files to a directory, where the target directory is in front and the source file is in the back.

4. Command examples:

Example 1: File renaming

Order:

mv test.log test1.txt

Output:

[root@localhost test]# ll

//Total 20 drwxr-xr-x 6 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   16 10-28 06:04 test.log

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

[root@localhost test]# ll

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

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

Explain:

Rename the file test.log to test1.txt

Example 2: Mobile files

Order:

mv test1.txt test3

Output:

[root@localhost test]# ll

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

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

[root@localhost test3]#

Explain:

Move the test1.txt file to directory test3

Example 3: Move the files log1.txt,log2.txt,log3.txt to the directory test3.

Order:

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

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-2806:18 test3

[root@localhost test]# cd test3/

[root@localhost test3]# ll

//Total 16

-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]# ll

//Total 20

-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

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

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

Explain:

The mv log1.txt log2.txt log3.txt test3 command moves the log1.txt, log2.txt, log3.txt files to the test3 directory, and 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 file 1 to file 2, and if file 2 already exists, ask if it is overwritten.

Order:

mv -i log1.txt log2.txt

Output:

[root@localhost test4]# ll

//Total 12

-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]# cat log1.txt

odfdfs

[root@localhost test4]# cat log2.txt

ererwerwer

[root@localhost test4]# mv -i log1.txt log2.txt

mv: Whether to Cover“ log2.txt"? y

[root@localhost test4]# cat log2.txt

odfdfs

[root@localhost test4]#

Example 5: Rename file1 to file2, and even if File2 exists, it will be overwritten directly.

Order:

mv -f log3.txt log2.txt

Output:

[root@localhost test4]# ll

//Total 8

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

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

[root@localhost test4]# cat log2.txt

odfdfs

[root@localhost test4]# cat log3

cat: log3: No file or directory

[root@localhost test4]# ll

//Total 8

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

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

[root@localhost test4]#

Explain:

The content of log3.txt directly covers the content of log2.txt, -f is a dangerous option, you must keep your mind clear when using it, generally it is better not to add it.

Example 6: Moving directories

Order:

mv dir1 dir2

Output:

[root@localhost test4]# ll

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

[root@localhost test4]# ll

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

[root@localhost test4]# cd ..

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

[root@localhost test]# cd test3

[root@localhost test3]# ll

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

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

[root@localhost test]# cd test3/

[root@localhost test3]# ll

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

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

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

[root@localhost test3]#

Explain:

If directory dir2 does not exist, change directory dir1 to dir2; otherwise, move dir1 to dir2.

Example 7: Move all files under the current folder to a higher level directory

Order:

mv * ../

Output:

[root@localhost test4]# ll

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

-rw-r--r-- 1 root 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--r-- 1 root root   25 10-28 07:02 log1.txt

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

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

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

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

Example 8: Move files from one subdirectory of the current directory to another

Order:

mv test3/*.txt test5

Output:

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

[root@localhost test]# cd test3

[root@localhost test3]# ll

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

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

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

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

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

[root@localhost test3]# cd ..

[root@localhost test]# mv test3/*.txt test5

[root@localhost test]# cd test5

[root@localhost test5]# ll

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

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

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

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

[root@localhost test5]# cd ..

[root@localhost test]# cd test3/

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

[root@localhost test3]#

Example 9: Make a simple backup before the file is overwritten, and add parameter - b before the file is overwritten.

Order:

mv log1.txt -b log2.txt

Output:

[root@localhost test5]# ll

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

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

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

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

[root@localhost test5]# mv log1.txt -b log2.txt

mv: Whether to Cover“ log2.txt"? y

[root@localhost test5]# ll

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

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

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

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

[root@localhost test5]#

Explain:

- b does not accept parameters, and mv reads the environment variable VERSION_CONTROL as a backup strategy.

Backup This option specifies four backup strategies if the target file exists:

1.CONTROL=none or off: No backup.

2.CONTROL=numbered or t: Backup of digital numbers

3.CONTROL=existing or nil: If a digitally numbered backup exists, continue numbered backup m+1... n:

Before the mv operation, there is a digitally numbered file log2.txt.~1 ~, then the second execution will produce log2.txt~2 ~, by analogy. If there are no previously digitally numbered files, use the simple backup described below.

4.CONTROL=simple or never: Use simple backups: Simple backups are made before they are overwritten. Simple backups can only have one copy. Simple backups can also be overwritten when they are overwritten again.

Note: This article refers to the "Linux enthusiasts" Wechat Public Number, for their own learning and use, welcomed Wechat search or scanning attention, access to the latest knowledge every day.

Posted by number67a on Thu, 11 Apr 2019 13:21:32 -0700