国际访客建议访问 Primers 编程伙伴 国际版站点 > Bash 教程 > mv 以获得更好的体验。

# Bash 的 mv 命令

mv [OPTION]... SOURCE... DIRECTORY

!subtitle:功能

移动(或重命名)文件。

!subtitle:类型

可执行文件(/usr/bin/mv),属于 coreutils

!subtitle:参数

  • OPTION 选项:

    • --backup[=CONTROL] - 选中怎样备份被覆盖的目标文件

    • -b - 备份被覆盖的目标文件

    • --debug - 输出调试信息(文件的复制方式)

    • -f, --force - 强制操作,目标存在且无法打开时会尝试将其删除

    • -i, --interactive - 覆盖前提示

    • -n, --no-clobber - 不覆盖现有文件,也不失败

    • --no-copy - 重命名失败时不复制文件

    • --strip-trailing-slashes - 删除每个 SOURCE 尾部的 /

    • -S, --suffix=SUFFIX - 设置备份文件的后缀

    • -t, --target-directory=DIRECTORY - 指定目标目录

    • -T, --no-target-directory - 将 DEST 视为文件(如果 DEST 是目录,则进行合并而非移动到它内部)

    • --update[=UPDATE] - 控制哪些现有文件需要更新;取值:all, none, older

    • -u - 仅在源文件比目标新的时候复制;同 --update[=older]

    • -v, --verbose - 输出详细信息(进行的全部操作)

    • -Z - 将目标文件的 SELinux 安全上下文设为默认类型

    • --help - 显示帮助

    • --version - 显示版本

  • SOURCE - 源文件列表

  • DEST - 目标路径

# 示例

!subtitle:移动文件

$ ls                            # 空目录下操作
$ touch src.txt                 # 创建文件
$ ls
src.txt
$ mv src.txt dest.txt           # 重命名文件
$ ls
dest.txt
$ mkdir dir                     # 创建目录
$ mv src.txt ./dir              # 移动到目录中
$ ls dir
src.txt

!subtitle:移动目录

$ mkdir src dest1 dest2         # 创建目录
$ touch src/1.txt               # 创建文件
$ tree src
src
└── 1.txt
$ mv src dest1                  # 将 src 移动到 dest1 内部
$ tree dest1
dest1
└── src
    └── 1.txt
$ mv -T dest1 dest2             # 移动 dest1 合并到 dest2
$ tree dest2
dest2
└── 1.txt

# 相关命令

命令 说明
cp 复制文件
ln 链接文件

# 推荐阅读

# 手册

MV(1)                            User Commands                           MV(1)

NAME
       mv - move (rename) files

SYNOPSIS
       mv [OPTION]... [-T] SOURCE DEST
       mv [OPTION]... SOURCE... DIRECTORY
       mv [OPTION]... -t DIRECTORY SOURCE...

DESCRIPTION
       Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.

       Mandatory  arguments  to  long  options are mandatory for short options
       too.

       --backup[=CONTROL]
              make a backup of each existing destination file

       -b     like --backup but does not accept an argument

       --debug
              explain how a file is copied.  Implies -v

       -f, --force
              do not prompt before overwriting

       -i, --interactive
              prompt before overwrite

       -n, --no-clobber
              do not overwrite an existing file

       If you specify more than one of -i, -f, -n, only the  final  one  takes
       effect.

       --no-copy
              do not copy if renaming fails

       --strip-trailing-slashes
              remove any trailing slashes from each SOURCE argument

       -S, --suffix=SUFFIX
              override the usual backup suffix

       -t, --target-directory=DIRECTORY
              move all SOURCE arguments into DIRECTORY

       -T, --no-target-directory
              treat DEST as a normal file

       --update[=UPDATE]
              control     which    existing    files    are    updated;    UP‐
              DATE={all,none,older(default)}.  See below

       -u     equivalent to --update[=older]

       -v, --verbose
              explain what is being done

       -Z, --context
              set SELinux security context of destination file to default type

       --help display this help and exit

       --version
              output version information and exit

       UPDATE controls which existing files in the destination  are  replaced.
       'all'  is  the  default operation when an --update option is not speci‐
       fied, and results in all existing files in the  destination  being  re‐
       placed.  'none' is similar to the --no-clobber option, in that no files
       in the destination are replaced, but also skipped files do not induce a
       failure.   'older' is the default operation when --update is specified,
       and results in files being replaced if they're older  than  the  corre‐
       sponding source file.

       The   backup   suffix   is  '~',  unless  set  with  --suffix  or  SIM‐
       PLE_BACKUP_SUFFIX.  The version control method may be selected via  the
       --backup  option  or  through the VERSION_CONTROL environment variable.
       Here are the values:

       none, off
              never make backups (even if --backup is given)

       numbered, t
              make numbered backups

       existing, nil
              numbered if numbered backups exist, simple otherwise

       simple, never
              always make simple backups

AUTHOR
       Written by Mike Parker, David MacKenzie, and Jim Meyering.

REPORTING BUGS
       GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
       Report any translation bugs to <https://translationproject.org/team/>

COPYRIGHT
       Copyright © 2023 Free Software Foundation, Inc.   License  GPLv3+:  GNU
       GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
       This  is  free  software:  you  are free to change and redistribute it.
       There is NO WARRANTY, to the extent permitted by law.

SEE ALSO
       rename(2)

       Full documentation <https://www.gnu.org/software/coreutils/mv>
       or available locally via: info '(coreutils) mv invocation'

GNU coreutils 9.4                 April 2024                             MV(1)
本文 更新于: 2025-11-27 09:38:15 创建于: 2025-11-27 09:38:15