Examples of advanced usage of the mv command in Linux

  • 2021-01-22 08:24:24
  • OfStack

preface

mv is short for move. It can be used to move files or rename them (move (rename) files). It is a common command for Linux systems and is often used to back up files or directories.

Command format:

[

mv [option] source file or directory Target file or directory

]

mv is one of the most frequently used commands under Linux, but what other advanced uses do you know of it?

1. Basic usage

Move 1 / more files; Move 1 / more directories; Rename files/directories.

These are all very basic uses and don't need to be repeated. Here are some more advanced uses.

2. Print operational information

If we only move 1 or 2 or a few files/directories, we can also temporarily go to the target location to check whether the file was moved successfully. But what if there are hundreds of files? How do we know if our operation has been successful without looking at the target location? 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, the same applies to moving directories.

3. Use interactive mode

By default, we move files/directories without prompting. If we have a file/directory with the same name in the target location, the mv command will replace the original file without prompting. And such operations can sometimes have disastrous consequences.

In this case, we can add a -i option, which will be prompted when the target location has a file of the same name. If you do want to overwrite the target file, just 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 of the same name

If the target location has a file with the same name and we don't want it 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' # Target location is not available file3.txt File, so successfully moved 
[alvin@VM_0_16_centos mv_test]$ ls
des file2.txt

5. Use the update option

If the target location has a file/directory of the same name, we only want the source file to be updated than the target file timestamp, then we will perform the overwrite, otherwise do not execute. 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 is newer than the destination file timestamp 
-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 timestamp updates are replaced 
[alvin@VM_0_16_centos mv_test]$ ls
des file2.txt

6. Create a backup before overwriting

The target location has the same name of the file, if the direct overlay always feel a little unpractical, 10,000 now the decision is wrong? Is it possible to back up the target file before overwriting it? The answer is yes, with an -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

As you can see, before overwriting, the target file will be backed up as a tilde ~ file, and careful readers will also notice that the timestamp of the overwritten file is not the same as the backup file.

The above is mv command 1 more advanced usage, flexible use of words for their work efficiency to improve a level, in front of colleagues can also be more forced.

conclusion


Related articles: