Understanding 'du' command

Hi

I have a questions related 2 commands : 'du' and 'ls'.
Why is the difference between output of 'du' and 'ls' cmd's ?

Command 'du' :
------------------

jakubn@server1 /home/jakubn $ du -s *
4       engine.ksh
1331  scripts

'du -s *' ---> shows block count size on disk (512 Bytes per block on disk)

jakubn@server1 /home/jakubn $ du -sh *
2K       engine.ksh
665K    scripts

'du -sh *' ---> shows KB size on disk

Math for 'du' cmd:
---------------------
1331 * 512 Bytes = 681472 Bytes
681472 Bytes / 1024 Bytes = 665.5 KB

#################################
Command 'ls' :
-----------------

jakubn@server1 /home/jakubn $ ls -la
-rwxrwxrwx 1 jakubn jakubn 1251 Jul 17 16:00 engine.ksh
drwxr-xr-x 6 jakubn jakubn 6 Jul 17 16:10 scripts

QUESTIONS :
---------------
What is the value 1251 and 6 from ls cmd in fifth column ? (I guess Bytes)
If those values represent Bytes, why is the difference between 'ls -la' and 'du -sh *' outputs ?

Because they measure different things.

1251 is indeed bytes but 6 represent a number of entries (links) and not bytes as "scripts" is a directory.

"du" reports the size the file takes on the disk. This size depends on the file system used. "ls" reports the file size. They are somewhat related but different metrics. You certainly can have a file size larger that the space it takes on disk.

Agree with that but there is another 6 in fifth column not the second one.

I guess it means that :

  • engine.ksh file is fragmentet into 4 blocks (512 Bytes each)
  • engine.ksh occupies 4 blocks * 512 Bytes = 2 KB
    • engine.ksh actual file size is 1251 Bytes

So 'du' cmd show how many blocks a particular file or directory occupies on the disk geometry but the actual size of the object should be read from ls -la cmd ?

Correct.

Also if you have two identical size files according to "ls" you can get different sizes from "du" due to fragmentation.

It is inefficient to have very large numbers of small files, but no worse in unix than Windows. You will notice that a directory file never shrinks.

That's the one I'm referring to. That fifth column 6 means there are 6 entries inside that directory.

Here is an example of files using less disk space than their actual size:

 $ ls -Rg       
.:
total 6
-rw-r--r--   1 jlliagre    1252 Sep 15 23:58 engine.ksh
drwxr-xr-x   2 jlliagre       5 Sep 15 23:58 scripts

./scripts:
total 210
-rw-r--r--   1 jlliagre  102400 Sep 15 23:58 a1
-rw-r--r--   1 jlliagre  102400 Sep 15 23:58 a2
-rw-r--r--   1 jlliagre  102400 Sep 15 23:58 a3
$ du -sk * */* 
1    engine.ksh
106    scripts
0    scripts/a1
4    scripts/a2
100    scripts/a3

thx.

that's really great example.