Advanced Usage Examples of mv Command in Linux

Advanced Usage Examples of mv Command in Linux

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 [options] source file or directory target file or directory

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

  • Move one or more files;
  • Move one or more directories;
  • Rename a file/directory.

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:
  • Advanced Linux system commands (recommended)
  • Detailed explanation of the command to exit edit mode in Linux
  • Command to view binlog file creation time in Linux
  • Summary of Linux commands commonly used in work
  • Detailed explanation of mkdir command in Linux
  • Summary of Linux command methods to view used commands

<<:  Learn MySQL database in one hour (Zhang Guo)

>>:  More Features of the JavaScript Console

Recommend

How to develop Java 8 Spring Boot applications in Docker

In this article, I will show you how to develop a...

MySQL uses custom sequences to implement row_number functions (detailed steps)

After reading some articles, I finally figured ou...

MySQL installation tutorial under Linux centos7 environment

Detailed introduction to the steps of installing ...

Using react+redux to implement counter function and problems encountered

Redux is a simple state manager. We will not trac...

Detailed explanation of three methods of JS interception string

JS provides three methods for intercepting string...

Solution to Ubuntu 20.04 Firefox cannot play videos (missing flash plug-in)

1. Flash plug-in package download address: https:...

Semantic web pages XHTML semantic markup

Another important aspect of separating structure ...

WeChat applet implements countdown for sending SMS verification code

This article shares the specific code for the WeC...

Use of Linux file command

1. Command Introduction The file command is used ...

Some tips on deep optimization to improve website access speed

Some tips for deep optimization to improve websit...

How to disable ads in the terminal welcome message in Ubuntu Server

If you are using the latest Ubuntu Server version...

In-depth explanation of Session and Cookie in Tomcat

Preface HTTP is a stateless communication protoco...