0

On the Bash on a Mac, It is possible to do something like

cd         # go to home directory
ls -d */

to get the name of the folders in the home directory.

And we can use

du -sh Documents

to get the size in human readable form the size of the folder.

I tried something like

ls -d */ | xargs du -sh 

but it won't work, due to some directories having the name Tmp Files (two words separated by a space), which made it du -sh Tmp Files.

Can this be modified or is there a simpler way to du -sh all top level folders and perhaps with a grand total?

1
  • I didn't know */... it seems it is to expand all directories only Mar 28, 2020 at 11:00

2 Answers 2

2

You can avoid the xargs:

du -sc */

Using xargs would only be useful if you have several thousands directories to avoid a "command line too long" error (but then would would not have a full total, only partial totals for each invocation of du by xarg).

Trick for other commands, to avoid the mis-parsing file names by xargs, have them separated by nulls instead of spaces or line feeds:

printf '%s\0' * | xargs -0 du -s 

(printf will loop over its arguments and print them with a NUL at the end and xarg will split on the NULs due to its -0 option). This assumes that the printf is a shell built-in (bash), using the binary would have the usual line length limit.

0
1

The grand total would be

du -s .

and the sizes of individual folders (and possible files)

du -s * .*

only the folders

du -s */  .*/
2
  • can you perhaps add what it means by */? Is it the same as on Bash and Zsh? Mar 28, 2020 at 10:52
  • ah, du -csh * and du -csh */ works well too, to show a grand total at last Mar 28, 2020 at 10:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .