views:

368

answers:

6

When I use ls or du I get the amount of disk space each file is occupying. I need the sum total of all the data in files and sub directories I would get if I opened each file and counted the bytes. Bonus points If i can get this with out opening each file and counting.

A: 

Edit: use du -sb, as scrible suggests.

du -sb DIR
rob
Sorry, didn't catch that the first time I read your question. Fixed now.
rob
+3  A: 

stat's "%s" format gives you the actual number of bytes in a file.

 find . -type f |
 xargs stat --format=%s |
 awk '{s+=$1} END {print s}'

Feel free to substitute your favourite method for summing numbers.

Nelson
Preferably use "find . -type f -print0 | xargs -0 ..." to avoid problems with certain file names (containing spaces etc).
hlovdal
yeah, good point. if it wasn't in bsd 4.2 I don't remember to use it :-(
Nelson
+4  A: 

If you want the 'apparent size' (that is the number of bytes in each file), not size taken up by files on the disk, use the -b or --bytes option (if you got a Linux system with GNU coreutils):

% du -shb <directory>
scrible
works on my newer red hat boxes, unfortunately not on my embedded Dev box.
Arthur Ulfeldt
A: 

If you use busybox's "du" in emebedded system, you can not get a exact bytes with du, only Kbytes you can get.

BusyBox v1.4.1 (2007-11-30 20:37:49 EST) multi-call binary

Usage: du [-aHLdclsxhmk] [FILE]...

Summarize disk space used for each FILE and/or directory.
Disk space is printed in units of 1024 bytes.

Options:
        -a      Show sizes of files in addition to directories
        -H      Follow symbolic links that are FILE command line args
        -L      Follow all symbolic links encountered
        -d N    Limit output to directories (and files with -a) of depth < N
        -c      Output a grand total
        -l      Count sizes many times if hard linked
        -s      Display only a total for each argument
        -x      Skip directories on different filesystems
        -h      Print sizes in human readable format (e.g., 1K 243M 2G )
        -m      Print sizes in megabytes
        -k      Print sizes in kilobytes(default)
arsane
A: 

How about:

$ du -ckx | grep total | awk '{print $1}'

Where is the directory you want to inspect.

The '-c' gives you grand total data which is extracted using the 'grep total' portion of the command, and the count in Kbytes is extracted with the awk command.

The only caveat here is if you have a subdirectory containing the text "total" it will get spit out as well.

Rob Jones
A: 

Just an alternative:

$ ls -lR | grep -v '^d' | awk '{total += $5} END {print "Total:", total}'

grep -v '^d' will exclude the directories.

Barun