In UNIX/Linux systems, the mv command shipped with the operating system is usually used for the following purposes:
- Moving files from source to destination directory.
- Rename the file or directory name.
If you’re familiar with the cp command, you can use the same techniques to use the mv command.
Even though you can use this article to learn how to use this command from the most basic to the most advanced levels.
|Description||Mv (Move Files and Directories)|
|Root or Sudo Privileges||No|
|Host System and Architecture||Ubuntu 22.10 (x64)|
|OS Compatibility||Ubuntu, Manjaro, Fedora, etc.|
Syntax of the mv Command
The mv command takes three arguments: one is the option, and the other two are the source and destination paths.
$ mv [OPTION] [SOURCE] [DESTINATION]
The following are some known options that you can use with this command.
|Overwrite the file without confirmation.|
|Prompt for confirmation before overwriting the file.|
|Avoid overwriting existing files.|
|If file “|
|Overwrite if the target file is old or doesn’t exist.|
|Print the file while being moved.|
-i” flag to get confirmation while overwriting the file.
Rename File Using the mv Command
The first thing that you can do with the mv command is rename the file or directory.
For example, if you have “
file.txt” and want to rename it “
my_file.txt“, then you can simply pass both of the filenames as arguments to the mv command.
$ mv file.txt my_file.txt
Moving a File to an Arbitrary Location
Unlike the previous command, you can either give a relative or an absolute path to the destination directory to move the file into it.
For example, the following command will move “
my_file.txt” to the “
$ mv my_file.txt ~/Documents/
Moving a File to an Arbitrary Location with a New Name
You can specify the new name for your file while specifying the full path of the destination directory.
$ mv my_file.txt ~/Documents/file.txt
Moving Multiple Files to an Arbitrary Location
You can move multiple files into a common destination directory by specifying relative or absolute paths and using the space as a separator.
$ mv file1.txt file2.txt file3.txt ~/Documents/
If files sharing the common sequence, like “
file3.txt“, .., “
file5.txt” and exist in the same directory, then instead of separately specifying their names, you can use the wildcard.
The following are a few examples of wildcard usage:
$ mv *.txt ~/Documents/ #Moving all the text files from source to destination directory $ mv *.jpg ~/Documents/ #Moving all the jpg files from source to destination directory $ mv *.mp4 ~/Documents/ #Moving all the mp4 files from source to destination directory
Asking for User Confirmation Before Overwriting the File
If the source file (or files with the same name) already exists in the destination directory, it will be replaced without a warning. This can be avoided if you use the “
When you use this flag, each time the common file between the source and destination is found, instead of replacing it, the mv command will ask for confirmation.
$ mv -i *.txt ~/Documents/
Forcefully Overriding the Write-Protected Files
When you overwrite a write-protected file with another, you will be asked if you want to overwrite it. You can avoid this by using the “
-f” flag to forcefully overwrite the write-protected files.
In the following command, we are trying to overwrite the “
file1.txt” write-protected file with the “
$ mv -f file2.txt file1.txt
Only Unique Files Should be Moved
Using the “
-n” flag will only move the unique files from the source that do not exist in the destination directory.
$ mv -n *.txt ~/Documents/
Backup Any Existing Files
Unlike the previous command, if you want to move a file that exists in both locations and you do not want to replace or override it in the destination directory, then use the “
For example, the “
file1.txt” file exists in both the source and destination directories, so when you use the mentioned flag, the “
~” suffix will be added at the end of the “
file1.txt~” in the destination directory.
$ mv -b *.txt ~/Documents/
Printing the File Names While Being Moved
-v” flag will enable the verbose mode that will print each file and directory name while they are being moved from the source to the destination directory.
$ mv -v *.txt ~/Documents/
The syntax for the cp and mv commands is nearly identical, and users who are familiar with cp will notice that there are only a few differences between the two commands.
I expect you to tell me those differences in the comment section. I’m waiting for your comments!