#Bash 的 file-rename 命令
file-rename [OPTION]... EXPRESSION FILE...
功能
通过表达式批量重命名文件。
这个命令是大多数 Linux 发行版上
rename命令的默认版本。可以通过
alternatives命令切换rename的版本:
- 注册
$ sudo alternatives --install /usr/bin/rename rename /usr/bin/file-rename 30- 切换
$ sudo alternatives --config rename
类型
可执行文件(/usr/bin/file-rename)。
参数
OPTION选项:-v,--verbose- 打印成功重命名的文件-0,--null- 从标准输入读取时使用空字符(\0)作为分隔符-n,--nono- 不实际重命名文件,查看哪些文件将会被重命名-f,--force- 运行覆盖现有文件--path,--fullpath- 重命名整个路径-d,--filename,--nopath,--nofullpath- 不要重命名中间目录,仅重命名路径的最后部分-m,--man- 打印 MAN 文档-u,--unicode [encoding]- 运行用户提供的代码时将文件名当作 Perl 字符串(Unicode);如果指定了encoding,则用该编码对文件进行编解码-e- 指定表达式EXPRESSION-E- 指定表达式EXPRESSION,以分号(;)结尾-h,--help- 显示帮助-V,--version- 显示版本
EXPRESSION- Perl 表达式- 变量
$_表示文件名 - 变量
$1或\1表示正则的第一个捕获;以此类推 s/xxx/yyy/表示将xxx替换为yyyy/abc/xyz/表示将abc映射为xyz
- 变量
FILE- 要重命名的文件列表
#转换函数
| 函数 | 作用 | 示例 |
|---|---|---|
uc($str) | 全大写 | uc($1) |
lc($str) | 全小写 | lc($1) |
ucfirst($str) | 首字母大写 | ucfirst($1) |
lcfirst($str) | 首字母小写 | lcfirst($1) |
#正则内的转换
| 转换 | 语法 | 作用 | 示例 |
|---|---|---|---|
| 全字符大写 | \U | 把后续字符转大写 | s/^(\w)/\U$1/ → file.txt → FILE.TXT |
| 全字符小写 | \L | 把后续字符转小写 | s/^(\w)/\L$1/ → FILE.TXT → file.txt |
| 单字符大写 | \u | 只把下一个字符转大写 | s/^(\w)/\u$1/ → file.txt → File.txt |
| 单字符小写 | \l | 只把下一个字符转小写 | s/^(\w)/\l$1/ → File.txt → file.txt |
| 结束标记 | \E | 结束 \U 或 \L 的作用 | s/^(\w)/\U$1\E.txt/ → file.txt → FILE.txt |
#示例
基础重命名
$ touch 1.txt 2.txt 3.txt # 创建一组文件
$ file-rename -v 's/\.txt$/.md/' * # 将 .txt 后缀替换为 .md
1.txt renamed as 1.md
2.txt renamed as 2.md
3.txt renamed as 3.md
集合映射
$ file-rename -v 'y/123/abc/' * # 将 123 映射为 abc
1.md renamed as a.md
2.md renamed as b.md
3.md renamed as c.md
正则捕获
$ file-rename -v 's/(.*)\.md/\1\1\1.md/' * # 将文件名重复 3 次
a.md renamed as aaa.md
b.md renamed as bbb.md
c.md renamed as ccc.md
(.*)创建一个捕获\1表示第一个捕获,\1\1\1重复三次
大小写转换
$ file-rename -v '$_ = uc($_)' * # 文件名转换为大写
aaa.md renamed as AAA.MD
bbb.md renamed as BBB.MD
ccc.md renamed as CCC.MD
$ file-rename -v '$_ = lc($_)' * # 文件名转换为小写
AAA.MD renamed as aaa.md
BBB.MD renamed as bbb.md
CCC.MD renamed as ccc.md
$ file-rename -v '$_ = ucfirst($_)' * # 首字母大写
aaa.md renamed as Aaa.md
bbb.md renamed as Bbb.md
ccc.md renamed as Ccc.md
$ file-rename -v '$_ = lcfirst($_)' * # 首字母小写
Aaa.md renamed as aaa.md
Bbb.md renamed as bbb.md
Ccc.md renamed as ccc.md
正则的大小写转换
$ file-rename -v 's/(.*?)\.md/\U$1\E.md/' *
aaa.md renamed as AAA.md
bbb.md renamed as BBB.md
ccc.md renamed as CCC.md
\U表示后续内容转换为大写,直到\E为止$1表示第一个正则捕获
#推荐阅读
#手册
RENAME(1p) User Contributed Perl Documentation RENAME(1p)
NAME
file-rename - renames multiple files
SYNOPSIS
file-rename [ -h|-m|-V ] [ -v ] [ -0 ] [ -n ] [ -f ] [ -d ] [ -u [enc]]
[ -e|-E perlexpr]*|perlexpr [ files ]
DESCRIPTION
"file-rename" renames the filenames supplied according to the rule
specified as the first argument. The perlexpr argument is a Perl
expression which is expected to modify the $_ string in Perl for at
least some of the filenames specified. If a given filename is not
modified by the expression, it will not be renamed. If no filenames
are given on the command line, filenames will be read via standard
input.
Examples (Larry Wall, 1992)
For example, to rename all files matching "*.bak" to strip the
extension, you might say
file-rename -- 's/\.bak$//' *.bak
To translate uppercase names to lower, you'd use
file-rename 'y/A-Z/a-z/' ./*
Examples rewritten to avoid globs which could inject options.
More examples (2020)
You can also use rename to move files between directories, possibly at
the same time as making other changes (but see --filename)
file-rename 'y/A-Z/a-z/;s/^/my_new_dir\//' ./*.*
You can also write the statements separately (see -e/-E)
file-rename -E 'y/A-Z/a-z/' -E 's/^/my_new_dir\//' -- *.*
You can use the predefined variables "$a, $b" in the code; for instance
to create sequences of numbers
rename -e '$a++;s/\w+/file_$a/' -- *.*
OPTIONS
-v, --verbose
Verbose: print names of files successfully renamed.
-0, --null
Use \0 as record separator when reading from STDIN.
-n, --nono
No action: print names of files to be renamed, but don't
rename.
-f, --force
Over write: allow existing files to be over-written.
--path, --fullpath
Rename full path: including any directory component. DEFAULT
-d, --filename, --nopath, --nofullpath
Do not rename directory: only rename filename component of
path.
-h, --help
Help: print SYNOPSIS and OPTIONS.
-m, --man
Manual: print manual page.
-V, --version
Version: show version number.
-u, --unicode [encoding]
Treat filenames as perl (unicode) strings when running the
user-supplied code.
Decode/encode filenames using encoding, if present.
encoding is optional: if omitted, the next argument should be
an option starting with '-', for instance -e.
-e Expression: code to act on files name.
May be repeated to build up code (like "perl -e"). If no -e,
the first argument is used as code.
-E Statement: code to act on files name, as -e but terminated by
';'.
ENVIRONMENT
No environment variables are used.
AUTHOR
Larry Wall
SEE ALSO
mv(1), perl(1)
DIAGNOSTICS
If you give an invalid Perl expression you'll get a syntax error.
BUGS
The original "rename" did not check for the existence of target
filenames, so had to be used with care.
perl v5.36.0 2024-01-01 RENAME(1p)