TrumanWong

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:

  1. If the target file is the path to a directory file, the source file will be moved to this directory and the file name will remain unchanged.
  2. If the target file is not a directory file, the source file name (there can only be one) will become the target file name and overwrite the existing file with the same name. If the source file and the target file are in the same directory, the function of mv is to change the file name. When the target file is a directory file, there can be multiple source files or directory parameters, and all source files will be moved to the target file. All files moved to this directory will retain their previous file names.

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

  • Source file: source file list.
  • Target file: If "target file" is a file name, rename it to "target file" while moving the file; if "target file" is a directory name, move the source file to "target file".

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