mv
Move (rename) files or directories
TLDR
Rename a file or directory when the target is not an existing directory
Move a file or directory into an existing directory
Move multiple files into an existing directory, keeping the filenames unchanged
Do not prompt for confirmation before overwriting existing files
Prompt for confirmation interactively before overwriting existing files, regardless of file permissions
Do not overwrite existing files at the target
Move files in verbose mode, showing files after they are moved
Specify target directory so that you can use external tools to gather movable files
SYNOPSIS
mv [OPTION]... SOURCE DESTINATION
mv [OPTION]... SOURCE... DIRECTORY
mv [OPTION]... -t DIRECTORY SOURCE...
PARAMETERS
-f, --force
Do not prompt before overwriting an existing file.
-i, --interactive
Prompt before overwriting an existing file.
-n, --no-clobber
Do not overwrite an existing file. This is the opposite of -f.
-u, --update
Move only when the SOURCE file is newer than the DESTINATION file or when the DESTINATION file is missing.
-v, --verbose
Explain what is being done. Shows the names of files as they are moved.
-t, --target-directory=DIRECTORY
Move all SOURCE arguments into DIRECTORY. Useful for moving multiple files to a specific directory.
-b, --backup[=CONTROL]
Make a backup of an existing destination file before overwriting it. CONTROL can specify the backup method.
-S, --suffix=SUFFIX
Override the usual backup suffix (e.g., '~' or '.bak').
-T, --no-target-directory
Treat DESTINATION as a normal file, not a directory, even if it has the same name as an existing directory. Useful when renaming a file to a name that matches an existing directory.
DESCRIPTION
The mv command is a fundamental Unix utility used to move files and directories from one location to another, or to rename a file or directory within the same location.
When moving files or directories within the same filesystem, mv performs an atomic operation by simply updating the directory entry, which is very fast and preserves the inode number. This means permissions, ownership, and timestamps are generally retained.
However, when moving files or directories across different filesystems, mv operates by copying the source to the destination and then deleting the original. This process is not atomic and can be slower, especially for large files. In such cases, new permissions and ownership might be applied based on the destination filesystem's defaults or the user's umask settings. If the copy fails, the original file might still exist, providing some fault tolerance.
CAVEATS
When moving files across different filesystems, mv behaves as a copy-and-delete operation, which is not atomic. This means if the process is interrupted (e.g., due to power loss), the file might be incomplete or missing from both locations. Ensure sufficient disk space on the destination when moving large files across filesystems. Permissions are crucial: you must have write permissions in both the source and destination directories.
ATOMIC RENAMING
On the same filesystem, mv uses the rename() system call, which is an atomic operation. This guarantees that a file is either fully moved/renamed or remains in its original state, even during system crashes, preventing data corruption for single files.
DIRECTORY HANDLING
When moving a directory, mv moves the directory itself and all its contents (subdirectories and files). If the destination directory already exists, the source directory becomes a subdirectory of the destination. If the destination does not exist, the source directory is renamed to the destination name.
HISTORY
The mv command has been a staple of Unix-like operating systems since their inception, providing a fundamental file system manipulation capability. It's part of the core utilities, standardized by POSIX, ensuring consistent behavior across various systems. Over time, GNU Coreutils and other implementations have added useful options like -t (target directory) and -u (update) to enhance its flexibility and safety, while maintaining its original simplicity for basic operations.