我需要以字节为单位计算文件夹大小。如果文件夹名称包含空格/folder/withspaces/,则以下命令不能正常工作
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 .....