10

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.

marcoc
  • 748
  • 4
  • 10

6 Answers6

13

Just use du -b example:

# du -sh .
215G    .

# du -sbh .
344G    .
code_dredd
  • 156
  • 1
  • 1
  • 11
Woyteck
  • 131
  • 2
5

This should just work:

find . -type f -exec ls -l {} + | nawk '{s=s+$5}
END {print s}'
jlliagre
  • 8,861
  • 18
  • 36
3

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 
Jose Sa
  • 31
  • 1
2

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.

Kenny Rasschaert
  • 9,045
  • 3
  • 42
  • 58
  • 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 02 '11 at 12:53
2

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
the-wabbit
  • 40,737
  • 13
  • 111
  • 174
2

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.
baitisj
  • 121
  • 2