File and directory operation in Linux the use of Sharp Weapon MV Command analysis

Source: Internet
Author: User
Tags file size

MV command is a command similar to CP, but it is not a replica/copy of a file or directory. No matter what version of Linux you're using, the MV is installed on your Linux system by default.

Usage: MV [options] ... [-t] source file destination file

Or: MV [options] ... Source file ... Directory

Or: MV [options] ...-t directory source files ...

Rename the source file to the destination file, or move the source file to the specified directory.

Parameters that must be used for long options are also required for short options.

--backup[=control] Creates a backup of each existing target file

-B is similar to--backup but does not accept parameters

-F,--force not asked before overwriting

-I,--interactive before overwriting

-N,--no-clobber does not overwrite existing files

If you specify multiple in-I, F,-N, only the last one takes effect.

--strip-trailing-slashes remove the slash on the tail of each source file parameter

-S,--suffix=suffix replaces the usual backup file suffix

-T,--target-directory=directory the source file or directory specified for all parameters

Move to the specified directory

-T,--no-target-directory the target file as normal file processing

-U,--update only in source file newer than target file, or target file

Move only if it does not exist

-V,--verbose detailed display of the steps

--HELP displays this help information and exits

--version display version information and exit

The suffix of the backup file is "~" unless the--suffix option or the Simple_backup_suffix

environment variable specified. Versioning can be done through the--backup option or the Version_control environment

Variable to select. The following are the available variable values:

None, off no backup (even with the--backup option)

Numbered, t backup files plus numbers to sort

Existing, nil if a digital backup file already exists, use a number, or use a normal way to back up

Simple, never always use normal way to back up

Using the sample

1. Moving files

When you move a file, you need to be aware that the source and destination addresses of the files must be different. For example, to move a file_1.txt file from the current directory to another directory, take/home/pungki/as an example, the syntax should read as follows:

The code is as follows:

$ MV File_1.txt/home/pungki/office

As we can see, when we move the Filetxt file, the file1.txt of the previous directory is deleted.

2. Move multiple files

If you want to move multiple files at once, we can put them on one line and separate them in a single space.

Copy Code

The code is as follows:

$ mv File_2.txt File_3.txt File_4.txt/home/pungki/office

You can use wildcards if you have a regular file to follow. For example, in order to remove all files with the. txt extension, we can use the following command:

The code is as follows:

$ MV *.txt/home/pungki/office

3. Move Directory

Unlike the copy command, moving the directory with the MV command is fairly straightforward. Mobile directory You can use the MV command without option. Look at the screenshot below at a glance.

4. Rename a file or directory

We also use MV command to rename files or directories. However, the target location and source location must be the same. The file name must then be different.

Suppose our current directory is/home/pungki/documents, and we want to rename File1.txt to File2.txt. Then the order should read as follows:

The code is as follows:

$ mv File_1.txt File_2.txt

If it is an absolute path, it should look like this:

The code is as follows:

$ mv/home/pungki/documents/file_1.txt/home/pungki/documents/file_2.txt

5. Renaming a directory

The rules for the previous paragraph also apply to the directory. Take a look at this example:

Copy Code

The code is as follows:

$ MV directory_1/directory_2/

6. Print Mobile Information

When you move or rename a large pile of files or directories, you may want to know if your own command was executed successfully without going to the target location to view it. This will take the-v option.

The code is as follows:

$ mv-v *.txt/home/pungki/office

The same method applies to directories.

7. Using interactive mode

When you move a file to another location and that location has exactly the same file, the MV command overwrites the original file. There is generally no hint of this behavior for MV. If you want to generate a hint about overwriting a file, we can use the-i option. Usually the distribution will be prompted by the alias command, using-I as the default option. )

Let's say we want to move file1.txt to/home/pungki/office. At the same time, there are already file1.txt files in the/home/pungki/office directory.

The code is as follows:

$ mv-i File_1.txt/home/pungki/office

This hint will let us know where the file_1.txt exists at the target location. If we press the Y key, then the file will be deleted or not.

8. Use the Update option

The-I option prompts us for a hint about overwriting the file, and-U executes the update only when the source file is newer than the target file. Let's take a look at the following example:

If File1.txt and file2.txt have the following characteristics:

Copy Code

The code is as follows:

File_1.txt has bytes file size and it last modified time is 12:00

File_2.txt has 0 bytes file size and it last modified time is 11:59

We want to move them to the/home/pungki/office directory. * * But the destination address * already has file1.txt and file2.txt.

We use the following command to move File1.txt and File2.txt from the current directory to/home/pungki/office

Copy Code

The code is as follows:

$ MV-UV *.txt/home/pungki/office

You can see that these files have been moved. These files can be moved because their most recent modification timestamp is newer than the files in the/home/pungki/office directory.

9. Do not overwrite any existing files

If the-I option asks if we want to overwrite the file, then the-N option will not allow us to overwrite any existing files.

To continue using the example in 8th, if we replace-u with-n plus the-v option, we will see that no files have been moved to the/home/pungki/office directory.

Copy Code

The code is as follows:

$ MV-VN *.txt/home/pungki/office

10. Create Backup when copying

By default, moving a file overwrites a target file that already exists. But what if we move the wrong file and the target file is covered by a new file? Is there a way to recover the previous file? The answer is yes. We can use the-B option. This option backs up the old file when the new file overwrites the old file. Here we also take the 8th example.

The code is as follows:

$ MV-BV *.txt/home/pungki/office

As seen in the screenshot, a file named File1.txt~ and file2.txt~ appears in the/home/pungki/office directory. The wave symbol (~) means that these files are backup files. From their properties we can see that these files are older than File1.txt and File2.txt.

11. Unconditional coverage of existing documents

You can use the-f option when you want to overwrite existing files or directories anyway. If the-f option and the-I or-n options are also specified, the-F option overrides them-that is, overwrite without any hint, so you know what you are doing when you use this parameter.

The code is as follows:

$ mv-f *.txt/home/pungki/office

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.