【问题标题】:batch prefix sequential numbers to file names批前缀序号到文件名
【发布时间】:2021-05-02 09:19:43
【问题描述】:

试图重命名一组以序号为前缀的文件。想用零填充单个数字。

03_file_9YIl.pdf
05_file_ehQh.pdf
08_file_fxUy.pdf
09_file_fxUy.pdf
 

问题: 已经是两位数的数字也会被填充。

04_file_9YIl.pdf
07_file_ehQh.pdf
08_file_fxUy.pdf
010_file_IEb1.pdf
014_file_kT9X.pdf
015_file_L6eG.pdf
020_file_t3QF.pdf
021_file_u00K.pdf

我尝试过的:

n=0
for f in *
 do 
    if [ "$f" = "rename.sh" ]
        then
            continue
    fi
printf -v new "%$((++n))_$f"
echo mv -v -- "$f" "$new"
done

任何帮助表示赞赏。谢谢!

【问题讨论】:

    标签: bash numbers rename sequential


    【解决方案1】:

    使用 find 的正则表达式标志来获取所需的确切文件,因此:

    while read file
    do
       fil=${file##*/}   # Extract the file name from the path
       dir=${file%/*}    # Extract the directory from the path
       echo "mv -f $line $dir/0$fil"
       # mv -f "$line" "$dir/0$fil" # Build the move command
    done <<< "$(find -maxdepth 1 -regextype posix-extended -regex '^.*/[[:digit:]]{1}_.*pdf$' -printf "%f\n")"
    

    一旦您确认命令的回显看起来像预期的那样,请取消注释实际的 mv 命令。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-04
      • 2023-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多