Moving a File Using the “mv” Command
The “mv” here specifies the move that will help you rename or move the files or directories from one specified location to another desired location within the Linux system. The below is the syntax for using the “mv” command.
In the above “mv” command syntax, the SOURCE specifies single or more files or directories per the user’s requirement. The specified DESTINATION can be a single file or directory accordingly.
- If you wish to move more than one file or directory specified as a SOURCE, then the specified DESTINATION must be a directory capable of holding all the source files.
- Suppose you wish to move a file specified as a SOURCE, and the specified DESTINATION is an existing directory. In that case, the source file will get moved to the mentioned destination directory.
- But in case if you wish to move a single file and have specified the destination also as a single file, then the “mv” command will rename the file.
- But when you specify the SOURCE as a directory and the mentioned DESTINATION doesn’t exist, the “mv” command will rename the SOURCE to DESTINATION. Otherwise, in case if specified DESTINATION exists, the source will get moved to the DESTINATION directory.
If you want to move a file, you must have write access to both the source and the destination; otherwise, you will get a permission denied error on the screen.
Suppose we move the file “file1” from the current working directory to the /tmp directory; we will use the following command.
But, if you want to rename the file, mention the destination file name as shown below.
If you want to move the directories, it will be the same process as moving the files, as shown above. Here, we use the example where we use dir1 as the source and dir2 as the destination.
Moving Multiple Files and Directories
If you wish to move more than a single file and directories, mention the files as the source. Considering the below example, we are moving file1 and file2 to the specified directory dir1. You can use the following command syntax to do so.
You can also use the “mv” command for pattern matching if you wish to move all the files with the “pdf” extension from the source to the destination directory “~/Documents”, as shown below.
Various “mv” Command Options
You can use various “mv” command options that will somehow affect the behavior of the command. The “mv” command is an alias in some Linux distributions to the “mv” command. But in CentOS, the “mv” command is used as an alias of “mv -i”.
Use the type command with the “mv” command to check if your distribution is using the alias for the “mv” command or not.
If the “mv” command is an alias, then you will get the below output.
Prompt Before Overwriting
If you are trying to move a source file to the already existing destination, then the destination will be overwritten by default. If you want the confirmation for the overwriting, use the “-i” option as shown below.
Press “Y” to confirm if you want to proceed with the overwrite.
If you want to overwrite the read-only file using the “mv” command, then you will get a confirmation if you wish to overwrite the files or not, as we have seen in the above example using the “-i” option with the “mv” command.
But if you do not want that confirmation and want to overwrite the desired files, then use the “-f” option along with the “mv” command.
This option is generally useful when you are moving multiple read-only files.
Do Not Overwrite Existing Files.
If you want no file to be overwritten, use the “-n” option along with the “mv” command, as shown below.
If the file1 file already exists in the /tmp folder, there will be the action of this command, but there is no file1 file in the /tmp folder, then it will move the file to the destination folder.
Backing Up Files
If you want to create a backup of the existing destination file, use the “-b” option along with the “mv” command below.
The backup file will be created with the same name as the original file but with the “~” appended. Verify if the backup of the file is created, run the “ls” command.
You will get the below-mentioned output-
If you want to get the progress of your command on the screen, use the “-v” option for the verbose output.
You will get the below output.
In Linux, you can do a task using the command-line interface efficiently. Only you need to have a good understanding of the command-line commands. One of the very common tasks is moving the files or directories from the source to the destination. Also, you can use various options with the move command for several features that will help you control your tasks. We hope you have understood the working of the “mv” command and how it behaves with various options.