mv

Used to rename files or directories

Supplementary instructions

mv command is used to rename files or directories, or move files from one directory to another. Source represents the source file or directory, and target represents the target file or directory. If you move a file to an already existing destination file, the contents of the destination file will be overwritten.

The mv command can be used to move source files to a target file, or move a group of files to a target directory. There are two different results when the source file is moved to the target file:

Note: The results of mv and cp are different. mv seems to have "moved" the files, and the number of files has not increased. When cp copies files, the number of files increases.

grammar

mv(options)(parameters)

Options

--backup=<backup mode>: If you need to overwrite the file, back it up before overwriting it;
-b: When the file exists, create a backup for it before overwriting;
-f: If the target file or directory duplicates an existing file or directory, the existing file or directory will be overwritten directly;
-i: Interactive operation, the user is asked before overwriting. If the source file has the same name as the target file or a file in the target directory, the user is asked whether to overwrite the target file. The user inputs "y" to indicate that the target file will be overwritten; input "n" indicates to cancel the move of the source file. This prevents files from being overwritten by mistake.
--strip-trailing-slashes: Remove slashes "/" in source files;
-S<suffix>: Specify a suffix for the backup file instead of using the default suffix;
--target-directory=<directory>: Specify the source file to be moved to the target directory;
-u: Only perform the move operation when the source file is newer than the target file or the target file does not exist.

Parameters

Example

Move all files in the directory /usr/men to the current directory (indicated by .):

mv /usr/men/* .

Move files

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 directory

mv directory_1/ /home/office/

Rename a file or directory

mv file_1.txt file_2.txt # Rename the file file_1.txt to file_2.txt

Rename directory

mv directory_1/ directory_2/

Print mobile information

mv -v *.txt /home/office

Prompt whether to overwrite the file

mv -i file_1.txt /home/office

Updates are performed only when the source file is newer than the target file

mv -uv *.txt /home/office

Do not overwrite any existing files

mv -vn *.txt /home/office

Create backup while copying

mv -bv *.txt /home/office

Unconditionally overwrite existing files

mv -f *.txt /home/office