管道查找输出到 xargs 时的引用问题

the*_*fog 0 find xargs filenames

我有一个包含以下子目录的目录,即如果我运行 $ tree -L 1

.
??? 2007-06-20_to_2008-10-01
??? 2008-07-21_to_2008_08_12-Nokia 2mp
??? 2009-11-01_to_2011-01-10 - All iphone Pics
??? 2011-01-01 palliser-pics
??? 2011-03-10_to_2011-04-12-few iphone pics b4 switch to HTC
??? 2011-03-31-to-2013-05-01 ALL HTC pics-backup-prior-to-factory-reset
Run Code Online (Sandbox Code Playgroud)

我想查看这 2 个目录及其所有内容占用的磁盘空间量。
例如所需的输出,类似

2.7G  2007-06-20_to_2008-10-01
200MB 2008-07-21_to_2008_08_12-Nokia 2mp
1.3G  2009-11-01_to_2011-01-10 - All iphone Pics
667MB 2011-01-01 palliser-pics
2.3G  2011-03-10_to_2011-04-12-few iphone pics b4 switch to HTC
123MB 2011-03-31-to-2013-05-01 ALL HTC pics-backup-prior-to-factory-reset
Run Code Online (Sandbox Code Playgroud)

所以我一直在尝试:

find .  -maxdepth 1 -type d | xargs du -h 
Run Code Online (Sandbox Code Playgroud)

但是因为某些目录名称包含空格,find输出中的每一行在WORD传递给 之前都会产生许多's xargs,是否可以解决这个问题?

我知道问题的根本原因是由文件中的空格引起的,rename如果我找不到使用du,findxargs

Mic*_*aël 5

find | xargs应始终与-print0and结合使用-0,如中所述man find

   -print0
          True; print the full file name on the standard output, followed
          by a null character (instead of the newline character that
          -print uses).  This allows file names that contain newlines or
          other types of white space to be correctly interpreted by
          programs that process the find output.  This option corresponds
          to the -0 option of xargs.
Run Code Online (Sandbox Code Playgroud)

因此,您想要:
find . -mindepth 1 -maxdepth 1 -type d -print0 | xargs -0 du -sh
或没有find并使用足够进化的外壳 ( zsh):
print -N *(/) | xargs -0 du -sh
引导您到更简单的:
du -sh *(/)