The mv
(move) command is used to move or rename files and directories in Linux. This guide covers all aspects of file movement and renaming operations.
Basic Syntax
mv [options] source destination
Common Options
-i
: Interactive mode (prompt before overwrite)-f
: Force move without prompting-n
: No clobber (don’t overwrite existing file)-u
: Update (move only when source is newer)-v
: Verbose mode (explain what’s being done)-b
: Create backup of existing destination files
Real-World Examples
1. Basic File Moving
# Move file to directory
$ mv file.txt Documents/
# Move multiple files
$ mv file1.txt file2.txt Documents/
# Rename file
$ mv oldname.txt newname.txt
2. Directory Operations
# Move directory
$ mv source_dir/ destination_dir/
# Rename directory
$ mv old_directory/ new_directory/
# Move contents
$ mv source_dir/* destination_dir/
3. Safe Moving
# Interactive mode
$ mv -i important.txt backup/
# Create backup of destination
$ mv -b file.txt destination/
# Move only newer files
$ mv -u *.txt destination/
Common Use Cases
-
File Organization
# Sort files by extension mv *.pdf documents/ mv *.jpg images/ mv *.mp3 music/
-
Backup Management
# Create dated backup mv file.txt file.txt.$(date +%Y%m%d) # Archive old files mv -t archive/ $(find . -mtime +30)
-
Project Management
# Reorganize project structure mv src/old-components/* src/components/ mv config.* settings/
Tips and Tricks
-
Pattern Matching
# Move multiple file types mv -t destination/ *.{jpg,png,gif} # Move files with specific prefix mv prefix* destination/
-
Safe Operations
# Don't overwrite existing files mv -n source/* destination/ # Create numbered backups mv --backup=numbered file.txt destination/
-
Bulk Operations
# Move and rename multiple files for f in *.txt; do mv "$f" "${f%.txt}.bak"; done
Best Practices
-
Use Interactive Mode for Important Files
mv -i critical_file.txt new_location/
-
Create Backups
# Simple backup mv -b important.conf /etc/ # Numbered backup mv --backup=numbered config.ini /etc/
-
Check Before Moving
# List files first ls -l files_to_move* mv -v files_to_move* destination/
Common Errors and Solutions
-
Permission Denied
# Solution: Use sudo sudo mv file.txt /etc/
-
Directory Not Empty
# Solution: Merge directories mv -i source_dir/* dest_dir/ rmdir source_dir
-
File Exists
# Solution: Use -i or -n mv -i source.txt destination/
Related Commands
cp
: Copy files and directoriesrm
: Remove filesrsync
: Advanced file copying/movingrename
: Rename multiple files
Advanced Usage
1. Bulk Renaming
# Rename with pattern
for f in *.txt; do
mv "$f" "${f%.txt}.md"
done
# Using rename command
rename 's/.txt$/.md/' *.txt
2. Moving with Conditions
# Move based on file size
find . -size +100M -exec mv {} large_files/ \;
# Move based on date
find . -mtime +30 -exec mv {} old_files/ \;
3. Safe Moving Script
#!/bin/bash
# Safe moving with verification
safe_move() {
if [ -e "$2/$1" ]; then
read -p "File exists. Overwrite? (y/n) " answer
[ "$answer" = "y" ] || return 1
fi
mv -v "$1" "$2"
}
Special Use Cases
1. Moving Across Filesystems
# Check if on same filesystem
df -P source destination
# Use rsync for cross-filesystem moves
rsync -av --remove-source-files source/ destination/
2. Moving Large Directories
# Move with progress indication
rsync -av --progress --remove-source-files source/ destination/
3. Moving with Timestamps
# Move and append timestamp
for f in *; do
mv "$f" "$f.$(date +%Y%m%d)"
done
Automation Examples
1. Auto-organizing Downloads
#!/bin/bash
cd ~/Downloads
mv *.pdf ~/Documents/PDFs/
mv *.{jpg,png,gif} ~/Pictures/
mv *.{mp3,wav} ~/Music/
mv *.{mp4,mkv} ~/Videos/
2. Project Cleanup
#!/bin/bash
# Move old logs to archive
find logs/ -name "*.log" -mtime +30 \
-exec mv -t archive/ {} +
# Move backup files
find . -name "*.bak" \
-exec mv -t backups/ {} +
Remember that mv
is a powerful command that can both move and rename files. Always use it carefully, especially with wildcards, and consider using the -i
option for important operations to prevent accidental overwrites.