The mv command in Linux is a widely used file/folder management command using Terminal. This command focuses on moving and renaming files. In this article, you will learn the basics, features, and multiple examples of using the mv command.
A. Description
The mv command allows changing the location of a file or folder. So, naturally, this command is used to move files. But changing the location helps in renaming the file as well just by changing the file name. The mv command is very useful for managing files in a UNIX or UNIX-like system.
B. Syntax
The syntax for the mv command can vary. There can be one or more options, one or more source paths, and a single destination path.
mv [OPTION]... SOURCE... DESTINATION
C. Options
Many useful options are available for the mv command. Here you’ll find some of the useful options available that will come in handy while moving or renaming files using the mv command in Linux.
Useful Options
- -n, –no-cobber (Do not overwrite an existing file)
- -i, –interactive (Prompt before overwriting)
- -f, –force (Do not prompt before overwriting)
- -v, –verbose (Explain what is being done)
- -u, –update (Move only when the source file is newer than the Destination file)
- –version (Outputs the version information)
Practical Examples of the “mv” Command in Linux
The mv command is used all the time while managing files. Here, you will find some of the practical applications of this command you will need most of the time.
Example 1: Moving a File to Another Directory Using the “mv” Command
This is the main purpose of the mv command in Linux which is to move files. You need to type the source and destination directories to be able to do that. But you need to keep the file name the same for both the source and the destination. Otherwise, the file name will be changed after moving. The general syntax to move a file is as follows:
mv SOURCE DESTINATION
You can imitate the following steps to move a file using the mv command.
Steps to Follow >
❶ Open Terminal in the Home directory. Let, you have a file structure like the following on your Desktop.
❷ You want to move a file from the Desktop named Pride.txt into Folder_2. Type the following command to check the Desktop and the Folder_2.
ls -l ~/Desktop/
ls -l ~/Desktop/LinuxSimply/Folder_2
❸ You have to type the following command to move Pride.txt from Desktop to Folder_2.
mv Desktop/Pride.txt Desktop/LinuxSimply/Folder_2/Pride.txt
❹ Press the Enter key.
Output >
As you can see in the following pictures, Pride.txt moved from Desktop to Folder_2.
Before moving:
After moving:
Example 2: Using the “mv” Command to Move Multiple Files to Another Directory
Say, you want o move not just one file, but multiple files, you can use the mv command. In this case, you can move the files to just one destination folder. The general syntax is as follows:
mv SOURCE1 SOURCE2 SOURCE3 DESTINATION
In this case, let’s say you want o move 2 files named Pride.txt and Metamorphosis.txt to Folder_2. You can take the following steps.
Steps to Follow >
❶ Open Terminal in Home Directory.
❷ Check the Desktop and the Folder_2 folders using the following command.
ls -l Desktop/
ls -l Desktop/LinuxSimply/Folder_2
❸ Now type the following command to move these 2 files.
mv Desktop/Pride.txt Desktop/Metamorphosis.txt Desktop/LinuxSimply/Folder_2
❹ Press the ENTER key.
Output >
As you can see from the images below, the 2 files are now in Folder_2.
Before moving:
After moving:
Similar Readings
- The “diff” Command in Linux [11 Practical Examples]
- The “cmp” Command in Linux [7 Practical Examples]
- The “comm” Command in Linux [10 Practical Examples]
Example 3: Moving Multiple Files with Similar Names Using the “mv” Command
Sometimes there are similar files that we want to move. Here is a method to do that easily using a placeholder symbol (*). The general syntax to use this command is as follows:
mv FILE_NAME* DESTINATION
Here are the steps you need to follow to move multiple files with the same name.
Steps to Follow >
❶ Open the Terminal in the Home directory.
❷ Navigate inside the directory where the files reside. Here, let’s say you want to rename the files inside the Folder_1. Navigate using the command below:
cd Desktop/LinuxSimply/Folder_1
❸ You can view the files inside using the following command.
ls -l
❹ Let’s say you want to move the FILE_1 and FILE_2 into the LinuxSimply parent directory. Type the following command.
mv FILE_* ../
❺ Now, you can come back to the LinuxSimply folder by the following commands and check the file names.
ls
cd ..
ls -l
❻ Press the ENTER key.
Output >
Here as you can see, the file names starting with the FILE_ have been moved from the Folder_1 to the LinuxSimply folder.
Before moving:
After moving:
Example 4: Moving a Folder to Another Directory Using the “mv” Command
You can also move a folder using the mv command. The syntax is quite similar to that of moving files.
mv SOURCE_FOLDER DESTINATION
Let’s say, you have created a folder named Novels on your Desktop. Now you want to move the folder inside the LinuxSimply folder. Using the following steps, you can quickly move the whole folder with files inside.
Steps to Follow >
❶ Open Terminal in the Home directory.
❷ Move to the Desktop folder using the cd command like the following.
cd Desktop/
❸ Now, view the files inside the Desktop and the LinuxSimply folder.
ls -l
ls -l LinuxSimply/
❹ To move the Novels folder inside the LinuxSimply folder, type the following command.
mv Novels LinuxSimply/Novels
❺ Press the ENTER key.
Output >
In the command prompt, you will see the following output. You have just moved the Novels folder.
Before moving:
After moving:
Example 5: Moving and Overwriting Files with an Interactive Prompt Using the “mv” Command in Linux
Sometimes you have the same files inside a folder and you want to overwrite that file. But you also want to make sure which file you want to overwrite and which file you want to keep as it is. To do that you can use the -i or –interactive option. The general syntax of the command is as follows:
mv -i SOURCE1 SOURCE2 SOURCE3 DESTINATION
You can follow the steps below to try this command.
Steps to Follow >
❶ Open the Terminal in the Home directory.
❷ Change the directory into the Desktop using the cd command.
cd
❸ View the contents of the Desktop and Novels folder using the following command.
ls -l
ls -l LinuxSimply/Novels
❹ Type the following command to move the 3 files named Pride.txt, Metamorphosis.txt, and Iliad.txt. As you can see, Pride.txt and Metamorphosis.txt are already inside the Novels folder and you want to overwrite those 2 files. Use the following command to overwrite with a prompt.
mv -i Pride.txt Metamorphosis.txt Iliad.txt LinuxSimply/Novels
❺ You will receive a prompt where you need to press y as Yes or n as No each time you want to overwrite.
❻ Press the ENTER key.
Output >
After you press the ENTER key, you will receive the prompt where if you want to overwrite, you have to press y or n to overwrite or not overwrite.
Before moving:
After moving:
Similar Readings
- The “ln” Command in Linux [6 Practical Examples]
- The “lsof” Command in Linux [8 Practical Examples]
- The “tar” Command in Linux [12 Practical Examples]
Example 6: Using the “mv” Command to Move and Overwrite without any Prompt
Now if you want to move the same set of files, but don’t want to see the prompt and just overwrite them, use the -f or –force option. The general syntax is as follows:
mv -f SOURCE1 SOURCE2 SOURCE3 DESTINATION
Again, try these steps to understand the command.
Steps to Follow >
❶ Open the Terminal in the Home directory.
❷ Get inside the Desktop directory by using the cd command.
cd
❸ You can check the contents inside the Desktop and Novels folder using the following command.
ls -l
ls -l LinuxSimply/Novels
❹ Now type the following command to directly overwrite the contents without any prompt.
mv -f Pride.txt Metamorphosis.txt Iliad.txt LinuxSimply/Novels
❺ Press the ENTER key.
Output >
As you can see, the files inside the Desktop have been moved to the Novels folder. But there was no prompt in this case files actually changed location.
Before moving:
After moving:
Example 7: Using the “mv” Command to Rename a File
You can also rename a file at your wish using the mv command. The mv command works as a location-changing command which also allows renaming. In this case, you just need to keep the same destination and change the file name like the following:
mv file.txt FILE.txt
You can go through the following method to use this command.
Steps to Follow >
❶ Open the Terminal in the Home directory.
❷ Navigate inside the folder where the files are located. Here, you will get inside the Desktop folder with the cd command.
cd
❸ Use the following command to view files on the Desktop.
ls -l
❹ Let’s say you want to rename the Metamorphosis.txt into Meta.txt. Type the following command to do that.
mv Metamorphosis.txt Meta.txt
❺ Press the ENTER key.
Output >
As you can see the file name has been changed from Metamorphosis.txt to Meta.txt
Before renaming:
After renaming:
Example 8: Renaming Folders Using the “mv” Command
Renaming any folder is also quite easy using the mv command. It is almost the same as renaming a file. The general syntax is as follows:
mv folder FOLDER
You can follow the steps below to try this out.
Steps to Follow >
❶ Open the Terminal in the Home directory.
❷ Navigate to the directory where the folder is. In this case, let’s say you want to navigate inside the LinuxSimply folder and rename the Novels Folder. Type the following command.
cd Desktop/LinuxSimply
❸ You can view the folders and files inside this directory using the ls command.
ls -l
❹ Now, you need to type the following command.
mv Novels Popular_Novels
❺ Press the ENTER key.
Output >
Here you’ll find that the Novels folder is renamed the Popular_Novels folder.
Before renaming:
After renaming:
Conclusion
In this article, you’ve learned about many different features and uses of the mv command with some practical examples. There are many options available for this command and you may find them difficult. Don’t worry, you’re not the only one. Just come back here to review or bookmark this page. Practice all these examples and I’m sure you’ll be using them like a professional in no time.
Similar Readings
- The “chgrp” Command in Linux [7 Practical Examples]
- The “file” Command in Linux [9+ Practical Examples]
- The “fsck” Command in Linux [7 Practical Examples]
- The “patch” Command in Linux [4 Practical Examples]
- The “rmdir” Command in Linux [7 Practical Examples]
- The “stat” Command in Linux [9 Practical Examples]