Used to rename or move files or directories
The mv command is used to rename files or directories, or to move files from one directory to another. source represents the source file or directory, and target represents the destination file or directory. If a file is moved to an existing destination file, the content of the destination file will be overwritten.
The mv command can be used to move a source file to a destination file, or a set of files to a destination directory. Moving a source file to a destination file has two different outcomes:
mv effectively renames the file. When the destination is a directory, multiple source files or directories can be specified, and all will be moved into the destination directory, retaining their original names.Note: Unlike cp, mv is like "moving house"; it doesn't increase the total number of files. cp creates a copy, so the number of files increases.
mv [options] source target
--backup=<mode>: Back up files before overwriting.
-b: Create a backup before overwriting if the file exists.
-f: Force overwrite: directly overwrite existing files or directories without prompting.
-i: Interactive mode: prompt before overwriting an existing file. Enter "y" to overwrite or "n" to cancel. This helps prevent accidental data loss.
--strip-trailing-slashes: Remove trailing slashes from source arguments.
-S <suffix>: Use the specified suffix for backup files instead of the default.
--target-directory=<directory>: Specify the destination directory to move source files into.
-u: Update: only move files that are newer than the destination or if the destination doesn't exist.
Move all files from /usr/men to the current directory (.):
mv /usr/men/* .
Move a file:
mv file_1.txt /home/office/
Move multiple files:
mv file_2.txt file_3.txt file_4.txt /home/office/
mv *.txt /home/office/
Move a directory:
mv directory_1/ /home/office/
Rename a file or directory:
mv file_1.txt file_2.txt # Rename file_1.txt to file_2.txt
Rename a directory:
mv directory_1/ directory_2/
Print move information (verbose):
mv -v *.txt /home/office
Prompt before overwriting:
mv -i file_1.txt /home/office
Update only when source is newer than destination:
mv -uv *.txt /home/office
Do not overwrite any existing files:
mv -vn *.txt /home/office
Create backup when moving:
mv -bv *.txt /home/office
Unconditionally overwrite existing files:
mv -f *.txt /home/office