Moving or renaming files or directory with mv command in Linux – Top 9 uses of mv command with example

mv command in Linux 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

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.

Syntax

mv filename /folder/name

Example

mv text1 /home/test/
mv command in Linux - Moving files with mv command in Linux

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.

Syntax

mv filename filename1 filename2 /folder/name

Example

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.

Syntax

mv *.mp3 filename2  /folder/name
mv command in Linux - Moving more than one file with mv command

Moving Directory with mv command

To move a directory with mv command we will have to use mv command without any parameters.

Syntax

mv directory_name/of/folder/name/for/move

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.

Syntax

mv old_file_name new_file_name

If files are in different folder than current folder, use Absolute path for files

Syntax

mv /folder/name/old_file_name /folder/name/new_file_name
mv command in Linux - Renaming files with mv command in Linux

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.

Syntax

mv old_directory_name new_directory_name
mv command in Linux - >Renaming directories using mv command

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.

Syntax

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.

Syntax

mv -i filename /folder/name

When prompted for user will have to provide y to overwrite file.

No overwrite

No files will be overwritten when –n option is used with mv.

Syntax

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