Updated June 16, 2023
Introduction to mv command in Linux
The mv command is termed as “Move,” which is a command-line utility to move files or directories from source to target. It supports moving a single file, multiple files, and directories. It is very similar to copy command (cp), used for copying, and the remove command (rm), used for deleting. The only difference is that the move command has features of both commands for renaming and moving files.
To run the move command (mv) to move a file or directory, we must write permission on the source and destination; otherwise, we will receive an error of permission denied.
mv [OPTION] SRC DEST
where SRC is the Source file or directory, and DEST is the Target file or directory.
Application of various factors:
- The source can be single or more files or directories, and the destination can be a single file or directory.
- The destination must be a directory when multiple files or directories are passed as a source. In this scenario, the source files will move to the target directory. If we specify a single file as a source, the destination target is an existing directory, and the file will be moved to the specified directory.
- When we specify a single file as a source and a single file destination, then we rename the file.
- When our source is a directory, and the destination does not exist, the source will be renamed to the destination, or if it exists, it will be moved inside the destination directory.
There are a variety of options available for move command.
1. mv -i: This option signifies “Interactive Mode,” which will prompt the user’s confirmation before moving a file that will replace/overwrite already an existing file with the same name. We must enter “y” to confirm or overwrite the file in this scenario.
2. mv -n: This option is used as a no-clobber, implying it will prevent a file from overwriting. In simple terms, we can rename a file to match another file and still keep the content of the already existing file.
3. mv -v: It means moving the file in “verbose mode,” which will display the activity status happening while the mv command is running.
4. mv -u: This option implies “update mode,” which means it will update the missing destination file only when the source file has any new content or even when the destination file is missing.
5. mv *: This option will move multiple files to a specific/current directory.
6. mv –suffix=suffix: This option takes a backup of the files or directories before overwriting it. The default is “~.”
7. mv –version: This option checks the version of the mv command.
8. mv -f, –force: This option will move the files or directories without a prompt. This option will be helpful if we need to overwrite multiple sets of files whose permission is read-only, and if you do not specify this option, then a prompt will appear for every file.
Examples of mv command in Linux
Given below are the examples of the mv command in Linux:
Example #1 – Rename the File.
When the file is renamed with the mv command, the inode number remains the same even after moving it to a different file name. Indone number changes when moving and changing the filesystem
mv File.txt File1.txt
Example #2 – Renaming the Directory.
Similarly, we can rename a directory using the mv command like a file renaming. Here also inode number is the same as in renaming a file.
ls -l will display the files and directory in the directory. For displaying the only directory, make use of the -d option. -i option, which will display the inode number of the directory.
mv Dir1 Dir2
Example #3 – Prompting of Terminal for confirmation of overwrite.
mv command will not ask for confirmation while overwriting but default if in the same place. It will simply overwrite it. To avoid any issue, we want a confirmation from the mv command before overwriting the destination file, so we need the “-i” option as shown. Then we can type Y or N for the operation to take place.
mv -i File.txt File1.txt
When the destination files permission differs from the source file, the mv -i command it will display the confirmation below.
Example #4 – Moving Multiple Files to a Specific Directory.
We can move multiple files or dir with the MV command. Below will show how to move the content of dir using the MV command.
mv * app1/
Example #5 – Backup file creation before overwriting.
A backup file would be created using the mv –suffix. The system will move the previous file, adding a new suffix specified within the -S option of the –suffix option.
mv --suffix=.bak File1 File2
Example #6 – File movement in the destination path that does not exist.
mv * will help move all files to a new location. mv -u command will enable move files or dir to the new location that were not existing or updated/changed.
The below command will move only File2 and File2 from app to app1, as the File1 file is already present in app1, so it will move.
mv -u app/* app1/
This has been a guide to mv command in Linux. Here we discuss the basic concept, options, and examples of the mv command in Linux. You may also have a look at the following articles to learn more –