Linux Commands

Linux mv Command Examples

The mv command is one of the basic Linux commands that is used to move the files and directories from one location to another. It is also used to rename the files and directories. The mv command is available on all Linux distributions by default.

In this post, we will show you the practical examples of the mv command in Linux. We will also show the command line options that are used with it.

Let’s start with this guide.

How Does the MV Command Work in Linux?

Linux offers the “mv” command to move the files and directories from one location to another. Still, you can use the “mv” command to rename a file or folder.

To move a single file to another directory, use the following syntax:

$ mv file/directory

The move command has different options that you can utilize when moving the files and directories. Let us have different examples to understand how to use it to move the files:

Options Description
-v  It overwrites the existing file or directory.
-i It prompts whether to overwrite the existing file or not.
-f It is used to forcefully overwrite the existing file without prompting.
-n The files remain intact and your file will not be moved to the destination directory.
-b It creates a backup file in the destination directory.

To explore more options of the “rm” command, users can utilize the “help” utility:

$ rm --help

The examples of the Linux mv command are as follows:

Example 1: Move a Single File or Directory from One Directory to Another

To move a single file or directory from one location to another, you need to tell “mv” where the file is and where to move it. Note that when you move a file to another directory where another file with the same name already exists, it overwrites the existing file.

For instance, to move a file named sample1.txt from the current directory to the ~/Document directory, the command is as follows:

$ mv sample1.txt ~/Documents/

 

Similarly, to move a directory from one location to another, use the following syntax:

$ mv directory1 directory2

For instance, to move a directory named testdir from the current directory to the ~/Documents directory, the command is as follows:

$ mv testdir/ ~/Documents/

 

Example 2: Move Multiple Files or Directories from One Directory to Another

To move multiple files from one directory to another, use the following syntax:

$ mv file1 file2 file3 dir1

For instance, to move the files named sample1.txt, sample2.txt, and sample3.txt from the current directory to the ~/Document directory, the command would be:

$ mv sample1.txt sample2.txt sample3.txt ~/Documents/

Similarly, to move multiple directories from one location to another, use the following syntax:

$ mv directory1 directory2 directory3 destination_directory

For instance, to move the directories named testdir1, testdir2, and testdir3 from the current directory to the ~/Documents directory, the command would be:

$ mv testdir1 testdir2 testdir3 ~/Documents/

Example 3: Rename a File and Directory

With the mv command, you can also rename a file or directory. To rename a file, use the following syntax:

$ mv file1 file2

For instance, to rename a file named sample1.txt to sample2.txt, the command would be:

$ mv sample1.txt sample2.txt

Note: If the file sample2.txt already exists, it is overwritten by the file sample1.txt.

To rename a directory, use the following syntax:

$ mv dircetory1 directory2

For instance, to rename a directory named testdir1/ to testdir2/, the command would be:

$ mv testdir1/ testdir2/

Example 4: Prompt Before Overwriting an Existing File

When you move a file to another directory where another file with the same name already exists, it overwrites the existing file at the destination directory by default. If you want, you can tell the mv command to ask before overwriting the existing file using the mv command’s -i option.

$ mv -i file1 directory

If you want to move the sample.txt file to ~/Documents directory which already contains a file named sample.txt, the -i option prompts you before overwriting the file.

$ mv -i sample.txt ~/Documents/

If you want to overwrite the file, hit y. Otherwise, it is canceled.

 

Example 5: Do Not Overwrite an Existing File

If you want, you can tell the mv command to never overwrite an existing file at the destination using the -n option as follows:

$ mv -n file1 directory

For instance, you want to move the sample.txt file to ~/Documents directory which already contains a file named sample.txt. If you use the -n option, it prevents the file from being overwritten.

$ mv -n sample.txt ~/Documents/

Example 6: Move Only If the Source File Is Newer Than the Destination

When moving a file to another directory that already contains the same file, you can tell the mv command to update the file at the destination only if the source file is newer than the file at the destination.

$ mv -u file1 directory

We have a sample2.txt file that exists in both the current directory and the ~/Documents directory. The sample.txt file that exists in the current directory is newer than the sample.txt file that exists in the ~/Documents directory as can be seen in the following screenshot.

Now, if we use the mv command -u option, the file at the destination is updated as the source file is more recent.

$ mv -u sample2.txt ~/Documents/

 

Example 7: Create a Backup of Existing Destination File

To avoid the already existing destination file being overwritten, you can also create its backup at the destination directory using the mv command’s -b option:

$ mv -b file1 directory

We have a test.txt file that exists in both the current directory and the ~/Documents directory. Before the test.txt file at the destination directory gets overwritten by the source file, you can create its backup using the -b option as follows:

$ mv -b test.txt ~/Documents/

It creates the backup file at the destination directory with the same name but with a tilde (~) appended to it.

 

That is all from the “mv” command.

Conclusion

The “mv” command offers more flexibility and is recommended to move the files in directories. We covered the different examples of using the “mv” command to move a file from one directory to another in the same or different location.

 

About the author

Syed Minhal Abbas

I hold a master's degree in computer science and work as an academic researcher. I am eager to read about new technologies and share them with the rest of the world.