Command mv is used to move files or directories from one location to another in Linux systems. In addition to moving files it can be used to rename files and directories also. Below detailed use of mv command in Linux is given.
- Example – Use of mv command in Linux
- Moving files with mv command
- Moving more than one file with mv command
- Moving Directory with mv command
- Renaming files with mv command
- Renaming directories using mv command
- Backing up files when moving with mv command
- Interactive mode for mv command
- mv command with No Ovewrite option
- Use mv command to update file if destination file is newer
Example – Use of mv command in Linux
Let’s see different uses of mv command in Linux.
Moving files with mv command in Linux
Here we will use mv command to move file from one folder to another folder. Please note both locations should be different. File will be deleted from original location and moved to new location given by user.
mv filename /folder/name
mv text1 /home/test/
Moving more than one file with mv command
It is also possible to move multiple file with move command. All file names are given with space between them.
mv filename filename1 filename2 /folder/name
mv text1 text2 /home/test/
Moving similar type of files using wildcard character. These files with same extension or have some pattern in their names.
mv *.mp3 filename2 /folder/name
Moving Directory with mv command
To move a directory with mv command we will have to use mv command without any parameters.
Renaming files with mv command in Linux
Another use of mv command is renaming of files. Renaming a file with mv command requires to use mv with name of file followed by change name of file. Note: location of file should be same.
mv old_file_name new_file_name
If files are in different folder than current folder, use Absolute path for files
mv /folder/name/old_file_name /folder/name/new_file_name
Renaming directories using mv command
Same as renaming a file with mv command can be used for renaming of directory. For this use mv with name of directory followed by change name of directory. Note: location of directory should be same.
mv old_directory_name new_directory_name
Backing up when moving files with mv command
mv command has a –b option which allows to backup file while moving it. This option is very helpful if you want to recover file, if you have made any mistakes while moving file.
mv -b filename /folder/name
Note: Backup file will be created with tilde after file name.
Interactive mode for mv command
While using interactive mode for mv command, a prompt will be given before overwriting destination file if file with similar name exists in destination folder.
mv -i filename /folder/name
When prompted for user will have to provide y to overwrite file.
No files will be overwritten when –n option is used with mv.
mv -n filename /folder/name
If file exists will same name in destination folder move will not be done.
Update file when destination file is newer
Using update –u option with mv command will ensure file is moved only when source file newer than destination file or no file with same name exists in destination folder.
mv -u filename /folder/name