我想移动并重命名所有文件,dir
并在其上添加日期和数字结尾前面作为文件唯一的指示。我编写了一个小 bash 脚本来执行此操作,但它将数字放在文件名的前面。这可能吗,还是我需要走不同的路线?
I am getting abc20210407215936.pdf but I would like to get the output to be something like 20210407215936-1.pdf 20210407215936-2.pdf.
提前致谢。以下是脚本。
#!/bin/bash
d=$(date +%Y%m%d%H%M%S)
echo "$d"
cd /path/to/pdfs
counter=1
for f in *.pdf; do
mv -- "$f" "$d-$((counter)).pdf"
echo "files will be moved"
done
exit
答案1
您可以添加一个计数器并像这样实现它
#!/bin/bash
d=$(date +%Y%m%d%H%M%S)
echo "$d"
counter=0
for f in *.pdf; do
# Since you are using mv, be very sure to check it by echoing, ow, files will be renamed
mv -- "$f" "$d-$((++counter)).pdf"
done
exit