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:
- $ mv file_1. TXT /home/pungki/office
MV Command
As we can see, when we move the File_1.txt file, the previous directory's file_1.txt was 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.
- $ mv file_2. TXT file_3. TXT file_4. TXT /home/pungki/office
Move Multiple Files
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:
- $ mv *. TXT /home/pungki/office
Move using pattern
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.
Moving directory
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 file_1.txt to File_2.txt. Then the command should look like this:
- $ mv file_1. TXT file_2. TXT
If it is an absolute path, it should look like this:
- $ MV /home/pungki/Documents/file_1. TXT /home/pungki/Documents/file_2. TXT
Renaming file
5. Renaming a directory
The rules for the previous paragraph also apply to the table of contents. Take a look at this example:
- $ MV directory_1/ directory_2/
Renaming directory
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.
- $ mv -v *. TXT /home/pungki/office
MV with verbose mode
The same method applies to the directory.
MV Directory with verbose mode
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 File_1.txt to/home/pungki/office. At the same time, there are already file_1.txt files in the/home/pungki/office directory.
- $ mv -i file_1. TXT /home/pungki/office
MV with interactive mode
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:
Update only newer
If File_1.txt and file_2.txt have the following characteristics:
- file_1.84 bytes file Size and it lasttime is 12< Span class= "pun" >:00
- File_2. TXT have 0 bytes file size and it last modified time is 11 :
We want to move them to the/home/pungki/office directory. but the target address already has file_1.txt and file_2.txt.
We use the following command to move File_1.txt and File_2.txt from the current directory to the/home/pungki/office
- $ 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.
- $ mv -vn *. TXT /home/pungki/office
No Overwrite
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.
- $ mv -BV *. TXT /home/pungki/office
Backup option
As seen in the/home/pungki/office directory, a file named file_1.txt~ and file_2.txt~ appears. The wave symbol (~) means that these files are backup files. From their properties we can see that these files are older than File_1.txt and File_2.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.
- $ 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.
MV Command in Linux