【问题标题】:rename batch files, and add leading zero's before underscore重命名批处理文件,并在下划线前添加前导零
【发布时间】:2021-01-19 17:27:02
【问题描述】:

我有多个文件 Batch1_00000001Batch9999_00000001,其中包含以下文件名:

Batch1_00000001.pdf
Batch10_00000001.pdf
Batch100_00000001.pdf
Batch1000_00000001.pdf

首先,我想删除单词Batch 并将字母b 添加到文件名的前面。之后,我想在开始时添加最多三个前导零。这样b1_00000001.pdf 就变成了b0001_00000001.pdf 就像:

b0001_00000001.pdf
b0010_00000001.pdf
b0100_00000001.pdf
b1000_00000001.pdf

有没有人有解决方案来打破这一切,或者制作一个单一的正则表达式 rename 解决方案?提前致谢!

【问题讨论】:

标签: bash rename batch-processing batch-rename


【解决方案1】:

你可以试试:

#!/bin/bash
cd /directory_contain_files
for file in *.pdf; do
        index=$(echo $file | cut -d'_' -f1 | grep -o "[0-9]*")
        if [[ $index -lt 10 ]]; then
                index="000$index"
        elif [[ $index -lt 100 ]]; then
                index="00$index"
        elif [[ $index -lt 1000 ]]; then
                index="0$index"
        fi
        mv $file b${index}_00000001.pdf
done

【讨论】:

  • 这太完美了,谢谢你给出这个例子。我在玩正则表达式,但这很棒。尤其是这三个步骤:10、100、1000。非常感谢!
猜你喜欢
  • 1970-01-01
  • 2012-10-26
  • 1970-01-01
  • 2019-12-04
  • 2012-02-13
  • 2018-04-18
  • 2017-04-07
  • 2016-08-07
  • 2021-05-10
相关资源
最近更新 更多