How to Use “mv” Command in Linux [9 Useful Examples]

Brief: In this beginner’s guide, we will discuss some practical examples of the mv command. After following this guide, Linux newbies will be able to rename and move files and directories easily from the command line interface.

Files and directories are the building blocks of the operating system. As regular users, we interact with the files and directories on a daily basis. Often times we rename or move files from one location to another for better organization. Definitely, we can perform this operation using the Graphical User Interface (GUI). However, most Linux users prefer to use the mv command due to its rich functionality.

In this easy-to-understand guide, we will learn the basics of the mv command. As the name suggests, the mv command is used to rename or move files and directories.

In this guide, we will learn about the mv command using practical examples. Beginners can use these examples on a day-to-day basis while working with Linux systems.

So let’s get started.

mv Command Syntax

The syntax of the mv command is similar to other Linux commands. At a high level, it is divided into two parts – options and arguments:

$ mv [OPTIONS] <SOURCE> <DEST>
$ mv [OPTIONS] <SOURCE-1> <SOURCE-2> ... <DIRECTORY>

In the above syntax, the square brackets ([]) represent the optional arguments whereas angular brackets (<>) represent the mandatory arguments.

1. How to Rename a File in Linux

The very basic use of the mv command is to rename a file. So let’s see how to rename a file from the current directory.

First, create a sample file using the touch command:

$ touch file-1.txt

Now, let’s rename the file using the mv command as follows:

$ mv file-1.txt file-2.txt

Finally, verify that the file has been renamed successfully using the ls command:

$ ls -1
Rename File in Linux
Rename File in Linux

2. Enable Verbose Mode in Mv Command

Sometimes, we want to know which files or directories are getting renamed. In such cases, we can use -v option to enable the verbose mode.

To understand this, let’s rename the file using the verbose mode:

$ mv -v file-2.txt file-1.txt 

renamed 'file-2.txt' -> 'file-1.txt'

In the above output, we can see that, now the mv command shows the rename message.

3. How to Rename a Directory in Linux

Similar to files, we can use the mv command to rename the directory. To understand more clearly, first you need to create a new directory with the name src:

$ mkdir src

Now, let’s rename the directory using the following command:

$ mv -v src dst

renamed 'src' -> 'dst'

4. How to Move Multiple Files to the Directory

Many times, we move files to a single directory for better organization. For example, it is very common practice to keep all audio files in a single directory.

Certainly, we can use the mv command multiple times to achieve this. However, the process quickly becomes time-consuming as files increase in number. However, to make it time-efficient, we can use the alternative syntax of the mv command.

Let’s understand by creating a few files and a new directory:

$ touch 1.mp3 2.txt 3.dat
$ mkdir misc

Now, let’s move all these files to the misc directory using the following command:

$ mv -v 1.mp3 2.txt 3.dat misc

renamed '1.mp3' -> 'misc/1.mp3'
renamed '2.txt' -> 'misc/2.txt'
renamed '3.dat' -> 'misc/3.dat'
Move Multiple Files in Linux
Move Multiple Files in Linux

It is important to note that, to use this alternative syntax the directory must be present already and it must be the last argument of the command.

5. How to Move Multiple Directories in Linux

Just like the files, we can use the mv command to move multiple directories at once. Let’s understand this with a simple example.

First, create a few directories using the mkdir command:

$ mkdir dir-1 dir-2 dir-3 dir-4

Now, let’s move all these directories to the dir-4 directory:

$ mv -v dir-1 dir-2 dir-3 dir-4
Move Multiple Directories in Linux
Move Multiple Directories in Linux

In the above output, we can see that we were able to move all directories.

6. How to Avoid Overwriting Files in Linux

By default, the mv command overwrites the destination file. Sometimes, we want to disable this default behavior to avoid data loss. In such cases, we can use the -n option.

To understand this, first, create a sample file:

$ touch file-2.txt

Now, let’s try to overwrite it using the following command:

$ mv -v -n file-1.txt file-2.txt

Here, we can see that verbose mode didn’t show any message. This indicates that file-2.txt hasn’t been overwritten.

7. How to Overwrite Files Interactively in Linux

In the previous example, we saw how to disable file overwriting. However, sometimes we want to overwrite files in a safe manner.

In such cases, we can use the mv command in an interactive mode. In this mode, the mv command shows the warning message and waits for the user’s confirmation before overwriting the file.

Now, let’s try to overwrite the file-2.txt file in an interactive mode:

$ mv -v -i file-1.txt file-2.txt

mv: overwrite ‘file-2.txt’?

In the above output, we can see that the command is waiting for the user’s confirmation. Just like other Linux commands, we can use 'y' to continue or 'n' to abort the operation.

8. Overwrite File Only When the Source Is Newer

In the previous example, we saw how to overwrite files using an interactive mode, but this method is not practical when we want to overwrite a large number of files.

However, we should not perform the overwrite operation without verification, because a user might overwrite the newer file accidentally. In such cases, we can use the -u option to perform the move operation only if the source is newer than the destination.

To understand this, let’s update the timestamp of the source file:

$ touch -t 201912301000 file-1.txt 
$ ls -l file-1.txt 

In the above example, we have used the -t option of the touch command to set the older timestamp on the file-1.txt file.

Next, let’s update the timestamp of the destination file to the current time:

$ touch file-2.txt

Finally, let’s try to perform overwrite the destination file using the -u option:

$ mv -v -u file-1.txt file-2.txt
Overwrite File When Source File Is Newer
Overwrite File When Source File Is Newer

In the above output, we can see that verbose mode didn’t show any message. This indicates that file-2.txt hasn’t been overwritten.

9. How to Create a Backup Before Overwriting Files

In the previous few examples, we saw how to overwrite the destination file in a safer way. The mv command provides one more option, which allows us to specify a backup policy using the --backup option, which takes a backup of the destination file before overwriting it.

$ touch file-1.txt file-2.txt
$ mv --backup=numbered -v file-1.txt file-2.txt

Here, we have used the numbered backup policy, which uses incremental numbers in the backup file names.

To understand this, let’s execute these two commands a few more times and check the output:

$ touch file-1.txt file-2.txt
$ mv --backup=numbered -v file-1.txt file-2.txt

$ touch file-1.txt file-2.txt
$ mv --backup=numbered -v file-1.txt file-2.txt

$ touch file-1.txt file-2.txt
$ mv --backup=numbered -v file-1.txt file-2.txt
Create Backup Before Overwriting Files
Create Backup Before Overwriting Files

In this beginner’s guide, we discussed how to rename and move files as well as directories using the mv command. Linux newbies can refer to these examples in day-to-day life while working with Linux systems.

Do you know of any other best example of the mv command in Linux? Let us know your views in the comments below.

Tutorial Feedback...
Was this article helpful? If you don't find this article helpful or found some outdated info, issue or a typo, do post your valuable feedback or suggestions in the comments to help improve this article...

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

Got something to say? Join the discussion.

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.