What is the mv command in Linux & how to use it?

The mv is the command in Linux and Unix-like operating systems. So, the primary use of the mv command is to move or rename the file or directory. Also, you can move a single file or multiple files and directories. I am going to describe the mv command and its essential arguments.

How to get help for the mv command

I always suggest reading the manual page and help. So, It will give you an in-depth introduction to the command.

Firstly, to get the manual page type the below command,

man mv

Secondly, to get the only command options, you can type the below command.

mv --help

Syntax of the mv command

Now you know how to get help about the mv command. So, let’s talk about mv syntax. The syntax of the mv command is as below,

Syntax: mv [option] source… destination

Moving a file into a directory using mv command

It is very simple to move a file using the mv command. So, to move the file type mv and the file name and then the new location name.

mv file_name new_file_location

For example, I have a file in /root with name file1 and I want to move it to a new location /data then the command will look as below.

mv file1 /data/file1

Also, you can see the output in the below picture.

mv command in linux
Moving a file using the mv command.

Renaming a file or directory name using mv command

Renaming a file or a directory is very easy using the mv command. Suppose you have a file which name is file1 and you want rename like test1.

mv file1 test1

As a result, you can see in the below picture.

mv command in linux
Renaming the file name using the mv command.

Moving multiple files into a directory

If you want to move multiple files at once you can do that using the mv command.

mv file1 file2 file3 file4 directory_name 

Get prompt before overwriting a file

By default, the mv command overwrites if any file exists with the same name. So, this is good practice to put -i while moving the file or renaming the file.

mv -i file1 test1

Moving the file newer than the destination

So, to only move files that are newer than the destination pass the -u option. If a file is not newer than a file with the same name in the destination it will not be moved. Here -u stands for the update.

mv -u file1 file1

Conclusion

In this article, you learned about the mv command in Linux, and you can see that it is easy to use and you get a lot of information. So, I hope, you understand, but if you have any questions, you can ask in the comment section.

Also, you may like to read about files 

Leave a Reply

Your email address will not be published.