Sum total bytes of files

files

If I have files a, b and c in a directory on a Linux machine. How can I get the total number of bytes of these 3 files in a way that does not depend on how e.g. ls shows the information? I mean I am interested in a way that is not error prone

Update
1) I am interested in binary files not ascii files
2) It would be ideal to be a portable solution e.g. GNU linux or Mac working

Best Answer

Use du with the -c (print total) and -b (bytes) options:

$ ls -l
total 12
-rw-r--r-- 1 terdon terdon  6 Sep 29 17:36 a.txt
-rw-r--r-- 1 terdon terdon 12 Sep 29 17:38 b.txt
-rw-r--r-- 1 terdon terdon 17 Sep 29 17:38 c.txt

Now, run du:

$ du -bc a.txt b.txt c.txt
6   a.txt
12  b.txt
17  c.txt
35  total

And if you just want the total size in a variable:

$ var=$( du -bc a.txt b.txt c.txt | tail -n1 | cut -f1)
$ echo $var
35
Related Question