Operating System - HP-UX
1819931 Members
3098 Online
109607 Solutions
New Discussion юеВ

How to get count total size of files under dir

 
SOLVED
Go to solution
diwa
Frequent Advisor

How to get count total size of files under dir

Hi,

as per one requirement i need to give the total size estimates of files under certain dir.
i want some simple command which can give me the correct total size of all files in bytes.
Is there any way that i can get size in MB or GB? If there are any simple way to do this then it will be very help ful for me?

Thanks
10 REPLIES 10
Pete Randall
Outstanding Contributor

Re: How to get count total size of files under dir

Try du:

du -sk /directory_name


Pete

Pete
James R. Ferguson
Acclaimed Contributor

Re: How to get count total size of files under dir

Hi:

# du -kxs /directory

...will return the total in 1K (1024) bytes

Regards!

...JRF...
spex
Honored Contributor
Solution

Re: How to get count total size of files under dir

> Is there any way that i can get size in MB or GB?

$ du -skx /mydir | awk '{printf("%.2f bytes, %.2f mbytes, %.2f gbytes\n",$1,$1/1024,$1/1048576)}'

PCS
Sp4admin
Trusted Contributor

Re: How to get count total size of files under dir

Hello,

You could do a "du -s" for the total of in the directory you are in or "du -s *" for all the the files in the directory.

sp,
Ernesto Cappello
Trusted Contributor

Re: How to get count total size of files under dir

Hi Diwa

du -sk .

BR.
Ernesto

---------------------------------------------
MAN du:

NAME
du - summarize disk usage

DESCRIPTION
The du utility writes to standard output the size of the file space allocated to, and the size of the file space allocated to each subdirectory of, the file hierarchy rooted in each of the specified files. The size of the file space allocated to a file of type directory is defined as the sum total of space allocated to all files in the file hierarchy rooted in the directory plus the space allocated to the directory itself. This sum will include the space allocated to any extended attributes encountered.

Files with multiple links will be counted and written for only one entry. The directory entry that is selected in the report is unspecified. By default, file sizes are written in 512-byte units, rounded up to the next 512-byte unit.


James George_1
Trusted Contributor

Re: How to get count total size of files under dir

Hi

# cd /dir

# du -sk * | sort -rn

Rgds / James
forum is for techies .....heaven is for those who are born again !!
diwa
Frequent Advisor

Re: How to get count total size of files under dir

Hi,

Thanks a ton to all.
Hi Spex,

Your Awk cmd is working fine. As JRF told du -ksx will return total in Kb, therefore can update your cmd like in place of bytes i am putting KB, please correct me if i am wrong:

$ du -skx /mydir | awk '{printf("%.2f Kbytes, %.2f mbytes, %.2f gbytes\n",$1,$1/1024,$1/1048576)}'
James R. Ferguson
Acclaimed Contributor

Re: How to get count total size of files under dir

Hi:

Rather than code your constant as 1048576 in:

# du -skx /mydir | awk '{printf("%.2f Kbytes, %.2f mbytes, %.2f gbytes\n",$1,$1/1024,$1/1048576)}'

...it is clearer to do:

## du -skx /mydir | awk '{printf("%.2f Kbytes, %.2f mbytes, %.2f gbytes\n",$1,$1/1024,$1/(1024*1024))}'

This makes the calculation much more obvious and is less likely to be wrong because of a typographical error.

Regards!

...JRF...
diwa
Frequent Advisor

Re: How to get count total size of files under dir

Hi JRF,

Thanks a lot. Now it is fine for me.
diwa
Frequent Advisor

Re: How to get count total size of files under dir

Hi JRF,

Thanks a lot. Now it is fine fro me.