Score:1

why are du -b and du -k different so much

kr flag
gyd

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?

Score:1
cl flag
A.B

/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
kr flag
gyd
Thanks for your answer, helps a lot.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.