Introduction#
Hello! Today I’ll explain the mv command.
The mv command moves files or renames them. Think of it as the command-line version of cut-and-paste or rename.
Super simple, but you’ll use it constantly.
What is the mv Command#
The mv command is an external command for moving or renaming files and directories. “mv” stands for “move”.
Unlike cp, the original file doesn’t remain - it’s moved or renamed. When you change the name in the same directory, it’s a “rename”. When you move it to a different directory, it’s a “move”.
Basic Syntax#
1
|
mv [options] source destination
|
Main Options#
| Option |
Description |
-i |
Prompt before overwrite (interactive) |
-f |
Force overwrite |
-n |
Do not overwrite existing files |
-v |
Verbose output |
-u |
Move only when source is newer (update) |
-b |
Create backup before overwriting |
-t |
Specify target directory first |
Usage Examples#
Example 1: Rename a File#
1
|
mv oldname.txt newname.txt
|
Output:
Renames oldname.txt to newname.txt. The most basic usage.
Example 2: Move File to Different Directory#
1
|
mv file.txt /home/user/documents/
|
Output:
Moves file.txt to the /home/user/documents/ directory.
Example 3: Move and Rename Simultaneously#
1
|
mv file.txt /backup/file_backup.txt
|
Output:
Moves file.txt to /backup/ and renames it to file_backup.txt.
Example 4: Move Multiple Files at Once#
1
|
mv file1.txt file2.txt file3.txt /documents/
|
Output:
Moves three files to the /documents/ directory. The last argument is the destination.
Example 5: Move a Directory#
Output:
Moves folder1 directory to /backup/. Unlike cp, no -r option needed.
Example 6: Rename a Directory#
1
|
mv old_folder new_folder
|
Output:
Renames old_folder to new_folder.
Example 7: Move with Confirmation (-i)#
1
|
mv -i source.txt destination.txt
|
Output:
1
|
mv: overwrite 'destination.txt'?
|
Prompts for confirmation if destination.txt exists. Press y to overwrite, n to cancel.
Example 8: Verbose Output (-v)#
1
|
mv -v file1.txt file2.txt /documents/
|
Output:
1
2
|
'file1.txt' -> '/documents/file1.txt'
'file2.txt' -> '/documents/file2.txt'
|
Shows which files were moved.
Example 9: Move with Wildcards#
Output:
Moves all .txt files to the documents directory.
Example 10: Backup Before Overwrite (-b)#
1
|
mv -b newfile.txt oldfile.txt
|
Output:
1
2
3
|
(No output on success)
ls
newfile.txt oldfile.txt~
|
If oldfile.txt exists, creates oldfile.txt~ as backup before overwriting.
Example 11: Move Multiple Files with -t Option#
1
|
mv -t /destination/ file1.txt file2.txt file3.txt
|
Output:
-t lets you specify the destination first. Handy in scripts.
Example 12: No Overwrite (-n)#
1
|
mv -n source.txt destination.txt
|
Output:
If destination.txt exists, skips without overwriting.
Tips & Notes#
Use -i for Safety#
Avoid accidentally overwriting important files by using -i.
1
|
mv -i source.txt destination.txt
|
Essential when working with important files.
Difference from cp#
1
2
3
4
5
6
7
|
# cp: original file remains
cp file.txt /backup/
ls file.txt # Still exists
# mv: original file is gone
mv file.txt /backup/
ls file.txt # Doesn't exist (moved)
|
mv moves the file, so the original disappears.
No -r Needed for Directories#
1
2
3
4
5
|
# cp requires -r
cp -r folder /backup/
# mv doesn't need -r
mv folder /backup/
|
mv can move directories directly.
Moving Within Same Directory#
1
2
3
4
5
|
# Rename in current directory
mv oldname.txt newname.txt
# Move with relative path
mv file.txt ./subfolder/
|
Check if Destination Exists#
1
2
3
|
# Avoid errors by creating directory first
mkdir -p /backup
mv file.txt /backup/
|
Set Up Aliases#
1
2
|
# Add to ~/.bashrc
alias mv='mv -i'
|
Always move with confirmation.
Changing Case in Filename#
1
2
3
4
5
6
7
|
# Linux distinguishes case - direct change works
mv file.txt FILE.TXT
# macOS with case-insensitive filesystem
# Need intermediate step
mv file.txt temp.txt
mv temp.txt FILE.TXT
|
Practical Usage#
Organize Project Files#
1
2
3
4
5
|
# Move test files to tests directory
mv *_test.py tests/
# Move old files to archive
mv old_*.txt archive/
|
Log File Rotation#
1
2
3
4
|
# Archive current log with date
mv app.log app_$(date +%Y%m%d).log
# New log file will be created automatically
|
Batch Rename#
1
2
3
4
5
6
7
8
9
|
# Change extension
for file in *.txt; do
mv "$file" "${file%.txt}.md"
done
# Add prefix
for file in *.jpg; do
mv "$file" "photo_$file"
done
|
Backup File Management#
1
2
3
4
|
# Move to dated backup directory
BACKUP_DIR="/backup/$(date +%Y%m%d)"
mkdir -p "$BACKUP_DIR"
mv old_project "$BACKUP_DIR/"
|
Organize Downloads#
1
2
3
4
5
6
7
8
9
10
11
|
#!/bin/bash
# Organize downloads folder
# Images
mv ~/Downloads/*.{jpg,png,gif} ~/Pictures/ 2>/dev/null
# Documents
mv ~/Downloads/*.{pdf,doc,docx} ~/Documents/ 2>/dev/null
# Videos
mv ~/Downloads/*.{mp4,avi,mkv} ~/Videos/ 2>/dev/null
|
Swap Configuration Files#
1
2
3
4
5
|
# Apply new config
mv config.new config.yml
# Backup old config
mv -b config.new config.yml # Creates config.yml~
|
Safe Move in Scripts#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
#!/bin/bash
set -e
SOURCE="file.txt"
DEST="/backup/file.txt"
if [ -e "$SOURCE" ]; then
if [ -e "$DEST" ]; then
echo "Warning: $DEST already exists"
read -p "Overwrite? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
mv "$SOURCE" "$DEST"
echo "Move completed"
fi
else
mv "$SOURCE" "$DEST"
echo "Move completed"
fi
else
echo "Error: $SOURCE does not exist" >&2
exit 1
fi
|
Summary#
Key points about the mv command:
- Move or rename files and directories
- -i: Prompt before overwrite (super important!)
- -v: Verbose output
- -n: Do not overwrite
- -b: Backup before overwrite
- No
-r needed for directories
- Original file doesn’t remain (unlike cp)
- Common combinations:
mv -i, mv -v
Moving and renaming are basic operations. Make -i a habit to protect your important files!