mv Command Cheat Sheet
mv moves (renames) files.
Synopsis
mv [OPTION]... SOURCE DEST
mv [OPTION]... SOURCE... DIRECTORY
Basic Usage
Rename File
mv old_name.txt new_name.txt
Move File to Directory
mv file.txt /home/user/Documents/
Move Multiple Files
mv file1.txt file2.txt /home/user/Documents/
Safety Options
Interactive (-i)
Prompt before overwriting an existing file.
mv -i source.txt existing_target.txt
# Output: mv: overwrite 'existing_target.txt'?
No Overwrite (-n)
Do not overwrite an existing file. Silently skip it.
mv -n source.txt existing_target.txt
Update Only (-u)
Move only if the SOURCE is newer than the DESTINATION (or DEST is missing).
mv -u new_file.txt old_file.txt
Backup Options
Create Backup (-b)
If target exists, rename the old one with ~ suffix.
mv -b config.xml /etc/app/config.xml
# Creates /etc/app/config.xml~
Custom Suffix (-S)
mv --backup=numbered file.txt target.txt
# Creates target.txt.~1~
Advanced: Atomic Moves
mv is atomic if moving within the same filesystem. Use this for updating files used by running services.
1. Write new config to config.tmp.
2. mv config.tmp config.final.
3. The service sees either the old file or the new file, never a half-written file.
Cross-Filesystem Moves:
If moving from / to /mnt/usb, mv actually does cp + rm. This is NOT atomic.
Troubleshooting
"Device or resource busy"
You cannot move a mount point or an open file/directory being used by the kernel in a specific way.
"Argument list too long"
Trying mv * destination/ in a directory with 1 million files?
Use find instead:
find . -name "*.jpg" -exec mv {} /destination/ \;
Notes
- Target: If target is a directory, source is moved into it. If target is a file (or doesn't exist), source is renamed to it.