gpt4 book ai didi

shell - 通过 shell 或 linux 命令增加文件名编号

转载 作者:行者123 更新时间:2023-12-01 10:19:59 24 4
gpt4 key购买 nike

我有文件,例如

- public_00000.jpg
- public_00001.jpg
- ...
- public_00535.jpg

但是我想把这些文件做成

- public_05674.jpg
- public_05675.jpg
- ...
- public_06209.jpg

我的意思是,我想将文件名中的数字整体增加 +5674。

如何通过 Shell 或命令执行此操作?

提前致谢:)

最佳答案

能否请您尝试以下。

for file in *.jpg
do
first_filename_part="${file%_*}"
last_filename_part="${file#*.}"
var="${file#*_}"
count="${var%.*}"
((count = count + 5674))
printf "%s %s %s_%05d.%s\n" "mv" $file $first_filename_part $count $last_filename_part
done

上面只会在屏幕上打印命令,如:

mv public_00000.jpg public_05674.jpg


尝试仅运行 1 个命令,首先从终端上的打印输出开始,一旦您对结果感到满意,然后尝试执行以下操作,因为这将重命名所有文件。

for file in *.jpg
do
first_filename_part="${file%_*}"
last_filename_part="${file#*.}"
var="${file#*_}"
count="${var%.*}"
((count = count + 5674))
printf "%s %s %s_%05d.%s\n" "mv" $file $first_filename_part $count $last_filename_part | bash
done


来自手册页:我使用了参数扩展机制。

${parameter#word}

${parameter##word} Remove matching prefix pattern. The word is expanded to produce a pattern just as in pathname expansion. If the pattern matches the beginning of the value of parameter, then the result of the expansion is the expanded value of parameter with the shortest matching pattern (the #'' case) or the longest matching
pattern (the
##'' case) deleted. If parameter is @ or *, the pattern removal operation is applied to each positional parame- ter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with @ or *, the pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list.

${parameter%word}

${parameter%%word} Remove matching suffix pattern. The word is expanded to produce a pattern just as in pathname expansion. If the pattern matches a trailing portion of the expanded value of parameter, then the result of the expansion is the expanded value of parameter with the shortest matching pattern (the %'' case) or the longest
matching pattern (the
%%'' case) deleted. If parameter is @ or *, the pattern removal operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with @ or *, the pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list.

关于shell - 通过 shell 或 linux 命令增加文件名编号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59817649/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com