Table of Contents
In Linux, to perform the move or rename of a file or directory, we use the mv command. This command performs one of the two features mentioned, depending on usage. In either case, the original filename no longer exists after the operation. mv is used in much the same way as cp command.
Use mv command
To move or rename file or directory “item1” to “item2”.
mv item1 item2
To move one or more items from one directory to another.
mv item... directorymv command useful options
mv command have many options as the same as cp command:
| Options | Meaning |
| -b | make a backup of each existing destination file but does not accept an argument |
| -f | do not prompt before overwriting |
| -i | Before overwriting an existing file, prompt the user for confirmation. If this option is not specified, mv will silently overwrite files. |
| -u | When moving files from one directory to another, only move files that either don’t exist, or are newer than the existing corresponding files in the destination directory. |
| -v | Display informative messages as the move is |
To display all options of mv command, execute command below:
mv --helpSome examples of mv command
Read more: Listing the contents of a directory
Rename file1 to file2.
mv file1 file2
Move file1 and file2 to a directory, you can use for directory as the same way.
mv file1 file2 dir/
Conclusion
This command works very similar to the cp command. The use of the this command should be very careful because it can delete the target file or directory during the use of the command.
(This is an article from my old blog that has been inactive for a long time, I don’t want to throw it away so I will keep it and hope it helps someone).