Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Examples of advanced usage of the mv command in Linux

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Preface

The mv command is an abbreviation for move, which can be used to move files or rename files (move (rename) files). It is a commonly used command on Linux systems and is often used to back up files or directories.

Command format:

Mv [options] Source file or directory destination file or directory

Mv is also the next very frequently used command for Linux, but what other advanced uses do you know besides some basic usage?

1. Basic usage

Move one / more files; move one / more directories; rename files / directories.

These are very basic uses, and there is no need to repeat them. Here are some more advanced uses.

two。 Print operation information

If we only move one or two or a few files / directories, we can also go to the target location to see if the file has been moved successfully. But what if there are thousands of documents? How do you know if our operation has been successful without going to the target location to check it? 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 moving directories.

3. Use interactive mode

By default, we don't get any prompts 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 directly replace the original file. Such an operation can sometimes have disastrous consequences.

In this case, we can add a-I option, which will prompt when the target location has a file with the same name. If you confirm that 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 in the target location, we do not 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' # there is no file3.txt file in the target location So successfully move [alvin@VM_0_16_centos mv_test] $lsdes file2.txt

5. Use update option

When there is a file / directory with the same name in the target location, we only want the source file to be updated than the target file timestamp, 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 # source file timestamp is updated than destination file timestamp-rw-rw-r-- 1 alvin alvin 0 Feb 8 17:26 file2.txt # source file is older than destination file timestamp-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 timestamped updated files are replaced [alvin@VM_0_16_centos mv_test] $lsdes file2.txt

6. Create a backup before overwriting

The target location already has a file with the same name, so if you overwrite it directly, it always feels a bit unreliable, what if the decision is wrong now? 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 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

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

These are some of the more advanced uses of the mv command, which, if used flexibly, will improve your productivity and be more pretentious in front of colleagues.

Summary

The above is the whole content of this article. I hope the content of this article has a certain reference and learning value for everyone's study or work. Thank you for your support.

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report