-1

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?

mpboden
  • 1,389

1 Answers1

7

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
Raffa
  • 32,237