
Introduction
The mv command in Linux enables you to move files and directories from one location to another. It's also known as move mv and can also be used to rename files and directories in a file system.
This article explains how to use the mv command in Linux to move and rename files.
Syntax of mv Command
Below is the basic mv command syntax:
$ mv [options] source destination
Within the above command, source is the target file or directory to move, and destination is the new file or directory location in the file system.
mv Command Options
| Option | Description | 
|---|---|
-i | 
  Interactive mode. Enables prompts when overwriting existing files. | 
-f | 
  Force mode. Moves files without prompting to overwrite existing files | 
-n | 
  No clobber. Does not overwrite existing files. | 
-u | 
  Update. Moves the file only if the source is newer than the destination or if the destination file is missing. | 
-v | 
  Verbose. Displays the progress of the move command. | 
Practical Examples of the mv Command
Move a file from your working directory to another directory.
console$ mv file1.txt /home/user/documents/
The above command moves
file1.txtfrom your active working directory to the/home/user/documents/directory.Output:

Rename a file.
console$ mv oldname.txt newname.txt
The above command renames
oldname.txttonewname.txtin your working directory.Output:

Move multiple files to a directory.
console$ mv file1.txt file2.txt /home/user/documents/
The above command moves
file1.txtandfile2.txtto the/home/user/documents/directory.Output:

Move a directory.
console$ mv /home/user/oldfolder /home/user/newfolder
The above command moves the
oldfolderdirectory to/home/user/newfolder.Output:

Enable overwriting prompts before replacing an existing file.
console$ mv -i file1.txt /home/user/documents/
The above command displays a confirmation prompt before overwriting
file1.txtin the destination directory if the file exists.Output:

Force move a file without prompting overwrite changes.
console$ mv -f file1.txt /home/user/documents/
The above command moves
file1.txtto the destination directory and overwrites the existing file without a confirmation prompt.Output:

Move files without overwriting the existing files.
console$ mv -n file1.txt file2.txt /home/user/documents/
The above command moves
file1.txtandfile2.txtonly if no files exist with the same name in the destination directory.Output:

Backup file an existing file with a new name.
console$ mv -b file1.txt file2.txt
The above command renames
file1.txttofile2.txtto create a backup offile2.txtwith a tilde character (~) appended if the file exists.Output:

View progress of the move operation.
console$ mv -v file1.txt /home/user/documents/
The above command moves
file1.txtand displays the progress of the move operation.Output:

Handling Special Scenarios
Move files with spaces in the names.
console$ mv "file name with spaces.txt" /home/user/documents/
The above command moves the filename defined with quotes and moves it to the destination directory.
Output:

Move hidden files.
console$ mv .hiddenfile /home/user/documents/
The above command moves the
.hiddenfilefile to the/home/user/documents/directory.Output:

Advanced Usage Scenarios
Use the
mvcommand in scripts to automatate file management tasks as described in the following steps.Create a new sample script file
mv_script.shusing a text editor such as Nanoconsole$ nano mv_script.sh
Add the following contents to the file.
bash#!/bin/bash for file in *.txt; do mv "$file" "${file%.txt}_$(date +%F).txt" done
Enable execute privileges on the file.
console$ chmod +x mv_script.sh
Run the script
console$ ./mv_script.sh
The above script appends the system date to all
.txtfiles in the working directory.Output:

Move files based on a pattern.
console$ mv *.txt /home/user/textfiles/
The above command uses a wildcard character to move all files with the
.txtextension to the/home/user/textfiles/directory.Output:

Enable confirmation while moving files.
console$ mv -i *.txt /home/user/documents/
The above command moves all
.txtfiles using interactive mode and enables confirmation prompts before overwriting any existing files in the destination directory.Output:

Move files based on the age.
console$ find /source_directory -type f -mtime +30 -exec mv {} /destination_directory/ \;
This above command finds and moves all files older than 30 days from
/source_directoryto/destination_directory.Output:

Conclusion
You have used the mv command to perform file movement and renaming tasks in Linux. For more command options, run the man mv command to view the mv manual page.