mv command in Linux with options

Hi friends! today we will learn about mv command in Linux with options and examples. Basically, this command mainly used for two reasons.

1. Used to rename the files and folder.

2. Used to move the files and folder.

So, this can be only used in the local machine, suppose if we have to use the mv command to send the files to a remote machine that is not possible with this command.

Q. How to move a Single file in Linux using mv command

Move the Single File into the Folder
# mv linux_file tastethelinux

So, we have moved the linux_file into the tastethelinux folder using the mv command.

Q. How to move a Multiple files in Linux using mv command

So, on the current directory, we have aa, bb, and cc files which we have to move that files in the exam folder then we have used the following command.

# mv -v aa bb cc exam/

Also, you can see that we have used -v option, which is used to print the message in Linux.

Q. How to move an all .txt file in Linux

Suppose we have the extension like .txt, .mp4, .doc and we have to move into another directory that time we will use the wildcard in Linux.

So, we can see that we have to move aa.txt, bb.txt, cc.txt, and dd.txt into the exam directory.

# mv -v *.txt exam/

We can do the same with the doc and for the many other extensions.

What if we have to move all the files in Linux?

# mv -v * exam/

So, we will use * which will move all the files and folders into the exam directory.

Q. How to move a folder in Linux

Now the command is same for the moving folder into the linux

# mv exam tastethelinux/

Q. How to Rename a folder in Linux

As, we know we can use the mv command to rename the file and folder.

So, we can see that we have renamed the tastethelinux folder to the exam folder.

# mv tastethelinux exam

If we have the exam folder then it will move the tastethelinux folder into the exam folder instead of renaming the folder

The same way we can rename the file in Linux, now suppose we have one file named as “linux_file” and we have to rename that file to “exam_file”

# mv linux_file exam_file

Q. How to make the mv command interactive

So, we will use -i option to make the mv command, which means it will prompt before overwrite any file and folder.

# mv -i linux tastethelinux

So, in our case, we are moving the linux file into the “tastethelinux folder” and because we have used -i option it has prompt me that the same file is already there in the tastethelinux folder

Should mv command overwrite the tastethelinux/linux file?

So, if you will “press y” then it will overwrite the file or the folder, but if you will “press n” then it will just exit from a command line.

Q. How to take the backup of a file before renaming the file in Linux

So, this -b option will “only work with the file”. We will see step by step, so we can understand how things are working

So, as you can see the content in the two files as of now in taste and the linux file.

# mv -b taste linux

Now, we have renamed the file of a taste with linux, but what happens with the linux file then.

So, because we have used -b option it will make the Backup of the linux file and then change the name of the file taste.

So, we can see the content of the linux file and also there is not taste file in the current directory

And we can see the linux~ file content which makes the backup of the linux file.

Q. How to take a backup of a file with suffix before renaming the file in Linux

As we can see after taking the backup the file is generated with the tilde(~) sign.

What if we want the name as the .bkp or .bakcup? So, let’s use -S option for the suffix.

# mv -S .bkp -b taste linux

So, use the -S option with the -b option to take the backup and add the suffix instead of the tilde(~) sign.

Q. Not to move the existing file at the destination in Linux

So, we have two options 1st is – n option and 2nd is -u option. Let’s have a look at both of the options

-n option with mv command means do not overwrite an existing file

# mv -n taste linux exam/

So, the taste and linux are files and we have to move that file into exam folder, but the linux file was already their in exam folder.

And while using -n option we can see into the output image that it has just moved the taste file not the linux file in the exam folder.

-u option is the update option only move the source file if it is newer than the destination file

# mv -u taste linux exam/

So, you can see that the timestamp in the current folder is the same as in the exam folder.

So, it has just put the taste file in the exam folder.

A cheat sheet of mv command in Linux
Q. How to move the single file in Linux using mv command

Syntax:
#mv [source_file] [destination_folder]

Example:
#mv taste exam/
Q. How to move a multiple file in Linux using mv command

Syntax:
#mv [source_file1,source_file2,source_file3] [destination_folder]

Example:
#mv taste the linux exam/
Q. How to move a particular extension file in Linux using mv command

Syntax:
#mv *.extension [destination_folder]

Example:
#mv *.txt exam/
#mv *.doc exam/
#mv *test* exam/
Q. How to move a folder in Linux using mv command

Syntax:
#mv [source_folder] [destination_folder]

Example:
#mv tastethelinux exam/
Q. How to rename a folder in Linux using mv command

Syntax:
#mv [source_folder] [changed_folder]

Example:
#mv tastethelinux exam 
Q. How to rename a file in Linux using mv command

Syntax:
#mv [source_file] [changed_file]

Example:
#mv linux_file exam_file
Q. How to make the output interactive in Linux using mv command

Syntax:
#mv -i [source_file] [destination_folder]

Example:
#mv -i linux_file exam/
Q. How to print the output in Linux using mv command

Syntax:
#mv -v [source_file] [destination_folder]

Example:
#mv -v linux_file exam/
Q. How to take backup of the file before renaming in Linux using mv command

Syntax:
#mv -b [source_file] [destination_file]

Example:
#mv -b linux_file exam_file
Q. How to take backup with suffix of the file in Linux using mv command

Syntax:
#mv -S [suffix] -b [source_file] [destination_file]

Example:
#mv -S .bak -b linux_file exam_file
Q. Do not overwirte the existing file in Linux using mv command

Syntax:
#mv -n [source_file1,source_file2] [destination_file]

Example:
#mv -n linux_file exam_file exam/
Q. Only move the source file if it is newer that the destination file in Linux using mv command

Syntax:
#mv -u [source_file1,source_file2] [destination_file]

Example:
#mv -u linux_file exam_file exam/

0%

The mv command is used to move the files and folder

Correct! Wrong!

Can we rename the files and folder?

Correct! Wrong!

-S is used in mv command for ?

Correct! Wrong!

How to send all the files using mv command?

Correct! Wrong!

Correct the Syntax?

Please select 3 correct answers

Correct! Wrong!

Play Quiz and check the grip in mv command
Thanks for your valuable time!
To Learn more Basic command you can go to "LINUX BASIC COMMAND SECTION" and Spread the Knowledge Keep Supporting us! Follow us on our ID's: >> Instagram: @tastethelinux_official >>Facebook: https://fb.me/tastethelinuxofficial >>Twitter: @LinuxTaste

Share your Results:

To learn ls command refer the Link

Give your valuable time