Moving and Renaming (mv)
The mv command moves files and directories, or renames them. It's a versatile command for organizing your filesystem.
Basic Usage
mv source destination
Two Uses of mv
1. Moving Files
Move a file to a different location:
mv file.txt /new/location/
mv document.pdf ~/documents/
2. Renaming Files
Rename a file (move to same location with different name):
mv oldname.txt newname.txt
mv report.txt final_report.txt
mv Options
| Option | Description |
|---|---|
-i | Interactive (prompt before overwrite) |
-n | No overwrite (never overwrite existing) |
-v | Verbose (show files being moved) |
-f | Force (don't prompt, override -i) |
Moving Directories
Unlike cp, mv doesn't need -r for directories:
mv old_folder new_folder # Rename directory
mv folder /new/location/ # Move directory
Exercise: Rename a File
Rename the file notes.txt to important_notes.txt:
Moving Multiple Files
Move multiple files to a directory:
mv file1.txt file2.txt file3.txt destination/
With wildcards:
mv *.txt documents/
mv *.jpg photos/
Interactive Mode
Prevent accidental overwrites:
mv -i source.txt existing.txt
# Prompts: overwrite 'existing.txt'?
Common Patterns
Organize by Extension
mv *.pdf documents/
mv *.jpg *.png images/
mv *.mp3 music/
Rename with Timestamp
mv report.txt report_$(date +%Y%m%d).txt
Move and Rename
mv ~/downloads/file.txt ~/documents/renamed_file.txt
mv vs cp
| Feature | mv | cp |
|---|---|---|
| Original file | Removed | Kept |
| Speed | Fast (same filesystem) | Slower (copies data) |
| Directories | No -r needed | Needs -r |
| Use case | Reorganize | Duplicate |
Troubleshooting
| Error | Cause | Solution |
|---|---|---|
cannot stat | Source doesn't exist | Check the path |
Not a directory | Dest exists as file | Choose different name |
Permission denied | No access | Check permissions |
Key Takeaways
mvmoves and renames files/directories- Same location = rename, different location = move
- No
-rneeded for directories - Use
-ito confirm overwrites - Original file is removed (unlike cp)

