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 use CSS to achieve data hotspot effect

The effect is as follows: analyze 1. Here you can...

Using js to implement simple switch light code

Body part: <button>Turn on/off light</bu...

Detailed explanation of the function and usage of DOCTYPE declaration

1. Browser rendering mode and doctype Some web pa...

How MySQL Select Statement is Executed

How is the MySQL Select statement executed? I rec...

Detailed explanation of where the images pulled by docker are stored

The commands pulled by docker are stored in the /...

Detailed explanation of custom instructions for Vue.js source code analysis

Preface In addition to the default built-in direc...

Vue implements drag progress bar

This article example shares the specific code of ...

Introduction to Semantic HTML Tags

In the past few years, DIV+CSS was very popular in...

Detailed explanation of the solution for migrating antd+react projects to vite

Antd+react+webpack is often the standard combinat...

Detailed explanation of Vue filters

<body> <div id="root"> <...

Vue custom directive details

Table of contents 1. Background 2. Local custom i...

How to realize vertical arrangement of text using CSS3

In a recent project, I wanted to align text verti...

How to place large images in a small space on a web page

Original source: www.bamagazine.com There are nar...

Detailed explanation of Vue's keyboard events

Table of contents Common key aliases Key without ...