0

I have done these commands to concatenate the files into one file:

$ ls -1 | wc -l
16916
$ ls -1 *.txt | wc -l
16916
$ ls -lh | head -1
total 93M
$ cat *.txt > ../nectar_3.txt
$ ls -lh ../nectar_3.txt 
-rw-r--r-- 1 llopis llopis 52M May 25 16:03 ../nectar_3.txt

Why is the resulting file size half of the sum of the size of all files? The only explanation I can found is about rounding in the ls -lh command, but I couldn't find anything (using ls -lk outputs almost the same 92.76953125M)

llrs
  • 3,308
  • 35
  • 68
  • You've go almost 17000 files, and the size total at the top is rounded. 16,000+ rounding errors can add to a very huge difference. – Marc B May 25 '15 at 14:27
  • So there is rounding in ls -h or -k and the total amount is summed from the individual rounded size of each file? – llrs May 25 '15 at 14:33
  • 1
    try it out. stuff a few smallish files into a dir, then just do `ls -lk` and manually sum up the sizes. on my test dir, the total header shows 137k, while a manual addition comes to 101k – Marc B May 25 '15 at 14:36
  • This should go to an answer then, thanks for clarifying it. – llrs May 25 '15 at 14:37

1 Answers1

1

The total is rounded, and is not guaranteed to be accurate:

Simple example:

marc@panic$ ls -lk
total 24
-rw-r--r-- 1 marc marc 6000 May 25 08:39 test1.txt
-rw-r--r-- 1 marc marc 7000 May 25 08:39 test2.txt
-rw-r--r-- 1 marc marc 8000 May 25 08:39 test3.txt

Three simple files, total size = 21,000 bytes, yet the total shows 24.

Marc B
  • 356,200
  • 43
  • 426
  • 500