mv Command Guide
The mv command moves and renames files and directories. Learn how to relocate and rename files safely.
5 min read•Last updated: 2024
Dai Aoki
CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm
Quick Reference
Basic
mv file.txt dir/Move file to directorymv old.txt new.txtRename filemv dir1/ dir2/Move/rename directoryOptions
-iPrompt before overwrite-nDo not overwrite-vVerbose outputSafety
-uMove only if newer-bCreate backup--backup=numberedNumbered backupsCommon
mv *.txt dir/Move multiple filesmv -t dir/ f1 f2Target directory firstmv file{,.bak}Quick backupDownloadable Image Preview
Failed to generate preview
Basic Usage
mv is used for both moving and renaming files.
bash
# Rename a file
mv oldname.txt newname.txt
# Move file to directory
mv file.txt /path/to/directory/
# Move and rename
mv file.txt /path/to/directory/newname.txt
# Move multiple files to directory
mv file1.txt file2.txt /path/to/directory/Common Options
mv Options
| -i | Prompt before overwriting |
| -n | Do not overwrite existing files |
| -f | Force overwrite without prompting |
| -u | Move only when source is newer |
| -v | Verbose output |
| -b | Create backup of destination files |
| -t dir | Move all sources to directory |
Renaming Files
bash
# Simple rename
mv report.txt final_report.txt
# Rename with different extension
mv script.txt script.sh
# Rename directory
mv old_project/ new_project/Moving Files and Directories
bash
# Move file to directory
mv document.pdf ~/Documents/
# Move multiple files
mv *.txt /destination/
# Move directory
mv project_dir/ /new/location/Info
Unlike cp, mv doesn't need
-r for directories. Directories are moved automatically.Preventing Overwrites
bash
# Ask before overwriting
mv -i file.txt /destination/
# Never overwrite
mv -n file.txt /destination/
# Only move if source is newer
mv -u file.txt /destination/Warning
By default, mv will overwrite existing files without warning. Use
-i in interactive sessions or -n in scripts.Creating Backups
bash
# Create backup before overwriting
mv -b file.txt /destination/
# Creates file.txt~ as backup
# Specify backup suffix
mv --suffix=.bak -b file.txt /destination/
# Creates file.txt.bakBatch Renaming
Using a for loop
bash
# Add prefix to all .txt files
for f in *.txt; do mv "$f" "prefix_$f"; done
# Change extension
for f in *.txt; do mv "$f" "${f%.txt}.md"; done
# Lowercase filenames
for f in *; do mv "$f" "$(echo $f | tr 'A-Z' 'a-z')"; doneUsing rename command
bash
# Rename using regex (Perl rename)
rename 's/.txt$/.md/' *.txt
# Add prefix
rename 's/^/prefix_/' *.txt
# Remove spaces
rename 's/ /_/g' *Practical Examples
Organize files by extension
bash
mv *.jpg images/
mv *.pdf documents/
mv *.mp3 music/Move files older than 30 days
bash
find . -mtime +30 -exec mv {} /archive/ \;Rename with timestamp
bash
mv file.txt "file_$(date +%Y%m%d_%H%M%S).txt"Move all files to parent directory
bash
mv subdir/* .Interactive move with verbose
bash
mv -iv *.txt /destination/mv vs cp + rm
On the same filesystem, mv is instant (just renames the inode). Across filesystems, mv copies then deletes.
bash
# Same filesystem: instant
mv /home/user/file.txt /home/user/subdir/
# Different filesystem: copy + delete
mv /home/user/file.txt /mnt/external/Summary
mv is essential for file organization. Key takeaways:
- Use
mv old newto rename - Use
mv file dir/to move - Use
-ito prevent accidental overwrites - Use
-bto create backups - Use
renameor loops for batch operations