Linux mv command
The Linux mv (short for "move file") command is used to rename files or directories, or to move them to other locations.
Syntax
mv [options] source dest
mv [options] source... directory
Parameter Description:
- -b: When the target file or directory already exists, a backup is created before overwriting it.
- -i: If the source directory or file specified for the move has the same name as the target directory or file, you will first be asked whether to overwrite the old file; enter y to overwrite directly, or n to cancel the operation.
- -f: If the source directory or file specified for the move has the same name as the target directory or file, no prompt is shown and the old file is overwritten directly.
- -n: Do not overwrite any existing file or directory.
- -u: The move is performed only when the source file is newer than the target file or the target file does not exist.
mv parameter settings and results
| Command format | Run Results |
|---|---|
mv source_file(文件) dest_file(文件) |
Rename the source file source_file to the target file dest_file |
mv source_file(文件) dest_directory(目录) |
Move the file source_file into the target directory dest_directory |
mv source_directory(目录) dest_directory(目录) |
If the directory dest_directory already exists, move source_directory into it; if dest_directory does not exist, rename source_directory to dest_directory |
mv source_directory(目录) dest_file(文件) |
Error |
Example
Rename the file aaa to bbb:
mv aaa bbb
Put the info directory into the logs directory. Note that if the logs directory does not exist, this command renames info to logs.
mv info/ logs
Another example: move all files and directories under /usr/runoob a given directory to the current directory, using the following command:
$ mv /usr/runoob/* .
Linux Command Reference