5
$ ls -lh file1.tar

-rw-r--r-- 1 wnice wnice 40K Aug  6 20:41 file1.tar

$ du -sh file1.tar

80K     file1.tar

Why is it showing different sizes of same file? Please help me understand

pLumo
  • 26,947

1 Answers1

7

The du command shows how much disk space is used for the given file.

In contrast, the ls command shows the size of the file. The space used can be larger than the file size, depending on the filesystem used.

For example, we can create a file with the size 1 byte like this (just one newline character in the file):

echo > tmp.txt

Then check its size using ls -l which shows the size one byte:

ls -l tmp.txt 
-rw-r--r-- 1 elias elias 1 aug  6 17:50 tmp.txt

Then check the used space using du:

du -h tmp.txt 
4,0K    tmp.txt

So the used disk space was in this example 4 kilobytes, although the filesize is only one byte.

Elias
  • 2,039
  • 2
    du is showing the block size and since the file size is smaller than the 4K block size it will only show you the 4K. If the file was 5K in size it would now show 8K being used. sudo blockdev --getbsz /dev/sda1 should show the block size of the drive being used. – Terrance Aug 06 '19 at 16:25
  • 1
    None of that explains why a file that is 40K in size would occupy 80K of disk space. – Tilman Jul 05 '22 at 12:11