Beginners Guide for mv Command on Linux

Linux TLDR
Last Updated:
Reading time: 3 minutes

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

DescriptionMv (Move Files and Directories)
Difficulty LevelLow
Root or Sudo PrivilegesNo
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisitesmv
Internet RequiredNo

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.

OptionsDescription
-fOverwrite the file without confirmation.
-iPrompt for confirmation before overwriting the file.
-nAvoid overwriting existing files.
-bIf file “A” is copied as file “B” and file “B” already exists in the same location, then it will rename it to “B~“.
-uOverwrite if the target file is old or doesn’t exist.
-vPrint the file while being moved.
💡
While moving the file, make sure the same name file does not exist in the destination; otherwise, it will be replaced. To avoid this, you can use the “-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:

Rename file using the mv command

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 file to different directory

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 file to different directory with new name

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:

Moving multiple files to new directory

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:

Ask for confirmation before overwriting the file

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:

Forcefully overriding the write-protected file

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:

Moving only the unique file leaving same file untouched

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:

Make the copy of already existing file

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:

Printing the filename while they being moved

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.