tags:

views:

53

answers:

4

I'm trying to get the size of the top level directories under the current directory (on Solaris). So I'm piping du to grep and want to match only those lines that have a single forward slash ie. the top level directories.

Something like:

du -h | grep -e <your answer here>

but nothing I try works. Help appreciated!

+2  A: 
grep -e '^[^/]*/[^/]*$'

Note that this matches lines that have exactly one (not at most one) slash, but that should be OK for your usage.

You could also probably do something with the -s switch

du -hs */
Matti Virkkunen
Yes they both work, thanks. Although with grep, the -v option given below is a little easier to remember ;-)
David
A: 

This doesn't answer your question exactly, but why don't you ask gdu to do that for you?

gdu --max-depth=1

If you really want to go the grep way, how about this?

du -h| grep -v '/.*/'

This will filter out lines with two or more slashes, leaving you with those that have one or zero.

Nathan Fellman
Don't have gdu on Solaris :/ The grep works, thanks.
David
@David: I believe `du` has a similar switch
Nathan Fellman
+1  A: 

You can also match the things you do not want with the -v option :

ptimac:Tools pti$ du | grep -v '/.*/'
22680   ./960-Grid-System
137192  ./apache-activemq-5.3.0
23896   ./apache-camel-2.0.0
386816  ./apache-servicemix-3.3.1
251480  ./apache-solr-1.4.0
345288  ./Community Edition-IC-96.SNAPSHOT.app

(I checked the solaris man page first now ;-)

There are other ways on GNU systems to skin that cat without using regex :

find . -d1 

finds all files/folders at a depth of 1

and a command I use often when cleaning housedisk is :

du -d1

or (and this should work on Solaris too)

du | sort -n

which shows me the largest directories wherever they are below the current directory.

Peter Tillemans
and then pipe that to `xargs gdu --max-depth=0`
Nathan Fellman
Yes the grep -v works, cheers. du | sort -n is useful too.
David
A: 
du --max-depth=1 -h
ghostdog74
No --max-depth option for du on Solaris :/
David