How to Move Files in Linux Using the mv Command
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.txt
from 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.txt
tonewname.txt
in your working directory.Output:
Move multiple files to a directory.
console$ mv file1.txt file2.txt /home/user/documents/
The above command moves
file1.txt
andfile2.txt
to the/home/user/documents/
directory.Output:
Move a directory.
console$ mv /home/user/oldfolder /home/user/newfolder
The above command moves the
oldfolder
directory 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.txt
in 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.txt
to 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.txt
andfile2.txt
only 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.txt
tofile2.txt
to create a backup offile2.txt
with 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.txt
and 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
.hiddenfile
file to the/home/user/documents/
directory.Output:
Advanced Usage Scenarios
Use the
mv
command in scripts to automatate file management tasks as described in the following steps.Create a new sample script file
mv_script.sh
using 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
.txt
files 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
.txt
extension to the/home/user/textfiles/
directory.Output:
Enable confirmation while moving files.
console$ mv -i *.txt /home/user/documents/
The above command moves all
.txt
files 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_directory
to/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.