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

# Bash 的 rename.ul 命令

rename.ul [OPTION]... EXPRESSION REPLACEMENT FILE...

现在大部分 Linux 发行版上默认的 rename 不是 rename.ul,而是功能更全面的 file-rename

可以通过 alternatives 命令切换 rename 的版本:

  1. 注册

    $ sudo alternatives --install /usr/bin/rename rename /usr/bin/rename.ul 30
    

  2. 切换

    $ sudo alternatives --config rename
    

!subtitle:功能

通过表达式重命名文件。

!subtitle:类型

可执行文件(/usr/bin/rename.ul),属于 util-linux

!subtitle:参数

  • OPTION 选项:

    • -s, --symlink - 不要重命名符号链接,但是修改它的指向

    • -v, --verbose - 显示被重命名的文件

    • -n, --no-act - 不实际修改文件名,配合 --verbose 查看哪些文件会被修改

    • -a, --all - 替换表达式的所有匹配位置,而不只是第一个匹配位置

    • -l, --last - 替换表达式的最后一个匹配位置,而不是第一个匹配位置

    • -o, --no-overwrite - 不要覆盖现有文件

    • -i, --interactive - 覆盖现有文件之前进行询问

    • -h, --help - 显示帮助

    • -V, --version - 显示版本

  • EXPRESSION - 匹配表达式;普通字符串,不支持通配符和正则表达式

  • REPLACEMENT - 文件名中匹配补番替换为的内容

  • FILE - 要重命名的文件列表

# 示例

!subtitle:批量重命名文件

$ rename.ul --last .txt .md *.txt       # 将 .txt 文件批量重命名为 .md 文件

!subtitle:批量修改符号链接指向

$ mv /path/to/dir1 path/to/dir2                 # 修改了某个目录名
$ rename.ul -s /path/to/dir1 /path/to/dir2 *    # 将符号链接中的 /path/to/dir1 批量修改为 /path/to/dir2

# 推荐阅读

# 手册

RENAME(1)                        User Commands                       RENAME(1)

NAME
       rename - rename files

SYNOPSIS
       rename [options] expression replacement file...

DESCRIPTION
       rename will rename the specified files by replacing the first
       occurrence of expression in their name by replacement.

OPTIONS
       -s, --symlink
           Do not rename a symlink but change where it points.

       -v, --verbose
           Show which files were renamed, if any.

       -n, --no-act
           Do not make any changes; add --verbose to see what would be made.

       -a, --all
           Replace all occurrences of expression rather than only the first
           one.

       -l, --last
           Replace the last occurrence of expression rather than the first
           one.

       -o, --no-overwrite
           Do not overwrite existing files. When --symlink is active, do not
           overwrite symlinks pointing to existing targets.

       -i, --interactive
           Ask before overwriting existing files.

       -h, --help
           Display help text and exit.

       -V, --version
           Print version and exit.

WARNING
       The renaming has no safeguards by default or without any one of the
       options --no-overwrite, --interactive or --no-act. If the user has
       permission to rewrite file names, the command will perform the action
       without any questions. For example, the result can be quite drastic
       when the command is run as root in the /lib directory. Always make a
       backup before running the command, unless you truly know what you are
       doing.

EDGE CASES
       If the expression is empty, then by default replacement will be added
       to the start of the filename. With --all, replacement will be inserted
       in between every two characters of the filename, as well as at the
       start and end.

       Normally, only the final path component of a filename is updated. (Or
       with --symlink, only the final path component of the link.) But if
       either expression or replacement contains a /, the full path is
       updated. This can cause a file to be moved between folders. Creating
       folders, and moving files between filesystems, is not supported.

INTERACTIVE MODE
       As most standard utilities rename can be used with a terminal device
       (tty in short) in canonical mode, where the line is buffered by the tty
       and you press ENTER to validate the user input. If you put your tty in
       cbreak mode however, rename requires only a single key press to answer
       the prompt. To set cbreak mode, run for example:

           sh -c 'stty -icanon min 1; "$0" "$@"; stty icanon' rename -i from to files

EXIT STATUS
       0
           all requested rename operations were successful

       1
           all rename operations failed

       2
           some rename operations failed

       4
           nothing was renamed

       64
           unanticipated error occurred

EXAMPLES
       Given the files foo1, ..., foo9, foo10, ..., foo278, the commands

           rename foo foo00 foo?
           rename foo foo0 foo??

       will turn them into foo001, ..., foo009, foo010, ..., foo278. And

           rename .htm .html *.htm

       will fix the extension of your html files. Provide an empty string for
       shortening:

           rename '_with_long_name' '' file_with_long_name.*

       will remove the substring in the filenames.

SEE ALSO
       mv(1)

REPORTING BUGS
       For bug reports, use the issue tracker at
       https://github.com/util-linux/util-linux/issues.

AVAILABILITY
       The rename command is part of the util-linux package which can be
       downloaded from Linux Kernel Archive
       <https://www.kernel.org/pub/linux/utils/util-linux/>.

util-linux 2.39.3                 2023-11-21                         RENAME(1)
本文 更新于: 2026-03-06 09:52:34 创建于: 2026-03-06 09:52:34