mv — Linux basic command
Linux / Basic Linux commands / mv.html
A command for moving or renaming files or directories.
Basic Information
| Command Name | mv (move) |
|---|---|
| One-line description | A command for moving or renaming files or directories. |
| Provided by | GNU coreutils (default on all Linux distributions) |
| Official Documentation | https://man7.org/linux/man-pages/man1/mv.1.html |
| Supported OS | Linux, macOS, BSD, WSL |
| Installation Required | Pre-installed (no separate installation needed) |
| Category | Basic Linux Command (tags: file move, rename, linux, basic) |
What is this command?
mv is short for move. It is a command used to move files or directories to a different location or to rename them. Within the same filesystem, it is very fast as it only modifies metadata.
Since it handles both renaming and moving locations, these two operations are performed by the same command in Linux.
What problem does it solve?
- It moves files to a different directory.
- It renames files.
- It renames directories.
- It helps organize by safely moving unnecessary files.
Key Features
Moving Files
It moves files to a different directory.
- Use cases: When organizing and placing files after work.
- Result: It disappears from the original location and appears at the new location.
- Limitations: If a file with the same name exists in the target location, it will be overwritten.
Renaming
It renames a file within the same directory.
- Use cases: When a file was named incorrectly by mistake.
- Result: The file content remains unchanged; only the name changes.
- Limitations: If a file with the target name already exists, it will be overwritten.
Preventing Overwrite (-n)
It skips if a file with the same name exists.
- Use cases: When you want to avoid accidentally overwriting an existing file.
- Result: The existing file is protected.
- Limitations: There is no notification for skipped files, so caution is needed.
Backup Mode (-b)
It creates a backup of the original file before overwriting.
- Use cases: When a safety measure is needed while replacing important files.
- Result: The original is saved with a name appended with .~.
- Limitations: It uses slightly more disk space.
Installation and Execution
Check if it's pre-installed
It is included by default in all Linux distributions.
which mv
mv --version
Separate Installation (Lightweight Environments)
Usually, it is already installed.
sudo apt install coreutils # Debian/Ubuntu
Practical Examples
Basic Moving and Renaming
Move or rename files.
# Moving a file
cd /home/user/reports
mv report.txt /var/backups/
# Renaming
mv old-name.txt new-name.txt
# Renaming a directory
mv project-old project-new
Moving Multiple Files
Move multiple files at once.
# Move all files with a specific extension
mv *.log /var/log/app/
# Move multiple files to a directory
mv file1.txt file2.txt file3.txt /tmp/archive/
Safe Moving (Preventing Overwrite)
Move while protecting existing files.
# Skip if the same name exists
mv -n new-config.txt /etc/app/
# Create a backup before overwriting
mv -b updated-file.txt /etc/app/file.txt
# Check the backup file
ls /etc/app/file.txt.~