[英]wc with find. error if space in folders name
我需要以字節為單位計算文件夾大小。 如果文件夾名稱包含空格/文件夾/帶空格/,則以下命令無法正常工作
wc -c `find /folder -type f` | grep total | awk '{print $1}'
有錯誤
wc: /folder/with: No such file or directory
wc: spaces/file2: No such file or directory
怎么可能做到?
試試這條線:
find /folder -type f | xargs -I{} wc -c "{}" | awk '{print $1}'
您需要單獨引用的名稱。
$: while read n; # assign whole row read to $n
do a+=("$n"); # add quoted "$n" to array
done < <( find /folder -type f ) # reads find as a stream
$: wc -c "${a[@]}" | # pass wc the quoted names
sed -n '${ s/ .*//; p; }' # ignore all but total, scrub and print
壓縮成短的幾行 -
$: while read n; do a+=( "$n"); done < <( find /folder -type f )
$: wc -c "${a[@]}" | sed -n '${ s/ .*//; p; }'
這是因為 bash(不同於 zsh)字拆分命令替換的結果。 您可以使用數組來收集文件名:
files=()
for entry in *
do
[[ -f $entry ]] && files+=("$entry")
done
wc -c "${files[@]}" | grep .....
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.