Operating System - HP-UX
1844572 Members
2759 Online
110233 Solutions
New Discussion

Re: Summation of file sizes in directory

 
SOLVED
Go to solution
Harishs
Occasional Advisor

Summation of file sizes in directory

Hi ,
$ du -sk * ,it gives size of individual files in k bytes.

I want to get a summation of size of all files in directory ,could you please tell the command syntax to be used.

Thanks in anticipation.

Regards
harish
8 REPLIES 8
Senthil Prabu.S_1
Trusted Contributor
Solution

Re: Summation of file sizes in directory

Hi,
awk will help to sum the field.

du -sk * | awk '{ x += $1 } ;END { print "total bytes: " x }'

This should help you....

Have fun!!!!!

HTH,
Prabu.S
One man's "magic" is another man's engineering. "Supernatural" is a null word.
Robert-Jan Goossens
Honored Contributor

Re: Summation of file sizes in directory

# du -ks .
for your current directory or
# du -ks directory_name
for a specific directory.

Regards.
Robert-Jan
Oviwan
Honored Contributor

Re: Summation of file sizes in directory

Hey

only "du -sk" without the '*' will show you the size including the subdirectories

Regards
Peter Godron
Honored Contributor

Re: Summation of file sizes in directory

Harish,
as you have allocated 8 points to Senthils response there is room for improvement.

Although Senthils answer sums up the entries via du the result is in KB not bytes.

Also in your original spec you said files, but the current answer includes directories.

So, if you want the 'perfect' answer please provide more info on exactly what you want to sum up. Files only, current directory (with/without subdirectories), local file system only ....?
Harishs
Occasional Advisor

Re: Summation of file sizes in directory

Peter ,

In this case it was only files in directory.

It will be helpful if you can guide for sub directories as well.

Regards
harish
TwoProc
Honored Contributor

Re: Summation of file sizes in directory

Found this in my personal bin directory. I didn't write it, origin unknown. Works well though:

du -sk $* | \
awk '{i = i + $1; print $0;}
END {printf("---------------------\n%d KBytes\n", i);}'
We are the people our parents warned us about --Jimmy Buffett
Arturo Galbiati
Esteemed Contributor

Re: Summation of file sizes in directory

Hi Harish,
I use the attached dsr.ksh script to gather info about dir spaces/file.

HTH,
Art
Ralph Grothe
Honored Contributor

Re: Summation of file sizes in directory

TIMTOWTDI,
but this should sum up sizes of files beneath a certain directory, remaining on the same filesystem (otherwise omit the -xdev).
e.g. for my /opt (chose any appealing start dir, also conversion to K, M, G or whatever ad lib)

# find /opt -xdev -type f|xargs ll|awk '$5>0{s+=$5};END{printf"%10.2f MB\n",s/2^20}'
1230.96 MB
Madness, thy name is system administration