mv Command in Linux

The mv command is one of Linux's built-in commands, and its primary function is to move files and directories. However, the mv command's function is not limited to moving files; it also performs a variety of other tasks. In Linux, mv stands for move.

In this tutorial, we will learn how to move files and directories in Linux using mv Command.

How to use mv command

The mv command is used to move files and directories from one location to another. When moving the mv command delete the original file or directory. But it will wait for the files/directories to be completely moved to end the action. When moving a file to a destination, if the destination already has a file with the same name then it will get overwritten.

You may also use the mv command to rename the name of the file and directory.  If both filenames or directories are on the same filesystem, results in a rename.

The mv won't rename a directory to another directory if the target directory contains the same directory with files. "Directory not empty" messages come due to this reason.

The basic syntax of the mv command is as follows.

Syntax:

$ mv [Options] Source Destination

[Options] refers to the various mv command options. The Source might be a single file or directory or multiple files or directories. At the Destination, a single file or directory can be specified.

You must have write permission on both the Source and the Destination to move a file or directory, or you will receive a permission denied error.

  • When the Source consists of numerous files or directories, the Destination must be a directory. The files or directories on the Source are moved to the Destination directory.

For example, if we need to move the text1.txt file to the /dir1 directory, the syntax would be as follows.

$ mv text1.txt /dir1
  • When the Source consists of numerous files or directories, the Destination must be a directory. The files or directories on the Source are moved to the Destination directory.
  • When the Source is a single file and the Destination is a directory, the file is moved to the Destination directory.
  • When the Source is a single file and the Destination is a filename, the Source file is renamed to the Destination filename.
  • The Source is a directory, as is the Destination, but the Destination directory does not exist. In this situation, the Source directory will be renamed as the Destination directory.  If the Destination directory already exists, the Source directory is moved to it.

How to move multiple files or directories

To move multiple files or directories, you must first specify the Source file names and the Destination directory.

For example, to move text1, text2, and text3 files to the dir1 directory, use the following syntax.

$ mv text1 text2 text3 dir1

The mv command also supports pattern matching For example, you could use the following syntax to move all text files to the ~/Documents directory.

$ mv *.txt ~/Documents

How to rename a file or directory

You can use the mv command to rename a file or a directory.

For example, to rename a file from text1.txt to text2.txt, use the following syntax.

$ mv text1.txt text2.txt

For example, if we need to move the dir1 directory to the dir2 directory, the syntax would be as follows. However, if the dir2 does not exist then the dir1 directory is renamed as dir2.

$ mv dir1 dir2

mv command Options

The mv command provides various options for specific purposes. Some of the useful mv command options are:

Prompt Before Overwriting

If the Destination file or directory already exists, it is overwritten by default. The -i option can be used to display a confirmation prompt. The following syntax is used to display prompts before overwriting.

$ mv -i Source Destination

While attempting to move file1 to the dir1 directory when file1 already exists in dir1, a prompt will appear as: 

mv: overwrite 'dir1/file1'?

If you want to overwrite type y or Y.

Not Overwrite Existing file

To never overwrite an existing file, use the -n option along with the mv command, as shown in the following syntax.

$ mv -n Source Destination

When attempting to move file1 to the dir1 directory, if file1 already exists, the command will do nothing; otherwise, the file will be moved to dir1.

Force Overwriting

The following syntax can be used to force a file to overwrite without displaying a prompt message.

$ mv -f Source Destination

Backup Files

To back up a file in an existing destination file, use the -b command-line option. The following syntax can be used to back up a file.

$ mv -b Source Destination

A tilde (~) will show in the backed-up file with the same name as the original.

Conclusion

In this tutorial, we learned how to use the mv command in Linux. For more information refer to man mv.

Leave a Comment