File size [Working with Shell - II]

Hello,
Can you pls help investigate why am i getting different folder size outputs while trying alternative methods below for /opt?
I am using Ubuntu 20.04 Sandbox playground.

1.du -hs /opt
12K /opt

2.du -h /opt
4.0K /opt/.init
12K /opt

3.ls -lh /opt
total 0

  1. ls -lh

rwxr-xr-x 1 root root 4.0K Mar 1 2023 opt

Kind regards,
Aditya Garg

Hi @aditya.garg

The first command du -hs /opt shows the disk usage in a human-readable format (-h) like 2M, 2Kb, etc by summarizing (-s) the contents of the /opt dir.
The second command displays the size of each file/sub-dir in the /opt dir in human-readable (-h) format.

Every command in Linux uses flags/args which operates on the command, df in this case to print the info distinctly. You can refer to the man page for df command with: man df

There’s no df command in the question :slight_smile:
But ls -lh /opt would normally list contents of /opt in long listing format (-l), with human readable sizes (-h), but it’s apparently an empty directory.
Whereas ls -lh without any arguments displays the same - i.e. long listing format (-l), with human readable sizes (-h), but for the contents of your current working directory. Here, the 4.0K is the human-readable size, that a directory entry takes in filesystem structure.