Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to check directory size

2,295 views
Skip to first unread message

ignace

unread,
Jan 24, 2003, 2:43:26 AM1/24/03
to
I would like to know how to check the size of a directory. I mean with
all subdirectories and files included (within the boundary of the file
syststem of course.)

if I do cd /usr ; ls -lF ,
the sizes of the directories are 1024 , I guess this is related to
block or fragment size.

with df /folder, i get the number of blocks for the whole filesystem
which is not very significative for me.

with du /path/to/folder, i get a listing.

may be i ll have to check in specific options in the man.


can you help me

thanks.

Chris Watkins

unread,
Jan 24, 2003, 6:53:47 AM1/24/03
to

du -sk /path/to/directory


--
Chris "hehehe... he said folder" Watkins

Alan D Johnson

unread,
Jan 24, 2003, 7:30:16 AM1/24/03
to
Chris Watkins wrote:

if you use du -sxk it will only give you the total size instead of
giving all of the subdirectory sizes also.

Chris Watkins

unread,
Jan 24, 2003, 7:57:10 PM1/24/03
to

That's what the "-s" option does, I thought.
I know "-k" gives the total, in 1024 byte blocks.
I'll have to look and see what "-x" does, next week.
I've just always used "du -sk /directory", myself, with the output being
the overall total of anything under that directory. It doesn't give a
total for each subdir, it just adds each to the grand total.


--
Chris

Alan D Johnson

unread,
Jan 25, 2003, 7:16:49 AM1/25/03
to
Chris Watkins wrote:

Sorry, you are correct, the x sets it so that it only gathers totals
from the same device, instead of all the subs underneath that are
separate mount points, I'm just slightly confused, you know you use
something for so long and dont even remeber why :-)

Chris Watkins

unread,
Jan 25, 2003, 7:56:46 AM1/25/03
to


I know how that goes. There are several "habitual" commands I use, that
I probably couldn't tell you what each option does, without looking.

I only remembered the -s and -k to du, because that's a far more recent
addition to my arsenal. So many command line options... so little time :-)


--
Chris

Umberto Quaia

unread,
Jan 25, 2003, 11:45:13 AM1/25/03
to

Of course. :-)

du -sk /path/to/dir

gives space usage summary for given dir.

du -sk /path/to/dir/*/

gives identical summary for each subdirectory.


For a more exotic, but potentially more flexible, method:

find /path/to/dir -ls | awk '{sum+=$7};END{print sum}'


The last command can be tailored to you needs. ;-)

Need just files modified more than a week ago?

find /path/to/dir -mtime +7 -ls | awk '{sum+=$7};END{print sum}'

Only tar.gz files?

find /path/to/dir -name '*.tar.gz' -ls | awk '{sum+=$7};END{print sum}'

man find gives you all options available.


Enjoy! :-)

Umberto

Last consideration.
If hard links are involved, space reported is bigger, because linked
files are counted twice or multiple times, depending on number of links.

But, wait, I have just now a wonderful idea to solve that as well. :-)
I will post that when I've worked on that. I must first switch to my
FreeBSD partition, check if it works, then I will post it if it works...


Umberto Quaia

unread,
Jan 25, 2003, 11:45:13 AM1/25/03
to

Of course. :-)

Umberto Quaia

unread,
Jan 25, 2003, 1:47:41 PM1/25/03
to
"Umberto Quaia" <umbert...@tin.it> wrote in message news:b0ueqg$tepak$1...@ID-163428.news.dfncis.de...

> "ignace" <lao.i...@lycos.com> wrote:
> > I would like to know how to check the size of a directory. I mean with
> > all subdirectories and files included (within the boundary of the file
> > syststem of course.)
> >
> > if I do cd /usr ; ls -lF ,
> > the sizes of the directories are 1024 , I guess this is related to
> > block or fragment size.
> >
> > with df /folder, i get the number of blocks for the whole filesystem
> > which is not very significative for me.
> >
> > with du /path/to/folder, i get a listing.
> >
> > may be i ll have to check in specific options in the man.
> >
> > can you help me
>
> For a more exotic, but potentially more flexible, method:
>
> find /path/to/dir -ls | awk '{sum+=$7};END{print sum}'
>
> Last consideration.
> If hard links are involved, space reported is bigger, because linked
> files are counted twice or multiple times, depending on number of links.
>
> But, wait, I have just now a wonderful idea to solve that as well. :-)
> I will post that when I've worked on that. I must first switch to my
> FreeBSD partition, check if it works, then I will post it if it works...
>

I'm back. I've already tried my idea on FreeBSD.

First of all, I've discovered that FreeBSD is ahead on the hard links
issue and their du already counts hard links just once. Good news.

Then I've refined that find|awk better.
I've used $2 field, number of 512-blocks allocated, which is more
accurate than $7, filesize. Then I've exploited the $4 field,
number of hard links, to divide by number of hard links, so that
space is attributed as a fraction to each link, except for
directories because find command does not list .. entries.

This is the result:

# df -k /usr
1948168
# du -sk /usr
1948168
Let's try the first version which doesn't account for links:
# find /usr -ls | awk '{sum+=$2};END{printf "%d\n",sum/2}'
1979164
And now the corrected one (backslash used to wrap to the other line):
# find /usr -ls | \
awk '{(if($3~/\<d/)sum+=$2;else sum+=$2/$4;END{printf "%d\n",sum/2}'
1948167

Success! Just a small rounding error due to floating point arithmetic.


I miss only the test on major platforms, like HP-UX, Solaris, AIX.

I have to check that "~/\<d/" is supported. It may be a gawk extension.

I've also discovered that FreeBSD filesystem caching is not so bad. Apart
from the very first time, following executions were quite fast. :-)

Umberto


Umberto Quaia

unread,
Jan 28, 2003, 10:48:20 AM1/28/03
to

Done tests.

For awk, ~/d.*/ works nicely.

On HP-UX, find -ls option is not present.
Workaround is (\ to wrap...):

find DIR -print | xargs ls -ladis | \
awk '{(if($3~/d.*/)sum+=$2;else sum+=$2/$4;END{printf "%d\n",sum/2}'

xargs avoids plenty of ls executions...

On Sun Solaris and AIX, -ls is corrected to Kb units already.
No need to divide sum by two... (again \ to wrap):

find DIR -ls | \
awk '{(if($3~/d.*/)sum+=$2;else sum+=$2/$4;END{printf "%d\n",sum}'

HP-UX workaround works on Sun and AIX too.
On AIX blocks are 1K, so no need to divide sum by two.


Umberto

0 new messages