Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I usually estimate the size of a whole directory tree using du -ks $DIRECTOY_TREE_ROOT, but this method cannot be used when zfs compression is on.

The total displayed by ls -l is ok for a single directory, but which is the simplest way to get the same result for a directory tree?


Operating system is Solaris 10.

I am looking for real file size, not the space used on disk.

share|improve this question
Are you looking for actual space used on disk, or true file size? – justarobert Mar 31 '11 at 8:38
up vote 5 down vote accepted

This should just work:

find . -type f -exec ls -l {} + | nawk '{s=s+$5}
END {print s}'
share|improve this answer

Just use du -b example:

# du -sh .

215G .

# du -sbh .

344G .

share|improve this answer

It is possible to get both file size and approximate disk usage direcly from command 'find' with the parameter '-ls'

 function lsdu() (
    export SEARCH_PATH=$*
    if [ ! -e "$SEARCH_PATH" ]; then
        echo "ERROR: Invalid file or directory ($SEARCH_PATH)"
        return 1
    find "$SEARCH_PATH" -ls | gawk --lint --posix '
        BEGIN {
            split("B KB MB GB TB PB",type)
            out_fmt="Path: %s \n  Total Size: %.2f %s \n  Disk Usage: %.2f %s \n  Compress Ratio: %.4f \n"
        NF >= 7 {
            ls += $7
            du += $2
        END {
            du *= 1024
            for(i=5; hls<1; i--) hls = ls / (2^(10*i))
            for(j=5; hdu<1; j--) hdu = du / (2^(10*j))
            printf out_fmt, ENVIRON["SEARCH_PATH"], hls, type[i+2], hdu, type[j+2], ls/du

Some sample command and output:

-bash-3.00# lsdu test_sloccount/
Path: test_sloccount/ 
  Total Size: 30.90 MB 
  Disk Usage: 1.43 MB 
  Compress Ratio: 21.6250 
share|improve this answer

This oneliner should produce the desired result:

find $DIRECTOY_TREE_ROOT -type d -exec ls -l '{}' \; | awk '/^total\ .[0-9]+$/ { sum+=$(NF) }END{ print sum }'

I don't have a ZFS partition to test it on, but on my ext4 partition it outputs the same result as du -ks.

share|improve this answer
The question has been edited to ask for the actual files size, not the one used on disk which both du and ls total are reporting. – jlliagre Apr 2 '11 at 12:53

man du would probably help here:

      print apparent sizes, rather than disk usage;  although
      the  apparent size is usually smaller, it may be larger
      due to holes in (`sparse') files,  internal  fragmenta-
      tion, indirect blocks, and the like
share|improve this answer
These is no such option os Solaris 10 du. It is a non standard Gnu extension. – jlliagre Apr 2 '11 at 10:07
Might be. Solaris Express 11 does have it, though. – the-wabbit Apr 8 '11 at 20:15
Solaris 10 doesn't bundle Gnu du. Solaris 11 Express has both /usr/bin/du that doesn't support the --apparent-size option and /usr/gnu/bin/du that supports it. – jlliagre Apr 8 '11 at 20:41
@jlliagre thanks for the insight. – the-wabbit Feb 21 '12 at 8:32

I'm going to include the answer to this question for FreeBSD for sake of completeness. According to man du:

 -A      Display the apparent size instead of the disk usage.  This can be
         helpful when operating on compressed volumes or sparse files.
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.