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.

1
  • Are you looking for actual space used on disk, or true file size? Mar 31, 2011 at 8:38

6 Answers 6

5

This should just work:

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

Just use du -b example:

# du -sh .
215G    .

# du -sbh .
344G    .
2
  • 1
    Twelve up-votes despite the fact there is no "-b" option with Solaris 10 du...
    – jlliagre
    Dec 14, 2018 at 15:11
  • On Solaris 11, it works via gdu -bsh foldername Apr 2, 2019 at 15:29
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 
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.

1
  • 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, 2011 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
3
  • These is no such option os Solaris 10 du. It is a non standard Gnu extension.
    – jlliagre
    Apr 2, 2011 at 10:07
  • Might be. Solaris Express 11 does have it, though.
    – the-wabbit
    Apr 8, 2011 at 20:15
  • 4
    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, 2011 at 20:41
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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