List directories recursively showing its sizes using only ls and grep

ls -lhR | grep -e "total\|:$"
ls -lhR Lists everithing using -l "long listing format" wich includes the space used by the folder. Displays it in -h "human readable form" (i.e. 2.2G, 32K), and -R recurses subfolders. grep -e using a regex, show lines containing the word "total" or a ":" at the end of the line (those with the name of the folder) only.
Sample Output
total 60K
total 2.2G
total 32K

By: Sebasg
2013-01-22 04:58:51

These Might Interest You

  • I wanted an easy way to list out the sizes of directories and all of the contents of those directories recursively. Show Sample Output

    du -h --max-depth=1 /path/folder/
    ene2002 · 2013-07-09 19:56:13 0
  • This will check if there are any empty directories, or newly emptied directories, in a list of directories. It will then delete all of those directories. It works with gnu find and OSX/BSD find.

    for foo in <list of directories>; do while find $foo -type d -empty 2>/dev/null| grep -q "."; do find $foo -type d -empty -print0 | xargs -0 rmdir; done; done
    ClashTheBunny · 2012-05-23 08:09:16 0
  • 1. easy to type 2. linear list of recursive structure of files/directories 3. search in list using / 4. filter list by :g!//d 5. view/edit file in list by Vgf 6. count files/directories in list by :set nu and G 7. sort by :sort

    find | vim -
    nick_olya · 2016-02-29 14:21:42 0
  • Here's an annotated version of the command, using full-names instead of aliases. It is exactly equivalent to the short-hand version. # Recursively list all the files in the current directory. Get-ChildItem -Recurse | # Filter out the sub-directories themselves. Where-Object { return -not $_.PsIsContainer; } | # Group the resulting files by their extensions. Group-Object Extension | # Pluck the Name and Count properties of each group and define # a custom expression that calculates the average of the sizes # of the files in that group. # The back-tick is a line-continuation character. Select-Object ` Name, Count, @{ Name = 'Average'; Expression = { # Average the Length (sizes) of the files in the current group. return ($_.Group | Measure-Object -Average Length).Average; } } | # Format the results in a tabular view, automatically adjusted to # widths of the values in the columns. Format-Table -AutoSize ` @{ # Rename the Name property to something more sensible. Name = 'Extension'; Expression = { return $_.Name; } }, Count, @{ # Format the Average property to display KB instead of bytes # and use a formatting string to show it rounded to two decimals. Name = 'Average Size (KB)'; # The "1KB" is a built-in constant which is equal to 1024. Expression = { return $_.Average / 1KB }; FormatString = '{0:N2}' } Show Sample Output

    ls -r | ?{-not $_.psiscontainer} | group extension | select name, count, @{n='average'; e={($ | measure -a length).average}} | ft -a @{n='Extension'; e={$}}, count, @{n='Average Size (KB)'; e={$_.average/1kb}; f='{0:N2}'}
    brianpeiris · 2012-03-13 17:58:10 2

What Others Think

What is wrong with du -h ?
michelsberg · 282 weeks and 4 days ago
I don't have permission to run du (or to install it) on the server.
Sebasg · 282 weeks and 4 days ago
Ok, I thought du would exist on any relevant *nix since it is in the coreutils.
michelsberg · 282 weeks and 4 days ago
very good
aelam · 282 weeks and 4 days ago
you're right, i understand it's use. for those have root on machine: du -csh /var/* [me@this] 22 19:39 Tue Jan 22 > sudo du -shc /var/* 2.0M /var/backups 548M /var/cache 700K /var/crash 4.0K /var/games 195M /var/lib 4.0K /var/local 0 /var/lock 6.7M /var/log 4.0K /var/mail 4.0K /var/metrics 4.0K /var/opt 0 /var/run 88K /var/spool 11M /var/tmp 763M total
gvitalie · 282 weeks and 4 days ago
To memorize it better, german speakers can use ls -Roh | grep "insgesamt\|:$" ("Roh" means raw) ls -o is almost identical to ls -l, but does only list owner info (and no group name).
michelsberg · 282 weeks and 4 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: