Preface The mv command is the abbreviation of move, which can be used to move files or rename files (move (rename) files). It is a commonly used command in Linux systems and is often used to back up files or directories. Command format:
mv is also a very frequently used command in Linux, but in addition to some basic uses, what other advanced uses do you know? 1. Basic usage
These are very basic usages and there is no need to elaborate on them. Here are some more advanced uses. 2. Print operation information If we only move one or two or a few files/directories, we can still go to the target location to check whether the files have been moved successfully. But what if there are thousands of files? How can we know whether our operation has been successful without going to the target location to check? We can add the -v option. [alvin@VM_0_16_centos mv_test]$ mv -v *.txt /home/alvin/test/mv_test/des/ 'file1.txt' -> '/home/alvin/test/mv_test/des/file1.txt' 'file2.txt' -> '/home/alvin/test/mv_test/des/file2.txt' 'file3.txt' -> '/home/alvin/test/mv_test/des/file3.txt' 'file4.txt' -> '/home/alvin/test/mv_test/des/file4.txt' 'file5.txt' -> '/home/alvin/test/mv_test/des/file5.txt' Of course, this method also applies to mobile directories. 3. Use interactive mode By default, there will be no prompt when we move files/directories. If we have a file/directory with the same name at the target location, the mv command will not prompt and will directly replace the original file. Such operations may sometimes lead to disastrous consequences. In this case, we can add a -i option, which will prompt when there is a file with the same name in the target location. If you are sure you want to overwrite the target file, type y. [alvin@VM_0_16_centos mv_test]$ mv -i file1.txt /home/alvin/test/mv_test/des/ mv: overwrite '/home/alvin/test/mv_test/des/file1.txt'? y 4. Do not overwrite files with the same name If there is a file with the same name at the target location and we don't want it to be overwritten, we can add the -n option. [alvin@VM_0_16_centos mv_test]$ ll *.txt des/*.txt -rw-rw-r-- 1 alvin alvin 0 Feb 8 17:26 file2.txt -rw-rw-r-- 1 alvin alvin 0 Feb 8 17:35 file3.txt -rw-rw-r-- 1 alvin alvin 0 Feb 8 17:26 des/file1.txt -rw-rw-r-- 1 alvin alvin 0 Feb 8 17:27 des/file2.txt [alvin@VM_0_16_centos mv_test]$ mv -nv *.txt /home/alvin/test/mv_test/des/ 'file3.txt' -> '/home/alvin/test/mv_test/des/file3.txt' #The target location does not have file3.txt file, so it is moved successfully [alvin@VM_0_16_centos mv_test]$ ls des file2.txt 5. Use the update option When there is a file/directory with the same name at the target location, we only want to overwrite the source file when the timestamp is newer than that of the target file, otherwise it will not be executed. In this case, we can use the -u option. [alvin@VM_0_16_centos mv_test]$ ll *.txt des/*.txt -rw-rw-r-- 1 alvin alvin 0 Feb 8 17:26 file1.txt #The source file has a newer timestamp than the target file -rw-rw-r-- 1 alvin alvin 0 Feb 8 17:26 file2.txt #The source file has an older timestamp than the target file -rw-rw-r-- 1 alvin alvin 0 Feb 8 16:53 des/file1.txt -rw-rw-r-- 1 alvin alvin 0 Feb 8 17:27 des/file2.txt [alvin@VM_0_16_centos mv_test]$ mv -uv *.txt /home/alvin/test/mv_test/des/ 'file1.txt' -> '/home/alvin/test/mv_test/des/file1.txt' #Only files with updated timestamps are replaced [alvin@VM_0_16_centos mv_test]$ ls des file2.txt 6. Create a backup before overwriting There is already a file with the same name at the target location. I feel a little uneasy if I just overwrite it. What if the current decision is wrong? Is it possible to back up the target file before overwriting it? The answer is yes, just add a -b option. [alvin@VM_0_16_centos mv_test]$ mv -bv *.txt /home/alvin/test/mv_test/des/ 'file1.txt' -> '/home/alvin/test/mv_test/des/file1.txt' (backup: '/home/alvin/test/mv_test/des/file1.txt~') 'file2.txt' -> '/home/alvin/test/mv_test/des/file2.txt' (backup: '/home/alvin/test/mv_test/des/file2.txt~') [alvin@VM_0_16_centos mv_test]$ ll des/ total 0 -rw-rw-r-- 1 alvin alvin 0 Feb 8 17:41 file1.txt -rw-rw-r-- 1 alvin alvin 0 Feb 8 17:26 file1.txt~ -rw-rw-r-- 1 alvin alvin 0 Feb 8 17:26 file2.txt -rw-rw-r-- 1 alvin alvin 0 Feb 8 17:27 file2.txt~ -rw-rw-r-- 1 alvin alvin 0 Feb 8 17:35 file3.txt It can be seen that before overwriting, the target file will be backed up as a file with a tilde ~. At the same time, careful readers will also notice that the timestamp of the overwritten file is different from that of the backup file. The above are some more advanced uses of the mv command. If you use them flexibly, you will improve your work efficiency and be more impressive in front of your colleagues. Summarize The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. You may also be interested in:
|
<<: Learn MySQL database in one hour (Zhang Guo)
>>: More Features of the JavaScript Console
In this article, I will show you how to develop a...
Table of contents 1. Overview of Docker consul 2....
Sometimes you need to use links, but you don't...
After reading some articles, I finally figured ou...
Detailed introduction to the steps of installing ...
Redux is a simple state manager. We will not trac...
JS provides three methods for intercepting string...
1. Flash plug-in package download address: https:...
Another important aspect of separating structure ...
This article shares the specific code for the WeC...
1. Command Introduction The file command is used ...
I am currently learning about Redis and container...
Some tips for deep optimization to improve websit...
If you are using the latest Ubuntu Server version...
Preface HTTP is a stateless communication protoco...