Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
mv Command
3.
Functions Performed using mv Command
3.1.
Renaming a File in Linux
3.2.
Moving a File to a Different Directory in Linux
3.3.
Moving and Renaming a File in Linux
3.4.
Moving Multiple Files in Linux
3.5.
Moving Directories in Linux
4.
Options Available in mv Command
4.1.
i Flag 
4.2.
version Flag
4.3.
f Flag 
4.4.
n Flag 
4.5.
b Flag
5.
Frequently Asked Questions 
5.1.
What is mv command in Linux?
5.2.
What is the mv and RM command in Linux?
5.3.
What is mv in command?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

mv Command in Linux

Author Rishabh
0 upvote
Roadmap to SDE career at Amazon
Speaker
Anubhav Sinha
SDE-2 @
25 Jun, 2024 @ 01:30 PM

Introduction

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 in Linux

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.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Functions Performed using mv Command

  1. Renaming a file
     
  2. Moving a file to a different directory
     
  3. Moving and renaming a file
     
  4. Moving multiple files
     
  5. 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

renaming a file

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

moving a file to a different directory

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

Moving and renaming a file

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

Moving multiple files

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

moving directory

Options Available in mv Command

  1. -i (interactive)
     
  2. -version
     
  3. -f (Force)
     
  4. -n (no-clobber)
     
  5. -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

interactive flag

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 

--version flag

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

force flag

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

no-clobber flag

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

backup flag

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 System to improve your knowledge about Linux.

To learn more, check out our articles:

And many more on our platform Coding Ninjas Studio.

Refer to our DSACompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio!

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 problemsinterview experiences, and interview bundles for placement preparations.
However, you may consider our paid courses to give your career an edge over others!

Previous article
How to Echo a New Line in Linux
Next article
WC Command in Linux
Live masterclass