簡體   English   中英

ls(列表命令)輸出目錄中所有文件的前綴文件名

[英]Prefix filename in for all the files in directory for ls (list command) output

我已經將一些文件下載到目錄中。

我想給所有文件加上001、002,..... 050前綴,如編號順序

但是我希望文件位於文件創建順序中

ls -c按創建順序列出文件 在此處輸入圖片說明

前綴應采用以下方式

  • 001_添加方法...
  • 002_Breaking Our App ......

我努力了

for i in "$(ls -c)";
  do
   echo i;
done;

echo i打印的文件名是好的。 但是我想在自動生成的數字前面加上001,002,003,.....的前綴

嘗試使用循環,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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM