Linux commands: MV command file move and rename

Source: Internet
Author: User

When you want to move a file from one location to another and do not want to copy it, the MV command is the first choice to complete the task.

Initial knowledge of MV command

The MV command is a command similar to CP, but it does not create replicas/replicas of files or directories. No matter what version of the Linux system you are using, the MV is installed on your Linux system by default. Take a look at some examples of the MV command in daily operations.

1. Move files

When moving files, it is important to note that the source and destination addresses of the files must be different. Here is an example where you want to move the File_1.txt file from the current directory to a different directory, in/home/pungki/as an example, the syntax should be as follows:

    1. $ 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 in one line and separate them with a space.

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

You can use wildcards if your files are regularly traceable. For example, in order to remove all files with the. txt extension, we can use the following command:

    1. $ MV *.txt/home/pungki/office

3. Moving the Directory

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

4. Renaming a file or directory

We also use the MV command to rename a file or directory. However, the target location and source location must be the same. Then the file name must be different.

Suppose we currently have a directory of/home/pungki/documents, and we want to rename File1.txt to File2.txt. Then the command should look like this:

    1. $ mv File_1.txt File_2.txt

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

    1. $ 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 table of contents. Take a look at this example:

    1. $ MV directory_1/directory_2/

6. Print Mobile Information

When you move or rename a large stack of files or directories, you may want to know whether your own commands have been executed successfully without going to the target location to view them. This will use the-v option.

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

The same method applies to the directory.

7. Using interactive mode

When you move the file to a different location, and that location happens to have 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, which will be 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.

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

This tip will let us know where the file_1.txt exists at the target location. If we press the Y key, then that file will be deleted, otherwise it won't.

8. Use the Update option

The-I option prompts us for hints about overwriting the file, and-u only performs the update 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:

    1. File_1.txt have bytes file size and it last modified time is 12:00
    2. File_2.txt have 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 target 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 the/home/pungki/office

    1. $ 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 us if we want to overwrite the file, then the-N option will not allow us to overwrite any existing files.

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

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

10. Create a 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 has been overwritten by the new file? Is there a way to recover a previous file? The answer is yes. We can use the-B option. This option will make a backup of the old file when the new file overwrites the old file. Here we also take the 8th as an example.

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

As seen in the/home/pungki/office directory, a file named File1.txt~ and file2.txt~ appears. 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. Unconditionally overwrite files that already exist

(This section is a translator's supplement, the original text omitted this important option)

You can use the-f option when you want to overwrite files or directories that already exist anyway. If the-f option and the-I or-n option are specified at the same time, the-f option overrides them-that is, they are overwritten without prompting, so when you use this parameter, you know what you are doing.

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

Summarize

The Move files and directories command is a basic command of the Linux system. Usually you can display the MV's hand albums via the man MV or MV--help for more details.

10 practical examples of turning from the Linux MV command

Linux commands: MV command file move and rename

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.