简体   繁体   中英

wc with find. error if space in folders name

I need to calculate folder size in bytes. if folder name contains space /folder/with spaces/ then following command not work properly

wc -c `find /folder -type f` | grep total | awk '{print $1}'

with error

wc: /folder/with: No such file or directory
wc: spaces/file2: No such file or directory

How can it done?

Try this line instead:

find /folder -type f | xargs -I{} wc -c "{}" | awk '{print $1}'

You need the names individually quoted.

$: 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

Compressed to short couple lines -

$: while read n; do a+=( "$n"); done < <( find /folder -type f )
$: wc -c "${a[@]}" | sed -n '${ s/ .*//; p; }'

This is because bash (different to zsh) word-splits the result of the command substitution. You could use an array to collect the file names:

files=()
for entry in *
do
  [[ -f $entry ]] && files+=("$entry")
done
wc -c "${files[@]}" | grep .....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM