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.
Tutorial Details
Description | Mv (Move Files and Directories) |
Difficulty Level | Low |
Root or Sudo Privileges | No |
OS Compatibility | Ubuntu, Manjaro, Fedora, etc. |
Prerequisites | mv |
Internet Required | No |
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.
Options | Description |
---|---|
-f | Overwrite the file without confirmation. |
-i | Prompt for confirmation before overwriting the file. |
-n | Avoid overwriting existing files. |
-b | If file “A ” is copied as file “B ” and file “B ” already exists in the same location, then it will rename it to “B~ “. |
-u | Overwrite if the target file is old or doesn’t exist. |
-v | 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
Output:
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 “~/Documents
” directory.
$ mv my_file.txt ~/Documents/
Output:
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
Output:
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/
Output:
If files sharing the common sequence, like “file1.txt
“, “file2.txt
“, “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 “-i
” flag.
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/
Output:
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 “file2.txt
” file.
$ mv -f file2.txt file1.txt
Output:
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/
Output:
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 “-b
” flag.
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/
Output:
Printing the File Names While Being Moved
The “-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/
Output:
Conclusion
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!
Join The Conversation
Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.