The mv command, short for move, as the name suggests, means to move files. In fact, it is equivalent to a cut operation, and the cp command mentioned above is copy and paste. There is no need to say more about the difference between the two.
basic use
use format
mv source file object file
I have a directory and a readme.txt file in my root directory, now I want to move the readme.txt file to the a directory:
// View a directory structure tree a // result a └── b ├── c │ └── 123.txt └── test.txt
move:
mv readme.txt ./a
verify:
tree a // result a ├── b │ ├── c │ │ └── 123.txt │ └── test.txt └── readme.txt
At this point, the readme.txt file in the root directory is also gone, and you will understand when you think about the cutting operation.
Rename
The mv command can also be used to rename, which sounds strange. What does the move operation have to do with renaming? In fact, it’s not difficult to understand the cp command. During the move, change the name without changing the location, so it’s not quite the same. was renamed.
// Enter a directory cd a // General readme.txt Heavy naming purpose demo.txt mv readme.txt demo.txt //tree . ├── b │ ├── c │ │ └── 123.txt │ └── test.txt └── demo.txt
Override Tips
Now suppose a scenario, in the a directory, we need to move the test.txt file in the b directory to the a directory and rename it to demo.txt, we know that there is already a demo.txt file in the a directory, like this The operation will overwrite the original demo.txt file, which can be tested in your own environment. By default, my environment will have an overwrite prompt:
mv b/test.txt ./demo.txt mv: overwrite ‘./demo.txt’?
For security, there should be prompts. mv provides an option -i to ensure prompts before each overwrite, which is the same as the cp command. Regardless of whether the environment has any prompts by default, try to add -i as much as possible:
mv -i b/test.txt ./demo.txt mv: overwrite ‘./demo.txt’? y tree . ├── b │ └── c │ └── 123.txt └── demo.txt