在 Linux 上重命名文件或者目录的 mv 命令的语法

要获得 mv 命令的命令行语法帮助,请在 Linux 或者 Unix shell 上执行以下命令。

[root@centos8 ~]# mv --help
Usage: mv [OPTION]... [-T] SOURCE DEST
  or:  mv [OPTION]... SOURCE... DIRECTORY
  or:  mv [OPTION]... -t DIRECTORY SOURCE...
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
  -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.
      --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
  -u, --update                 move only when the SOURCE file is newer
                                 than the destination file or when the
                                 destination file is missing
  -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
The backup suffix is '~', unless set with --suffix or SIMPLE_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
GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Full documentation at: <https://www.gnu.org/software/coreutils/mv>
or available locally via: info '(coreutils) mv invocation'

如何在 Linux 或者 Unix 中重命名文件

要重命名文件,我们可以使用 mv 命令,如下所示。

[root@centos8 ~]# mv -v file1 file2
renamed 'file1' -> 'file2'

在 Linux 上访问 mv 命令的帮助手册

为了简单起见并限制本文的范围,我们不在这里讨论每个命令行开关。
我们可以参考mv命令的GNU官方文档或者使用以下命令访问mv命令的手册。

[root@centos8 ~]# man mv

基于 CentOS 8 的 Linux 系统的mv手册如下。

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
       -f, --force
              do not prompt before overwriting
 Manual page mv(1) line 1 (press h for help or q to quit)
欢迎 on it road

如何在 Linux 或者 Unix 中重命名目录

要重命名目录,我们可以使用 mv 命令,如下所示。

[root@centos8 ~]# mv -v dir1 dir2
renamed 'dir1' -> 'dir2'

我们在上面的命令中使用了 -v 开关来在控制台上显示上面 mv 命令的输出。
但是,如果我们省略 -v 开关,即使成功执行, mv 命令也不会显示任何输出。

如何在 Linux 或者 Unix 中重命名文件或者目录

如何在 Linux 或者 Unix 中重命名文件或者目录?

事实上,Linux 或者 Unix 没有附带重命名文件或者目录的本机命令。

但是,GNU Coreutils 提供了 mv(move 的缩写)命令,用于将文件或者目录从一个位置移动到另一个位置。

通过在 mv 命令中利用此功能,我们可以重命名文件和目录。

日期:2020-09-17 00:13:47 来源:oir作者:oir