使用for
循环遍历目录,使用另一个for
循环遍历文件。维护一个计数器,为每个文件增加 1。
没有用前导零填充数字的直接便捷方法。你可以打电话printf
,但是有点慢。一个有用的快速技巧是从 101 开始计数(如果您想要两位数 - 如果您想要 3 位数字,则为 1000,依此类推)并去掉前导的 1。
cd /root
for d in */; do
i=100
for f in "$d/"*; do
mv -- "$f" "$d/${d%/}_${i#1}.${f##*.}"
i=$(($i+1))
done
done
${d%/}
strips /
at the end $d
, ${i#1}
strips 1
at the start $i
and strips at the end, strips at the beginning and strips at the end, strips at the beginning and strips at the end, strips at the end and strips at the end, strips at the end and strips at the end, strips at the end, strips at the end, strips at the end, strips at the end, strips at the end, strips at the end, strips at the end, strips at the end, strips at the end, strips at the end, strips at the end, strips at the end, strips at the end, strips at the end, strips at the end, strips at the end, strips at the end, strips at the end, strips at the end 的 strips 和 strips at the start , 并${f##*.}
从$f
除最后一个之外的所有内容中剥离.
。这些构造记录在您的 shell 手册中的参数扩展部分中。
请注意,此脚本假定目标文件名不会与现有文件的名称冲突。如果您有一个名为 的目录img
,则某些文件将被覆盖。如果这可能是一个问题,最简单的方法是首先将所有文件移动到不同的目录,然后在重命名它们时将它们移回原始目录。