In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "the usage of mv command in linux". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the usage of the mv command in linux.
1. Command format:
Mv [options] Source file or directory destination 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 a file, the mv command finishes renaming the file. At this point, there can be only one source file (or source directory name), which renames the given source file or directory to the given destination file name. When the second parameter is an existing directory name, there can be multiple source files or directory parameters, and the mv command moves the source files specified by each parameter to the destination directory. When moving a file across a file system, mv copies and then deletes the original file, and the link to the file is lost.
3. Command parameters:
-b: if you need to overwrite the file, overwrite the previous backup.
-f: force force means that if the target file already exists, it will be overwritten without being asked.
-I: if the target file (destination) already exists, it will be asked whether to overwrite it!
-u: if the target file already exists and the source is relatively new, it will be updated (update)
-t:-- target-directory=DIRECTORY move all SOURCE arguments into DIRECTORY, that is, specify the destination directory of the mv. This option is suitable for moving multiple source files to a directory, where the destination directory comes first and the source file comes after.
4. Command example:
Example 1: file renaming
Command:
Mv test.log test1.txt
Output:
The code is as follows:
[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 root 16 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 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
Description:
Rename the file test.log to test1.txt
Example 2: moving files
Command:
Mv test1.txt test3
Output:
The code is as follows:
[root@localhost test] # ll
Total 20drwxr-xr-x 6 root 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 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] #
Description:
Move the test1.txt file to the directory test3
Example 3: 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:
The code is as follows:
[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 root 4096 10-28 06: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] #
Description:
The mv log1.txt log2.txt log3.txt test3 command moves the log1.txt, log2.txt, and 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 the file file1 to file2. If file2 already exists, ask whether to overwrite it.
Command:
Mv-I log1.txt log2.txt
Output:
The code is as follows:
[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: does it overwrite "log2.txt"? Y
[root@localhost test4] # cat log2.txt
Odfdfs
[root@localhost test4] #
Example 5: rename the file file1 to file2, even if file2 exists, it will be overwritten directly.
Command:
Mv-f log3.txt log2.txt
Output:
The code is as follows:
[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: there is no such 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] #
Description:
The content of log3.txt directly covers the content of log2.txt.-f this is a dangerous option. Be sure to keep a clear mind when using it. In general, it is best not to add it.
Example 6: the movement of directories
Command:
Mv dir1 dir2
Output:
The code is as follows:
[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] #
Description:
If the directory dir2 does not exist, rename the directory dir1 to dir2; otherwise, move the dir1 to dir2.
Example 7: move all files under the current folder to a higher-level directory
Command:
Mv *.. /
Output:
The code is as follows:
[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 subdirectory
Command:
Mv test3/*.txt test5
Output:
The code is as follows:
[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, preceded by the parameter-b
Command:
Mv log1.txt-b log2.txt
Output:
The code is as follows:
[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: does it overwrite "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] #
Description:
-b does not accept parameters, mv will read the environment variable VERSION_CONTROL as the backup strategy.
-- backup this option specifies the action if the destination file exists, there are four backup strategies:
1.CONTROL=none or off: no backup.
2.CONTROL=numbered or t: numbered backup
3.CONTROL=existing or nil: if there is a numbered backup, continue with the numbered backup mnumbered 1.. n:
The numbered file log2.txt.~1~, already exists before the mv operation is performed, so execution again will result in log2.txt~2~, and so on. If there are no numbered files before, use the simple backup described below.
4.CONTROL=simple or never: use a simple backup: a simple backup is made before it is overwritten, there can be only one simple backup, and when it is overwritten again, the simple backup is overwritten.
At this point, I believe you have a deeper understanding of "the use of the mv command in linux". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.