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?

EDIT:

Operating system is Solaris 10.

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

link|improve this question
Are you looking for actual space used on disk, or true file size? – justarobert Mar 31 '11 at 8:38
feedback

5 Answers

up vote 4 down vote accepted

This should just work:

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

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.

link|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
feedback

Just use du -b example:

# du -sh .

215G .

# du -sbh .

344G .

link|improve this answer
feedback

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
    fi
    find "$SEARCH_PATH" -ls | gawk --lint --posix '
        BEGIN {
            split("B KB MB GB TB PB",type)
            ls=hls=du=hdu=0;
            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 
link|improve this answer
feedback

man du would probably help here:

 --apparent-size
      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
link|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. – syneticon-dj Apr 8 '11 at 20:15
3  
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. – syneticon-dj Feb 21 at 8:32
feedback

Your Answer

 
or
required, but never shown

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