Advanced usage of mv command under Linux

mv is also a very frequently used command under Linux, but besides some basic usages, what advanced usages do you know?

1. Basic usage

  1. Move one/multiple files;
  2. Move one/multiple directories;
  3. Rename the file/directory.

These are very basic usages, so there is no need to go into details. Here are some more advanced usages.

2. Print operation information

If we only move one or two or a few files/directories, we can go to the target location for the time being to check whether the files are moved successfully. But what if there are hundreds of files? How can we know whether our operation has been successful without checking the target location? We can add -voptions.

[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 is also applicable to mobile directories.

3. Use interactive mode

By default, when we move files/directories, there will be no prompts. If we have a file/directory with the same name at the target location, the mv command will directly replace the original file without prompting. And such operations may sometimes cause catastrophic consequences.

In this case, we can add an -ioption when the destination file with the same name, it will prompt. If you are sure 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 with the same name

If the destination files have the same name, we do not want it to be covered, you can add -noptions.

[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’    #目标位置没有file3.txt文件,所以成功移动
[alvin@VM_0_16_centos mv_test]$ ls
des  file2.txt

5. Use update options

When there is a file/directory with the same name in the target location, we only want the source file to be overwritten when the timestamp of the target file is newer than that of the target file, otherwise it will not be executed. In this case, we can use the -uoption.

[alvin@VM_0_16_centos mv_test]$ ll *.txt des/*.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 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’    #只有时间戳更新的文件被替换
[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 in the target location. If you overwrite it directly, it feels a bit unreliable. What if the current decision is wrong? Is it possible to back up the target file before overwriting? The answer is yes, add a -boption.

[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 time stamp of the overwritten file is different from that of the backup file.

The above are some of the more advanced uses of the mv command. If you use it flexibly, it will increase your work efficiency to a higher level, and you will be more forceful in front of colleagues.

Finally, recently many friends asked me for the Linux learning roadmap , so based on my experience, I spent a month staying up late in my spare time and compiled an e-book. Whether you are in an interview or self-improvement, I believe it will help you! The directory is as follows:

Give it to everyone for free, just ask you to give me a thumbs up!

Ebook | Linux development learning roadmap

I also hope that some friends can join me to make this e-book more perfect!

Gain? I hope the old irons will have a three-strike combo so that more people can read this article

Recommended reading:

Guess you like

Origin blog.csdn.net/yychuyu/article/details/108089217