Familiar with the copy command, the next related command is the mv command. When you want to move a file from one location to another and don't want to copy it, the mv command is the first choice for this task.
First met the mv command
The mv command is a command similar to cp, but it is not a copy/copy of a file or directory. Regardless of the version of Linux you are using, mv is installed by default on your Linux system. Take a look at some examples of mv commands in everyday operations.
1. Move files
Note that when moving files, the source and destination addresses of the files must be different. Here is an example. I want 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 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 with spaces.
$ mv file_2.txt file_3.txt file_4.txt /home/pungki/office
If your files are regular, then you can use wildcards. For example, to remove all files with a .txt extension, we can use the following command:
$ mv *.txt /home/pungki/office
3. Mobile directory
Unlike copy commands, moving directories with the mv command is fairly straightforward. Moving directories You can use the mv command without options. Look at the screenshot below to see 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. Then the file name must be different.
Suppose we are currently in the directory /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
5. Rename the directory
The rules of the previous paragraph also apply to the directory. Please see this example:
$ mv directory_1/ directory_2/
6. Print mobile information
When you move or rename a bunch of files or directories, you may want to know if your own commands have been executed successfully without going to the target location. This will use 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 a different location, and that location happens to have the same file, the mv command will overwrite the original file. There is usually no hint for this behavior of mv. If you want to generate a hint about overwriting a file, we can use the -i option. (Annotation: Usually the release will use the alias command, with -i as the default option, so there will be prompts.)
Suppose we want to move file_1.txt to /home/pungki/office. At the same time, there is already a file_1.txt file in the /home/pungki/office directory.
$ mv -i file_1.txt /home/pungki/office
This prompt will let us know the existence of file_1.txt at the target location. If we press y, then that file will be deleted, otherwise it will not.
8. Use the update option
The -i option prompts us for tips on overwriting files, while -u performs updates only when the source file is newer than the target file. Let us take a look at the following example:
If file_1.txt and file_2.txt have the following characteristics:
File_1.txt has84bytes file size andit last modified time is12:00
File_2.txt has0bytes file size andit last modified time is11:59
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 /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 recent modification timestamps are newer than those 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 files.
Continue to use the example in point 8. If we change -u to -n with the -v option, we will see that no files have been moved to the /home/pungki/office directory.
$ mv -vn *.txt /home/pungki/office
10. Create a backup when copying
By default, moving files will overwrite existing target files. But if we move the wrong file and the target file has been overwritten by the new file, what should we do then? Is there a way to recover the previous file? The answer is yes. We can use the -b option. This option will back up old files when new files are overwritten. Here we also take the 8th point as an example.
$ mv -bv *.txt /home/pungki/office
As seen in the screenshot, files named file_1.txt~ and file_2.txt~ appear in the /home/pungki/office directory. That tilde (~) means that these files are backup files. As we can see from their properties, these files are older than file_1.txt and file_2.txt.
11. Unconditionally overwrite existing files
(Annotation: This section is supplemented by the translator, the original article misses this important option)
When you want to overwrite an existing file or directory anyway, you can use the -f option. If you specify both the -f option and the -i or -n option, the -f option overrides them—that is, overwrites without any hints, so when you use this parameter, you know what you are doing.
$ mv -f *.txt /home/pungki/office
to sum up
Moving files and directory commands is a basic command for Linux systems. Usually you can display the mv man page via man mv or mv –help for more details.
T Copper Tube Terminals,Non-Insulated Pin-Shaped Naked Terminal,Copper Cable Lugs Terminals,Insulated Fork Cable Spade Terminal
Taixing Longyi Terminals Co.,Ltd. , https://www.lycopperlugs.com