通过使用bash添加值来重命名文件
我有一系列在目录和子目录中具有相似扩展名的文件。我想为他们的Basename整数部分添加一个恒定的值。有什么方法可以使用bash?让我们以以下示例。
前: ../parentDirectory/directory/123-file.ext
在整数零件(123+20 = 143)添加20之后: ../parentDirectory/directory/143-file.ext
我假设需要一系列查找和重命名命令,但我不确定如何!
I have a series of files with similar extension in a directory and it's subdirectories. I'd like to add a constant value to their integer part of the basename. Is there any way to do it using Bash? Let's take following example.
Before:
../ParentDirectory/Directory/123-File.ext
after adding 20 to the integer part (123+20=143):
../ParentDirectory/Directory/143-File.ext
I assume a series of FIND and RENAME commands are needed, but I'm not sure how!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设:
包含要重命名的文件。
在文件名的中间。
然后,您可以尝试
bash
脚本:它以干式运行输出
mv
命令。如果输出看起来不错,drop
echo
在mv
命令之前,然后再次运行。正如Barmar建议的那样,如果
Perl Rename
命令可用,请尝试相反:
\ d+(?!。
它避免匹配目录名称中包含的数字。
e
修改器在重命名
命令的末尾告诉perl
评估替代
$& + 20
作为perl表达式而不是字面的替代字符串。
parentdirectory/**/*。ext
由于shopt -s globstar
选项。-n
选项仅在没有操作的情况下打印文件名。如果输出看起来不错,请删除-n
。请注意,有两种不同的
重命名
命令:“ Perl Rename”并根据分布的不同,“非透支重命名”。您可以确定
通过查看
MAN重命名
来安装哪一个。如果是包括
perl
的单词,它是Perl Rename
命令。Assuming:
contain the files to be renamed.
in the middle of the filenames.
Then would you please try the
bash
script:It outputs the
mv
commands as a dry run. If the output looks good,drop
echo
beforemv
command and run again.As Barmar suggests, if
perl rename
command is available, please tryinstead:
\d+(?!.*\/)
matches number not followed by a slash.It avoids to match numbers included in the directory names.
e
modifier at the end of therename
command tellsperl
to evaluate the substitution
$& + 20
as a perl expressionrather than a literal substitution string.
ParentDirectory/**/*.ext
is expanded recursively due to theshopt -s globstar
option.-n
option just prints the filenames with no action. If the output looks good, drop-n
.Please note there are two different
rename
commands: "perl rename"and "non-perl rename" depending on the distribution. You can determine
which one is installed by taking a look of
man rename
. If itincludes the word
Perl
, it is theperl rename
command.