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.
Let’s see different uses of 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
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
To move a directory with mv command we will have to use mv command without any parameters.
mv directory_name/ /folder/name/for/move
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
Absolute paths can also be given
mv /folder/name/old_file_name /folder/name/new_file_name
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 file
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
when 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.
Syntax mv -u filename /folder/name