Overview
The mv (move) command is used to change the location of files and directories or to rename them. In Linux, renaming a file and moving it to a different path are fundamentally the same operation within the file system; therefore, mv handles both tasks. It is an essential command for everything from daily file organization to automated rotation scripts.
Specifications (Arguments and Options)
Syntax
To rename a file:
mv [options] [current_filename] [new_filename]
To move files to a directory:
mv [options] [source_file...] [destination_directory]
Main Options
| Option | Description |
-f | Forces an overwrite without prompting (force). |
-i | Prompts for confirmation before overwriting an existing file (interactive). |
-u | Moves/overwrites only if the source file is newer than the destination (update). |
-t [dir] | Explicitly specifies the target directory. |
-b | Creates a backup of files being overwritten (appends ~). |
-S [suffix] | Specifies a custom suffix for backup files. |
--backup=[type] | Controls backup naming conventions (e.g., t for numbered backups). |
Basic Usage
1. Renaming a File
This changes the filename while keeping the data in its current location.
mv old_notes.txt new_notes.txt
2. Moving a File to a Directory
This relocates the file into the specified folder.
mv data.csv ./archive/
Practical Commands
1. Moving Multiple Files
List all source files followed by the destination directory.
mv file1.txt file2.txt file3.txt ./target_dir/
# Or using a wildcard
mv *.log ./logs/
2. Interactive Overwrite Protection
Use the -i option to avoid accidentally replacing important files.
mv -i manifest.json ./backup/
Example prompt:
mv: overwrite './backup/manifest.json'?
(Press 'y' to confirm, 'n' to cancel)
3. Creating Backups During Moves
To preserve the original file at the destination if a name collision occurs:
Simple Backup:
# Saves the existing file as filename~
mv -b update.bin ./system/
Numbered Backup:
Use –backup=t to keep multiple historical versions.
mv --backup=t app.log ./history/
Resulting files (viewed via ls):
app.log
app.log.~1~
app.log.~2~
4. Renaming with Brace Expansion
A shortcut to avoid typing the same filename twice.
# Renames config.yaml to config.yaml.old
mv config.yaml{,.old}
5. Moving Massive File Sets
When dealing with thousands of files, use find and xargs with the -t option to avoid “Argument list too long” errors.
# Find and move all .tmp files to the trash directory
find . -maxdepth 1 -name "*.tmp" -print0 | xargs -0 mv -t ./trash/
Customization Points
- Alias Check: Most systems set
alias mv='mv -i'. To ignore this and force an action, use a backslash:\mv. - Custom Suffixes: Change the default
~backup mark using-S, such asmv -b -S .bak file.txt ./dir/.
Important Notes
- Directory Behavior: If the destination is an existing directory, the source is moved inside it. If the destination does not exist, the source directory is renamed.
- Cross-Partition Moves: Moving files between different disks or partitions involves copying and then deleting, which takes time proportional to the file size.
- Permissions: You must have write and execute permissions on the parent directory to move or rename files within it.
Advanced Application
Batch Lowercasing Filenames
Combine mv with a for loop to rename multiple files at once.
# Convert all .JPG extensions to .jpg
for f in *.JPG; do
mv "$f" "${f%.JPG}.jpg"
done
Conclusion
The mv command is a vital utility for file management and system maintenance. Its backup and update options make it particularly effective for managing logs and configuration updates safely. Mastering the -t option alongside xargs ensures you can handle large-scale data organization without encountering shell limitations.
