Introduction to mv command in Linux
The mv command termed as “Move”, which is a command-line utility to move files or directories from source to target. It supports the moving of a single file, multiple files, and directories. It is very similar to copy command (cp), used for copying and remove command (rm), used for deleting. The only difference is that move command has features of both commands for renaming and moving of files.
To run move command (mv), to move a file or directory we must need to have to write permission on the source and destination else we will receive an error of permission denied.
mv [OPTION] SRC DEST
where SRC is Source file or directory and DEST is Target file or directory.
Application of various factor:
- The source can be single or more files or directories and destination can be a single file or directory.
- When multiple files or directories are passed as a source then the destination needs to be a directory. In this scenario, the source files will move to the target directory. If we specify a single file as a source, then the destination target is an existing directory, then 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 are renaming the file.
- When our source is a directory and destination does not exist then the source will be renamed to the destination or if destination exists then 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”, means that it will prompt the user’s confirmation before moving a file that will replace/overwrite already existed file with the same name. In this scenario, we have to enter “y” to confirm or overwrite the file.
2. mv -n: This option used as no-clobber which implied that 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”, means it will display the activity status happening while mv command is running.
4. mv -u: This option implies as “update mode”, means it will update the destination file is missing 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 is used to take a backup of the files or directories before overwriting it. Default is “~”.
7. mv –version: This option checks the version of mv command.
8. mv -f, –force: This option will move the files or directories without any prompt. This option will be useful if we need to overwrite multiple sets of files whose permission is read-only and if you do not specify this option then prompt will appear for every file.
Examples of mv command in Linux
Given below are the examples of mv command in Linux:
Example #1 – Rename the File.
When the file is renamed with mv command then the inode number remains the same even after moving it to a different file name. Indone number changes when moving and changing filesystem
mv File.txt File1.txt
Example #2 – Renaming the Directory.
Similarily like renaming of a file, we can also rename a directory by mv command. 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 -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 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 destination files permission is different than the source file then mv -i command will display the below confirmation.
Example #4 – Moving Multiple File to a Specific Directory.
We can move multiple files or dir with MV command. Below will show how to move the content of dir using MV command.
mv * app1/
Example #5 – Backup file creation before overwriting.
Now when we use mv –suffix, a backup file would be created. Now that previous file will be moved with new suffix specified within -S of –suffix option.
mv --suffix=.bak File1 File2
Example #6 – File movement in the destination path that does not exist.
mv * will help in the movement of all files to a new location. mv -u command will help in moving files or dir to the new location which were not existing and not updated/changed as well.
The below command will move only the File2 and File2 from app to app1, as the File1 file 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 introduction, options, and examples of mv command in Linux. You may also have a look at the following articles to learn more –