4

Possible Duplicate:
Directory size calculation difference

I don't understand how the du command under Linux works. I wanted to count the overall size of a few hundred, small files and du -sh returned something along the lines of 150MB. I didn't question it until I realized my file manager displayed only around 50MB for the same set of data and I got confused.

Here's what I then tried:

fkochem@WK-6:~/temp$ cat test 
a
fkochem@WK-6:~/temp$ du -sh test 
4.0K    test

So, now this must be a mistake? I looked at du -sh * of my original data and found the following:

4.0K    NCO-0.9.9.9.json
4.0K    NCO-0.99.9.json
4.0K    NCO-0.9.9.json
8.0K    NCO-0.99.json
8.0K    NCO-0.9.json

Why does du seem to count in 4KB steps? What am I missing here?

2
  • 4k is the size of a single disk block. Disk space is allocated in blocks, so even the smallest disk takes up one block (4k) on your disk. Mar 16, 2012 at 12:40
  • For completeness, to get the exact size of a small file I'd suggest wc -c. If you need the sum, you can always cat *.json | wc -c, or for a recursive version find . -name "*.json" -print0 | xargs -0 cat | wc -c
    – Davide
    Jul 22, 2015 at 10:02

1 Answer 1

4

du is disk usage, and it counts that amount of disk allocated to the file. The minimum "block size" (i.e. the minimum chunk allocated to a file) is usually 4k. So the file-system counts in 4k chunks, not du.

It would count in 1k chunks if you formatted a drive as follows:

mke2fs -b 1024 /dev/XXX

Not the answer you're looking for? Browse other questions tagged .