With the increasing popularity of Linux-based operating systems, it is important for us to understand commands in Linux, and one such command is mv. mv stands for “move”. As the name suggests, the mv command is used to move or rename files and directories.
mv Command
mv command in Linux is generally used to move or rename a file or directory. It allows us to move a file or rename a file, or both at the same time.
Let’s learn more about mv command.
Functions Performed using mv Command
Renaming a file
Moving a file to a different directory
Moving and renaming a file
Moving multiple files
Moving directories
We will learn about each function in detail.
Renaming a File in Linux
For renaming a file using mv command, we can specify the name of the old file as a source and the new name as the destination.
Syntax
mv current_name new_name
For Example
Here the file name is changed from text.txt to notes.txt using the mv command.
NOTE: "ls" command is used to see the files and directories inside a directory.
Moving a File to a Different Directory in Linux
For moving a file to a different directory, we specify the file as a source and the destination directory as a destination for the file.
Syntax
mv file_path destination_directory
For Example
Here we have created a new directory named “newdir”. Now for moving the file notes.txt to newdir, we simply write the mv command. We can see that we have successfully moves notes.txt to newdir using the ls command.
Moving and Renaming a File in Linux
We can also move and rename the file simultaneously using the mv command.
Syntax
mv current_name directory_name/new_name
For Example
Here we are moving notes.txt to a directory named “new” followed by a change in its name to alphabet.txt. Hence we see that alphabet.txt comes into the directory “new”.
Moving Multiple Files in Linux
We can also move multiple source files to a new directory by using the mv command in the following manner.
Syntax
mv file_1 file_2 destination_directory
For Example
NOTE: “touch” command creates two different files, alpha.txt, and numeric.txt.
After creating two different files, we moved both files to a directory named “new” using the mv command. Hence we can see both the files using the mv command.
Moving Directories in Linux
We can also move the directory using the mv command by specifying the source directory and the new destination directory.
Syntax
mv current_directory destination_directory
For Example
Options Available in mv Command
-i (interactive)
-version
-f (Force)
-n (no-clobber)
-b (backup)
Now we will explain each of the options in detail.
i Flag
The ‘-i’ flag stands for interactive. When we use the -i flag, the mv command asks the user before overwriting any existing files.
Syntax
mv -i old_file new_file
For Example
Here we can see when we are trying to overwrite “file.txt” to “newfile.txt” the mv command asks if we want to overwrite the file
version Flag
The –version flag displays the version of the mv command.
Syntax
mv --version
For Example
f Flag
-f stands for force. When the -f flag is used, the mv command overwrites the file without prompting the user.
Syntax
mv -f old_file new_file
For Example
As we can see in this image, the mv command doesn’t ask for a confirmation which was the case in -i.
n Flag
-n stands for no-clobber. With the -n flag, mv prevents the existing file from being overwritten.
Syntax
mv -n old_file new_file
For Example
As we can see, the existing one is not renamed. But a new file with a new name is present.
b Flag
-b stands for backup, which is used to create a backup of any existing file before overwriting it. The backup file will have the same name as the original file, with a tilde (~) at the end of the file name.
Syntax
mv -b old_file new_file
For Example
As we can see, the mv command creates a backup of the file with a (~) sign at the end.
Frequently Asked Questions
What is mv command in Linux?
mv mv stands for “move”. mv command in Linux is generally used to move or rename a file or directory. It allows us to move a file, rename a file, or both at the same time.
What is the mv and RM command in Linux?
We use the mv command to move and rename files and directories in Linux, whereas the rm command stands for remove and is used to remove files and directories in Linux, so it should be used carefully.
What is mv in command?
The mv command in Linux is used to rename, move, or move and rename a file or directory in Linux. The command should be used carefully as this changes your file name or move it into the new directory.
Conclusion
In this article, we discussed the mv Command in Linux. You can also read the article Linux operating Systemto improve your knowledge about Linux.
But suppose you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problems, interview experiences, and interview bundles for placement preparations. However, you may consider our paid courses to give your career an edge over others!