Linux – Why does du -b show a different result than stat

bashdufilesystemslinux

I've recently ran into this problem:

find /tmp/tmp33hn25wv -type f -exec stat --format='%s' {} + | awk '{s+=$1} END {print s}'
10420224

du -bs /tmp/tmp33hn25wv
12198004    /tmp/tmp33hn25wv

Results are consistently different. All files are written in multiples of block-size bytes.

Where does du find these extra bytes? I understand that file-system may need more or less space to store the contents of the files, but I hoped that -b option to du means that it has to count the "apparent" size, not the size used by file system…

Best Answer

du includes the size of directories. If you add -type d to the find criteria you may get the result you want (I do on a directory tree containing only standard files):

find /tmp/tmp33hn25wv \( -type f -o -type d \) -exec stat --format='%s' {} + |\
    awk '{s+=$1} END {print s}'

However, there may be other file types that take up space, so try omitting the type check altogether:

find /tmp/tmp33hn25wv -exec stat --format='%s' {} + | awk '{s+=$1} END {print s}'
Related Question