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

VMware15/16 Detailed steps to unlock VMware and install MacOS

VMware version: VMware-workstation-full-16 VMware...

Implementing a simple Gobang game with native JavaScript

This article shares the specific code for impleme...

The principles and defects of MySQL full-text indexing

MySQL full-text index is a special index that gen...

Example of implementing a virtual list in WeChat Mini Program

Table of contents Preface analyze Initial Renderi...

Native JavaScript to achieve the effect of carousel

This article shares the specific code for JavaScr...

Analysis of the locking mechanism of MySQL database

In the case of concurrent access, non-repeatable ...

Analysis of the advantages and disadvantages of MySQL stored procedures

MySQL version 5.0 began to support stored procedu...

React State state and life cycle implementation method

1. Methods for implementing components:組件名稱首字母必須大...

Basic installation process of mysql5.7.19 under winx64 (details)

1. Download https://dev.mysql.com/downloads/mysql...

CSS3 realizes the website product display effect diagram

This article introduces the effect of website pro...

How to build Nginx image server with Docker

Preface In general development, images are upload...

This article will show you the basics of JavaScript: deep copy and shallow copy

Table of contents Shallow copy Deep Copy Replenis...