mv — Move and Rename Files in Linux
mv both moves files to new locations and renames them — it's the same command. Learn the difference, the safety flags, and everyday patterns.
January 13, 20254 min read
LinuxFilesCommands
mv does two things that feel different but use the same underlying operation — changing a file's path:
- Rename — same directory, different name
- Move — different directory, same or different name
There's no separate rename command for basic use cases in Linux. mv handles both.
Options
mv Options
| Option | Long Form | Description |
|---|---|---|
-i | --interactive | Prompt before overwriting |
-f | --force | Force overwrite without prompting |
-n | --no-clobber | Never overwrite existing files |
-u | --update | Move only if source is newer than destination |
-v | --verbose | Show each operation |
-b | --backup | Make a backup of overwritten files |
Examples
mv-examples.sh
# ─── Renaming ─────────────────────────────────────────────────
mv old_name.txt new_name.txt # Rename file
mv old_folder/ new_folder/ # Rename directory
mv report.doc report_v2.doc # Rename with versioning
# ─── Moving ──────────────────────────────────────────────────
mv file.txt Documents/ # Move to Documents/ (keeps name)
mv file.txt Documents/newname.txt # Move and rename at the same time
mv file1.txt file2.txt mydir/ # Move multiple files to a directory
mv folder/ /var/www/ # Move entire directory
# ─── Safety ──────────────────────────────────────────────────
mv -i source.txt dest.txt # Ask before overwriting
mv -n source.txt dest.txt # Never overwrite
mv -b source.txt dest.txt # Backup: dest.txt → dest.txt~
mv -v file.txt destination/ # Show: renamed 'file.txt' -> 'destination/file.txt'mv Overwrites Without Warning by Default
Unlike cp, mv will silently overwrite the destination if it already exists — no prompt, no backup. Always use mv -i when moving files to a location where a file with the same name might exist. Or use mv -b to auto-create backups (file.txt~).
Practical Patterns
# Organise downloads by type
mv ~/Downloads/*.pdf ~/Documents/PDFs/
mv ~/Downloads/*.jpg ~/Pictures/
# Add a date suffix to a folder before archiving
mv project/ project_2025-01/
# Rename all .txt files to .md (using a loop)
for f in *.txt; do mv "$f" "${f%.txt}.md"; done
# Atomically replace a file (mv is atomic on the same filesystem!)
cp -a config.yaml config.yaml.tmp
# edit config.yaml.tmp...
mv -f config.yaml.tmp config.yaml # Instantly swaps in the new fileQuick Check
You run `mv report.txt /archive/report.txt` but `/archive/report.txt` already exists. What happens by default?
Exercise
Practice mv:
- Create a file
draft.txtand rename it tofinal.txt - Create a directory
archive/and movefinal.txtinto it - Now move and rename in one step: move
archive/final.txtto the current directory asfinal_v1.txt - Create two files
a.txtandb.txt, then trymv -i a.txt b.txt— what does -i show you?