DU + LS


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers DU + LS
# 1  
Old 04-19-2011
DU + LS

Just curious if anyone had written a function that combined the "du" and "ls" command so you get a list of directories and files in the current directory, each with a file size. I googled a bit but didn't find one. I didn't want to re-invent the wheel if it had already been done.
# 2  
Old 04-19-2011
ls doesn't list just directories, though.

If you're feeding its output into something else, find is usually better. It's like an ls that defaults recursive, with configurable filters, that can be told to automatically run what you want for every matching it finds.

Code:
find . -type d -maxdepth 1 -mindepth 1 -exec du -hs '{}' ';'

To break it down:
Code:
# look inside . aka the current directory
find .
#find only directories
 -type d
# Don't print . itself, look 1 level deeper.
 -mindepth 1
# ...but don't look any deeper than that.
 -maxdepth 1
# run "du -hs ./dir" for every matching entry 
 -exec du -hs '{}' ';'

All one command with no pipes, when with ls you'd need to ls -l and do some complex grepping/sedding/awking to get only the dirs, and then get the filename out of that mess.
# 3  
Old 04-20-2011
Code:
/usr/bin/du . -h --max-depth=1 | sort -n

# 4  
Old 04-22-2011
Quote:
Originally Posted by Corona688
Code:
find . -type d -maxdepth 1 -mindepth 1 -exec du -hs '{}' ';'

With find (GNU findutils) 4.4.2:
Code:
find . -type d -maxdepth 1 -mindepth 1 -exec du -hs '{}' ';'
find: warning: you have specified the -maxdepth option after a non-option argument -type, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it).  Please specify options before other arguments.

find: warning: you have specified the -mindepth option after a non-option argument -type, but options are not positional (-mindepth affects tests specified before it as well as those specified after it).  Please specify options before other arguments.

If you are using GNU find, try:
Code:
find . -maxdepth 1 -mindepth 1 -type d -exec du -hs '{}' ';'

 
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question