通过使用bash添加值来重命名文件

发布于 2025-01-20 05:19:28 字数 242 浏览 1 评论 0原文

我有一系列在目录和子目录中具有相似扩展名的文件。我想为他们的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撧情箌佬 2025-01-27 05:19:28

假设:

  • 顶级目录是“ ParentDirectory”及其子目录
    包含要重命名的文件。
  • 文件具有扩展名为“ .ext”。
  • 文件名从数字开始,不包含其他数字
    在文件名的中间。

然后,您可以尝试bash脚本:

#!/bin/bash

find ParentDirectory -type f -name "*.ext" | while IFS= read -r path; do
    dir=${path%/*}              # extract the directory name
    fname=${path##*/}           # extract the file name
    num=${fname//[^0-9]/}       # number part of the file name
    remain=${fname//[0-9]/}     # remaining part of the file name
    (( num += 20 ))             # add 20 to the number
    newpath="$dir/$num$remain"  # compose the new filename
    if [[ -f $newpath ]]; then  # make sure the new filename doesn't duplicate
        echo "$newpath already exists. skip renaming."
    else
        echo mv -- "$path" "$newpath"
                                # print the mv command
    fi
done

它以干式运行输出mv命令。如果输出看起来不错,
drop echomv命令之前,然后再次运行。

正如Barmar建议的那样,如果Perl Rename命令可用,请尝试
相反:

shopt -s globstar
rename -n 's/\d+(?!.*\/)/
amp; + 20/e' ParentDirectory/**/*.ext
  • REGEX \ d+(?!。
    它避免匹配目录名称中包含的数字。
  • e修改器在重命名命令的末尾告诉perl
    评估替代$& + 20作为perl表达式
    而不是字面的替代字符串。
  • parentdirectory/**/*。ext由于
    shopt -s globstar选项。
  • -n选项仅在没有操作的情况下打印文件名。如果输出看起来不错,请删除-n

请注意,有两种不同的重命名命令:“ Perl Rename”
并根据分布的不同,“非透支重命名”。您可以确定
通过查看MAN重命名来安装哪一个。如果是
包括perl的单词,它是Perl Rename命令。

Assuming:

  • The top directory is "ParentDirectory" and its subdirectories
    contain the files to be renamed.
  • The files have extension ".ext".
  • The filenames start with the number and do not contain other numbers
    in the middle of the filenames.

Then would you please try the bash script:

#!/bin/bash

find ParentDirectory -type f -name "*.ext" | while IFS= read -r path; do
    dir=${path%/*}              # extract the directory name
    fname=${path##*/}           # extract the file name
    num=${fname//[^0-9]/}       # number part of the file name
    remain=${fname//[0-9]/}     # remaining part of the file name
    (( num += 20 ))             # add 20 to the number
    newpath="$dir/$num$remain"  # compose the new filename
    if [[ -f $newpath ]]; then  # make sure the new filename doesn't duplicate
        echo "$newpath already exists. skip renaming."
    else
        echo mv -- "$path" "$newpath"
                                # print the mv command
    fi
done

It outputs the mv commands as a dry run. If the output looks good,
drop echo before mv command and run again.

As Barmar suggests, if perl rename command is available, please try
instead:

shopt -s globstar
rename -n 's/\d+(?!.*\/)/
amp; + 20/e' ParentDirectory/**/*.ext
  • The regex \d+(?!.*\/) matches number not followed by a slash.
    It avoids to match numbers included in the directory names.
  • The e modifier at the end of the rename command tells perl
    to evaluate the substitution $& + 20 as a perl expression
    rather than a literal substitution string.
  • ParentDirectory/**/*.ext is expanded recursively due to the
    shopt -s globstar option.
  • The -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 it
includes the word Perl, it is the perl rename command.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文