how to use mv command featured image

How to use mv Command?

The Linux “Move” command is effective to move files and directories inside the file system. The mv command can be also rename a file or directory as well as relocate it to a different location. As the Linux terminal has no rename command this command also acts as a important method to rename the files and directories.

Example syntax

mv [Source] [Destination]

Where source specifies the location of the file or directory you wish to move, and destination represents the place to which you wish to move the file or directory.

How to use mv command
Note: In the example we are moving the “sample.txt” file to “howto” folder using “mv sample.txt howto/sample.txt” command.

Special Note

frequently used mv command options

  • -i: The “-i” option asks the user for approval before overwriting a file that already exists.
How to use mv command with -i option
In the example “sample.txt” already exists inside “howto” folder and when we try to move new sample.txt file with command “mv -i sample.txt howto/sample.txt” we get a prompt asking for permission to over write existing file.
  • -f: With the “-f” option, you can compel a command to overwrite a file without asking for your approval.
how to use mv command with -f
This is similar to the previous option but in this case when we us “mv -f sample.txt howto/sample.txt” it skips the confirmation and overwrites the existing file.
  • -n: In contrast to “-i”, the -n option does not ask for confirmation before overwriting a file. Instead, if the file is present, it will simply skip it and move on to the next file.
How to use mv command with -n
While “‘-f” skips the confirmation and overwrites a existing file using “mv -n sample.txt howto/sample.txt” not only skips the confirmation but also skips any existing file and does not over write.
  • -v: Use the “-v” option to show verbose output of what has been done.
How to use mv command with -v
using “-v” does not effect the execution of the command instead using “mv -v verbose.txt howto/verbose.txt” will just inform us what has been done.
how to use mv command to rename file
“mv” command is not only for moving files it can also rename as shown in the example “mv verbose.txt renamed.txt”.

Use of mv command

  • to move or rename files or directories in the file system.
  • transfer or rename a file or directory and that it might overwrite an existing file. This is when the “-i” ,”-f” and “-n” options come in handy.
  • to transfer or rename numerous files and folders without overwriting any existing files, the -n option is helpful.
  • using the mv command to relocate a file or directory will delete it from its original place. As a result, if you require a copy of the file or directory, use the cp command rather than mv.

1 thought on “How to use mv Command?”

  1. I have to thank you for the efforts you have put in writing this site. I really hope to view the same high-grade content by you later on as well. In fact, your creative writing abilities has inspired me to get my very own blog now 😉

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
Scroll to Top