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.
- How Does the MV Command Work in Linux?
- Move a Single File or Directory from One Directory to Another
- Move Multiple Files or Directories from One Directory to Another
- Rename a File and Directory
- Prompt Before Overwriting an Existing File
- Do Not Overwrite an Existing File
- Move Only If the Source File Is Newer Than the Destination
- Create a Backup of Existing Destination File
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:
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:
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:
Similarly, to move a directory from one location to another, use the following syntax:
For instance, to move a directory named testdir from the current directory to the ~/Documents directory, the command is as follows:
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:
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:
Similarly, to move multiple directories from one location to another, use the following syntax:
For instance, to move the directories named testdir1, testdir2, and testdir3 from the current directory to the ~/Documents directory, the command would be:
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:
For instance, to rename a file named sample1.txt to sample2.txt, the command would be:
Note: If the file sample2.txt already exists, it is overwritten by the file sample1.txt.
To rename a directory, use the following syntax:
For instance, to rename a directory named testdir1/ to testdir2/, the command would be:
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.
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.
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:
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.
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.
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.
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:
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:
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.