[英]Prefix filename in for all the files in directory for ls (list command) output
嘗試使用循環,let和printf進行填充:
a=1
for i in *; do
new=$(printf "%03d" "$a") #03 pad to length of 3
mv -i -- "$i" "$new$i"
let a=a+1
done
使用-i標志可防止自動覆蓋現有文件。
試試這個作為當前目錄:
c=0
find . -maxdepth 1 -type f -printf '%A@ %f\n' |
sort -n -k1 |
while IFS= read -r line; do
echo mv "${line#* }" "$(printf '%03d_%s\n' $((++c)) "${line#* }")"
done
而且請不要解析ls輸出
注意:測試正常后,請刪除mv
命令的echo
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.