
我正在嘗試使用find . -type d -print | wc -m
where-m
或--chars
will 列印資料夾名稱的字元數。
以下面的螢幕截圖為例,字元數為臨時資料夾+臨時資料夾+叮咚資料夾只是34,並且計數斜線和點的事件仍然是40。為什麼指令要印出來43人物?額外的3個角色是從哪裡來的?
答案1
find . -type d -print | tr -d '\n' | wc -m
tr
將刪除所有換行符號並wc
計算您想要的數量。
答案2
顯然尾隨換行符也被算作一個字元。
假設您想要每個目錄的字元數,您可以這樣做
$ find -type d -exec bash -c 'echo -n $0 | wc -m' {} \;
1
12
27
加起來就是您要找的 40 個。請注意,echo -n
刪除了尾隨的換行符。
將這三者相加也是可能的。
$ find -type d -exec bash -c 'echo -n $0 | wc -m' {} \; | awk '{S+=$1} END {print(S)}'
40
答案3
將 bash/ksh/zsh 陣列設定ct
為 的輸出wc -lm
,然後使用 shell 算術。
ct=($(find . -type d -print |wc -lm)); echo $((ct[1]-ct[0]))
或使用字段分割和位置參數:
set $(find . -type d -print |wc -lm); echo $(($2 - $1))