What is difference between ls -lh and du -sh?

5,273

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.

Share:
5,273

Related videos on Youtube

Nitin Kumar
Author by

Nitin Kumar

Updated on September 18, 2022

Comments

  • Nitin Kumar
    Nitin Kumar over 1 year
    $ 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

  • Terrance
    Terrance almost 5 years
    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.
  • Admin
    Admin almost 2 years
    None of that explains why a file that is 40K in size would occupy 80K of disk space.