Mv Command in Linux

Last updated: March 24, 2022

Moving files and directories to organize them is an important aspect of the Linux operating system. It is one of the most basic tasks that every user has to perform very often. Linux provides the mv command to move and rename files and directories.

In this tutorial, we learn how to use the Linux mv command to move and rename files and directories.

Mv Command

Mv, also known as move, is used to move files and directories from one location to a new location. This utility can also be used to rename directories and files. It does not require any additional disk space for rename operation.

By default, if the target directory already has a file with the same name, then mv overwrites the existing destination file without any warning. You must use either the -i option to prompt before overwriting, or the -n option to avoid overwriting the destination file.

This command supports moving a single file or directory, as well as multiple files and directories.

You must have write permission on both the source and destination file and directory to use this command.

Syntax

This is the basic syntax of the Linux mv command:

mv [option] source destination

Difference between Cp and Mv Commands

Mv and cp command both works in a similar fashion, but they have the following differences and cannot be used interchangeably:

MVCP
Moves the specified directory or file from one place to another.Creates a copy of the source files or directories in the destination directory.
Removes the source files or directories.Does not impact the source.
Can also be used to rename directories or files.Cannot be used to rename directories or files.
Does not have an option to preserve file or directory metadata.Provides an option to preserve file and directory metadata.

How to Use Mv command in Linux

Use the mv command to move Linux and UNIX files/directories from the source directory to the destination directory. To move a file, specify mv followed by the source and destination. For example,

mv source_file destination_file

This command requires at least two arguments - source file or directory and destination file or directory. If both source and destination files are on the same file system, then this command renames the file. Otherwise, the file or directory is copied from one place to another.  

Mv Command Examples

The following practical mv command examples will show you how this command-line utility works.

Move a File to a Directory

To move a file from one directory to another, pass the file name that you want to move and the target directory name:

mv users.txt docs
move users.txt file to the docs directory

Here we are moving a file from the current directory to the docs directory. Once the move operation is complete, the file is deleted from the source directory.

Move Multiple Files to a Directory

To move multiple files in a single command, pass the list of filenames that you want to move and the destination directory name:

mv dev.txt qa.txt admin.txt docs
move files named dev.txt qa.txt admins.txt to docs directory

Move a Directory to another Directory

Moving a directory to another directory is similar to moving a file. Specify the source and the destination directory names as the Linux mv command arguments:

mv design admin
move directory named design to admin directory

Rename a File using mv

To rename a file, just specify the source file name followed by the new filename. Both source and destination file location should be the same.

In the following example, the file named doc.txt is renamed to api.txt which both reside in the current directory.

mv doc.txt api.txt
rename the file named doc.txt to api.txt in the current directory

When a file is renamed using the mv command in Linux, the inode number of the file remains intact. The inode number is changed only when the file is moved to a different directory.

Rename a Directory using mv

We can also rename a directory like we rename a file. The following command rename the directory named admin to specs where both reside in the same location:

mv admin specs
rename directory named admin to specs

The inode number remains the same for a directory as it does for a file. The inode number changes only when you move a file to a different directory.

Prompt Overwriting Destination File

We can instruct the mv command to prompt before overwriting a file. This option is useful when moving multiple files and we are not sure if a file with the same name already exists in the destination directory.

For example:

mv -i scenario.txt docs
prompt to confirm overwrite when moving a file

Here we can choose to either overwrite an existing file by typing yes or cancel overwriting by typing no.

Backup Existing File

To avoid overwriting the existing file, you can also create a backup of the existing file and then move the source file. The backup file will have a tilde (~) character added to the file name.

mv -b users.txt docs/
move the file named users.txt keeping a backup in the source directory

You can further rename the backup file using this command.

View Output

To see what the mv command is exactly doing, use the -v option:

mv -v apis.txt cases.txt admin/
move operation with verbose using -v option

The command output shows progress for each file in a single line.

Mv Command Options

The following table describes various command-line options of the mv command that you can use with the mv command to perform different operations.

OptionDescription
-iPrompts before overwriting an existing destination file
-bCreates backup of the existing file
-fOverwrites existing read-only files without any prompt
-nPrevents the files from overwriting
-uMoves files in update mode. The file is moved only if the source file is newer than the destination file, or the destination file is missing
-vShows the move operation progress on the terminal window

Conclusion

In this tutorial, we learned how to use the mv command to move and rename directories and files on your Linux or UNIX computer. The mv command deletes the source file or directory, so you must be careful while using this command. This utility is part of the default installation in all Linux distributions, including Ubuntu, Red Hat, Fedora, and Debian.

For more information browse to mv man page or type mv --help on the terminal.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment