10 practical examples of Linux-mv commands

Source: Internet
Author: User

10 practical examples of Linux-mv commands
GuideThe mv command is similar to the cp command, but it is not a copy/copy 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, note that the source address and target address of the file must be different. Here is an example. To move the file_1.txt file from the current directory to another directory, take/home/pungki/as an example. The syntax should be as follows:

$ mv file_1.txt /home/pungki/office


As we can see, when we move the file_1.txt file, the file_1.txt file in the previous directory is deleted.

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 files with the .txt 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 want to rename file_1.txtas file_2.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 requires the-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. To generate a message about overwriting a file, we can use the-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 file_1.txt to/home/pungki/office. At the same time, the/home/pungki/office directory already contains the file_1.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 press y, the file will be deleted. Otherwise, no.

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 file_1.txt and file_2.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 file_1.txtand file_2.txt.

We use the following command to move file_1.txt and file_2.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 the-I option asks if we want to overwrite the file, the-n option will not allow us to overwrite any existing file.

Continue to use the example at. If we replace-u with-n and add the-v option, 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 the-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 shown in, the file name is file_1.txt ~ in the/home/pungki/office directory ~ And file_2.txt ~ . The Tilde (~) This means that these files are backup files. We can see from their examples that these files are older than file_1.txtand file_2.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 use man mv or mv -- help to display the mv manual page for more details.

From: https://linux.cn/article-2688-1.html

Address: http://www.linuxprobe.com/linux-move-rename.html


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.