A practical example of mv command under CentOS

Source: Internet
Author: User

A practical example of mv command under CentOS

Familiar with the Copy command, the next command is the mv command. If you want to move an object from one location to another and do not want to copy itThe mv command is the first choice to complete this task.

Early knowledge of mv commands

The mv command isCp commands, but itIt is not a copy or replica of a file or directory. No matter what version of Linux you are using, mv is installed on your Linux system by default. Let's take a look at some examples of Music Video commands in daily operations.

1. Move files

When moving a file, you must note the source and target addresses of the file.Must be different. Here is an example.The file_1.txt file is moved from the current directory to another directoryFor example, the syntax of/home/pungki/is as follows:

$ mv file_1.txt /home/pungki/office

As we can see, when we move the filetxt fileDeleted.

2. Move multiple files

If you want to move multiple files at a time, we can put them in one row and separate them with spaces.

$ mv file_2.txt file_3.txt file_4.txt /home/pungki/office

If your files are regular, you can use wildcards. For example, to remove all. Txt is a file with the extension. We can use the following command:

$ mv *.txt /home/pungki/office

3. Move the Directory

Unlike the Copy command, using the mv command to move the directory is quite direct. You can use the mv command without the option to move the directory. You can see the following at a glance.

4. rename 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. The file names must be different.

Assume that the current directory is/Home/pungki/Documents, and we wantFile1.txt is renamed as file2.txt. The command should be as follows:

$ mv file_1.txt file_2.txt

If it is an absolute path, it should be like the following:

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

5. Rename the Directory

The preceding rule applies to directories. See this example:

$ mv directory_1/ directory_2/

6. Print the Mobile Information

When you move or rename a large number of files or directories, you may want to know whether your command has been successfully executed without checking the target location. This is necessary-V option.

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

This method also applies to directories.

7. Use Interactive Mode

When you move the file to another location where the same file exists, the mv command overwrites the original file. This line of music videos is generally not prompted. If you want to generate a message about overwriting a file, you can use-I option. (Note: The release usually uses the-I as the default option through the alias command, so a prompt is displayed .)

Suppose we want to move file1.txt to/home/pungki/office. At the same time, the/home/pungki/office directory already contains the file1.txt file.

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

This prompt will allow us to know the target location for the existence of file_1.txt. If we pressKey y, the file will be deleted, otherwise it will not.

8. Use the update Option

-I option will prompt us about overwriting the file, while-u will only execute the update when the source file is newer than the target file. Let's take a look at the following example:

Assume that file1.txt and file2.txt have the following features:

File_1.txt has 84 bytes file size and it last modified time is 12:00File_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 target address * already has file1.txtand file2.txt.

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

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

The files are moved. These files can be moved because their latest modification timestamp is newer than the files in the/home/pungki/office directory.

9. Do not overwrite any existing files

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

Continue to use the example in, if weReplace-u with-n and add-V option, then we will see that no file is moved to the/home/pungki/office directory.

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

10. Create a backup during replication

By default, moving a file overwrites an existing target file. But what should we do if we move the wrong file and the target file has been overwritten by the new file?Is there any way to restore the previous file? The answer is yes. We can use-B option. This option backs up the old file when the new file overwrites the old one. Here we also take the 8th point as an example.

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

As you can see in the/home/pungki/office directoryFile1.txt ~ And file2.txt ~ . The Tilde (~) This means that these files are backup files. We can see from their examples that these files are older than file1.txt and file2.txt.

11. Unconditionally overwrite existing files

This section is added by the translator. The original Article misses this important option)

If you want to overwrite an existing file or directory, you can use the-f option. If both the-f option and-I or-n option are specified, the-f option overwrites them-that is, they are overwritten without any prompts. Therefore, when you use this parameter, know what you are doing.

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

Commands for moving files and directories are basic commands in Linux. Generally, you can useMan mv orMv -- help displays the mv manual page for more details.

Via: http://linoxide.com/linux-command/mv-command-linux/

Related Article

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.