1

I am using Ubuntu 16.04.5 LTS, and the version of du is 8.25. Now I have a question:

$ du -b /var/log/lastlog
69788251412 /var/log/lastlog

the size matches command ls -al. While I use du -k, it has another result:

$ du -k /var/log/lastlog
80 /var/log/lastlog

The two result doesn't match, why?

gyd
  • 11
  • 1

1 Answers1

1

/var/log/lastlog is a binary file used in "random access" mode rather than a log appended to, and is thus naturally a sparse file, as noted in this (GNU/Linux) manual:

NOTE

The lastlog file is a database which contains info on the last login of each user. You should not rotate it. It is a sparse file, so its size on the disk is usually much smaller than the one shown by "ls -l" (which can indicate a really big file if you have in passwd users with a high UID). You can display its real size with "ls -s".

Sparse means its actual disk usage is less than its apparent size.

On GNU/Linux, the -b option for du is described as:

-b, --bytes

equivalent to --apparent-size --block-size=1

That is, -b explicitly disables the sparse file detection and handling by including --apparent-size.

So to have size in bytes match -k (on-disk actual usage) use instead:

du --block-size=1 /var/log/lastlog

or to have size in KiB match -b (with apparent size) use instead:

du --apparent-size -k /var/log/lastlog
A.B
  • 11,090
  • 2
  • 24
  • 45