Score:-2

Why do `ls` and `du` commands show a different size for the same file?

cw flag

I am currently using Ubuntu 20.04.

When I check a file's size using the du command, I get the following output:

ubuntu@ip-172-31-49-39:/myxfs$ du -h newquota
4.0K    newquota

With the ls -la command, I get the following:

ubuntu@ip-172-31-49-39:/myxfs$ ls -la
total 8
drwxr-xr-x  2 ubuntu ubuntu   22 Apr 18 19:17 .
drwxr-xr-x 22 root   root   4096 Apr 17 12:05 ..
-rw-r--r--  1 ubuntu ubuntu 1024 Apr 18 19:17 newquota

Why is there a difference in size for the same file? And what is the actual size of the file?

Nmath avatar
ng flag
Does this answer your question? [Why do "df" and "du" commands show different disk usage?](https://askubuntu.com/questions/280342/why-do-df-and-du-commands-show-different-disk-usage) Because a file can contain 10 bytes of data, but if the file system block size is 4096 bytes, the file will take up 4096 bytes of space.
Alakananda S avatar
cw flag
these answers are not completely answering my question. Could someone please explain in detail?
Score:6
jp flag

In the case of a file, ls shows the actual size of the file ... While du shows the space on the disk reserved/occupied by the file ... Filesystems differ in minimum size allocation to a certain file ... Most filesystems will reserve a minimum of a block size which in your filesystem's case appears to be 4.0K.

Think of it as the standard size of a room in a hotel ... Hotels will accommodate a guest in the standard room size they have ... That same room might accommodate two or three guests as well ... But in all cases each guest/group of guests(whether one, two or three) occupy the same minimum allocatable room space ... Hotel rooms are filesystem blocks and guests are files contents (metaphorically).

So either:

  • an empty block(also empty files as well are merely metadata"e.g. names and inodes ... etc" that count towards the size of their parent directory but no actual size reserved for them on disk yet).

  • or an occupied block(i.e. a file with contents less or equal to the block size)

Please see the demonstration below:

# Show block size
$ stat -fc %s .
4096 # 4.0K
$ touch file
$ head -c 1k /dev/urandom > file
$ du -h file 
4.0K    file
$ ls -la file 
-rw-rw-r-- 1 ubuntu ubuntu 1024 Apr 19 00:21 file
I sit in a Tesla and translated this thread with Ai:

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.