Operating System - HP-UX
1821539 Members
2244 Online
109633 Solutions
New Discussion юеВ

** Getting Space Utilization on a Directory **

 
Michael Gretton
Frequent Advisor

** Getting Space Utilization on a Directory **

I would like to know which command (with appropriate switches) is best for reporting how much space is being used by a directory reported in K-Bytes or Megabytes. According to the man pages, du only reports blocks. I am trying to make this fit a script that management already uses and would like to get it to report more cleanly. It would also be helpful to know of a command that can tell me which files use the most space in a given directory but the first request is much more important. Thanks in advance if anyone can help.

9 REPLIES 9
harry d brown jr
Honored Contributor

Re: ** Getting Space Utilization on a Directory **

Bytes being used versus blocks being used are different. If, for example, a block were 512 characters, and FILEA was 1025 bytes, then it would consume 3 blocks, and if FILEB was 1400 bytes, it would also consume 3 blocks, therefore block consumsion is more important than bytes in use.


live free or die
harry


Live Free or Die
Patrick Wallek
Honored Contributor

Re: ** Getting Space Utilization on a Directory **

To get a summmary of the amount of space a directory is using in KB do:

du -ks

To see how much space, in KB, each sub-directory in a directory is using do:

du -k

To find the largest files in a directory you could do:

ll | sort +4n --> To sort in ascending size order

ll | sort +4nr --> To sort in descending size order
Helen French
Honored Contributor

Re: ** Getting Space Utilization on a Directory **

Hi Mike,

For the first request:

# du -k ( this will give you the space utilization in KB; this -k option will not list in the man pages of 10.20 but included in 11.X man pages, but it will work in both)

For finding out large files in directory:

# find /dir_path -xdev -size +10000 -exec ll {} \;

( this will list all files bigger than 10000 blocks. For more bigger files put big values)

HTH,
Shiju
Life is a promise, fulfill it!
Sridhar Bhaskarla
Honored Contributor

Re: ** Getting Space Utilization on a Directory **

Hi Michael,

It depends on the system administrator. Some administrators are used to blocks while some are to MBs. I prefer the metrics in MBs. To check a directory for utilization, I would do the following.

#cd /directory
#du -sk * |sort -n

I will find the biggest file/directory and will try to trim it.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
harry d brown jr
Honored Contributor

Re: ** Getting Space Utilization on a Directory **

This will do:

ls -l | tr -s " " ""|cut -d" " -f5|xargs echo|sed "s/ / + /g"|bc


There are TWO SPACES in the first quoted string for "tr", and ONE SPACE in the second quoted string. "cut" also has a SINGLE SPACE quoted string for the "-d" option.


live free or die
harry
Live Free or Die
Jeff Machols
Esteemed Contributor

Re: ** Getting Space Utilization on a Directory **

du -k will give you size in MB. if you do a du -ks .
it will give the total of the directory only

du -k

will give the size of all the sub-directories also
harry d brown jr
Honored Contributor

Re: ** Getting Space Utilization on a Directory **

Note the differences:

[root]pbctst: ls -l /tmp | tr -s " " ""|cut -d" " -f5|xargs echo|sed "s/ / +>
42374586

[root]pbctst: du -sk /tmp
44020 /tmp

[root]pbctst: echo "44020 * 1024"|bc
45076480

[root]pbctst: bdf /tmp
Filesystem kbytes used avail %used Mounted on
/dev/vg00/lvol4 65536 45329 19015 70% /tmp


live free or die
harry


Live Free or Die
Deshpande Prashant
Honored Contributor

Re: ** Getting Space Utilization on a Directory **

HI
#du -sk * will list space used by directories in current directory.
With sort you can arrange the output in ascending/descending order.
#du -sk |sort -nr (for reverse)
#du -sk |sort -n

Thanks.
Prashant.
Take it as it comes.
Robin Wakefield
Honored Contributor

Re: ** Getting Space Utilization on a Directory **

Hi Michael,

To search a directory within a filesystem, use something like:

find directory -type f -xdev | xargs ll | sort +4n | awk '{print $5,$NF}' | tail -n number_of_lines

Rgds, Robin.