How to Use the mv Command to Move Files & Directories in Linux

Featured Image

The mv command in Linux stands for move. It is used to move one or more files or directories from one location to another within the file system. The command usually works without prompting, but we will show you how you can prompt before renaming or moving a file.

In this article, we will discuss the mv command in Linux, its options, and some common use cases.

How Does the mv Command Works

The main operations that the mv command performs include:

  • Rename a file or directory.
  • Move one file or group of files to another directory.

We will elaborate on how the mv command works in the following example. Suppose we have four files named: one.txt, two.txt, three.txt, and four.txt.

  • If you specify one file as source, and another as a destination, then the file will be renamed.
  • If you specify a source file and destination directory, then the file will be moved to the directory.
    • The destination file will be automatically created if it does not exist already. The mv command will simply rename the source file to the destination (new file).
    • If the destination file already exists, then it will be overwritten and the source file will be deleted.
  • If you provide multiple files and directories as sources, then the destination must be a directory, and all source files and directories will be moved to the destination.
Important: The mv command DOES NOT prompt before overwriting an existing file.

Syntax

mv [options] source destination

mv Command Options

mv Command Option Description
–help Display the command help and exit.
–version Display version information and exit.
-f Prevent prompt before overwriting.
-i Prompt before overwriting
-u Only moves files that do not exist.
-v Explain what is currently happening.
-n Prevents an existing file from being overwritten.
-b Back up an existing file that will be overwritten.

Rename a File using mv Command

mv command can be used to rename a file or move one file’s content to another already existing file. We will discuss both examples down below.

Rename a File Where the Destination File Does Not Exist

To rename file one.txt to file random.txt that does not exist:

ls
Output
one.txt two.txt three.txt four.txt
mv one.txt random.txt
ls
Output
two.txt three.txt four.txt random.txt

In the above example, file one.txt is deleted and replaced by a new file called random.txt.

Rename a File Where the Destination File Exists

We’ll rename file one.txt to an existing file random.txt:

ls
Output
one.txt two.txt three.txt four.txt random.txt
cat one.txt
Output
This is the one.txt file
cat random.txt
Output
This is the random.txt file
mv one.txt random.txt
ls
Output
two.txt three.txt four.txt random.txt
cat random.txt
Output
This is the one.txt file

In the above example, we move and rename the one.txt file, overwriting the random.txt file.

Move a File to Another Directory using the mv Command

As the name indicates, the mv command can move one or more files from one directory to another.

Moving a File to Another Directory and Change the Name

Moving a file from one directory to another and giving it a new name deletes it from the source location. To move a file from one directory to another and give it a new name:

mv one.txt newdir/two.txt

Move a File to Another Directory While Keeping the Same Name

To move a file from one directory to another, type the file name after mv followed by the directory name.

The following command will move file1 to a directory called newdir:

mv file1 a_dir

It has the same result as:

mv file1 a_dir/file1

Move Multiple Files into a Directory using the mv Command

To move multiple files from one directory to another, type the file names separated by spaces after the mv command with the directory name at the end:

mv file1 file2 file3 a_dir

You can also use the wildcard character to select all files of a specific type ( for example, TXT or PDF) and move them to another directory. For that, type *.filetype after the mv command:

mv *.txt a_dir

Rename a Directory using mv Command

Alongside renaming files, the mv command can also be used to rename directories. The syntax is the same as with files. We just move the directory under a different name, in the same directory it’s currently located in.

The following command will rename the dir directory to newdir:

mv a_dir newdir

Move Directory using mv Command

Moving a directory into a different directory is the same as moving files.

The following command will move the a_dir directory into the work_dir directory.

mv a_dir work_dir

OR

mv a_dir work_dir/a_dir

Prompt before Overwriting a File

By default, the mv command does not show a prompt before overwriting a file, which can lead to several issues.

To avoid overwriting, add the -i option after the mv command to show a prompt before overwriting:

mv -i file1.txt file2.txt
Prompt
mv: overwrite 'file2'? y

Press y to continue the overwrite or n to abort.

Avoid Overwrite an Existing File

You can use the -i option to show prompt before overwriting, but if you want to avoid overwriting an existing file, then use the -n option with the mv command. The -n option causes the mv command to ignore anything that may overwrite an existing file.

Say you’re moving a number of files into a directory and you’d waste time checking if some files with the same name already exist in that directory.

In this case you can use the -n option, which tells mv to do nothing if the file already exists:

mv -n file1 file2 file3 some_dir

Move Only the Files Newer than the Destination

You can also choose to move files that are newer than the destination with the -u option. With the -u option, the file will not move if it’s not newer than the destination.

In the following example, we have file file1, which was created on 11th October 2021, and there is also a a_dir/file1, which was created on 12th October 2021. Using the -u option will mean that only the newer files will be moved.

Since file1 is older than a_dir/file1, it will not get overwritten.

mv -u file1 file2 a_dir

Backup an Existing File

If you want to backup a that’s about to get overwritten, then you can use -b option. The -b option will create a backup file with the same name and a tilde character appended to it.

mv -b file1.txt file2.txt
ls
Output
file2.txt file2.txt~

Conclusion

In this tutorial we explained how to use the mv command and some of its various options to move or rename files and directories. To learn more, visit the mv command man page. If you encountered any issues or have any questions then feel free to leave a comment and we’ll get back to you as soon as we can.

0 Shares:
Subscribe
Notify of
guest
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
You May Also Like