0

use du -a in a given directory, but the disk usage of directory is larger than sum of all file's disk usage in that directory. why is that? From the man page, I assume that du command return the disk usage of given directory or file. I tried in sereval different directory, it's always larger. and there is no hidden files in all this directorys.
this is the result I got from running that command in a directory.

4       ./v
4       ./ls1.c
8       ./ls2.c
4       ./chown.c
24      .

The total is 24, but I expected 20 (the sum of the values above).

this is the result I got from running ls -al command in the same directory.

total 28
drwxr-xr-x 2 zxy zxy 4096 Oct  4 16:11 .
drwxr-xr-x 6 zxy zxy 4096 Oct  4 13:09 ..
-rw-r--r-- 1 zxy zxy  981 Oct  2 15:34 chown.c
-rw-r--r-- 1 zxy zxy  651 Oct  1 13:50 ls1.c
-rw-r--r-- 1 zxy zxy 4133 Oct  2 21:30 ls2.c
-rw-rw-r-- 1 zxy zxy    5 Oct  4 16:11 v

thanks.

6
  • It's unclear what numbers you are comparing (28 and 24, or each individual file?). Each file uses 4 Kb of actual disk space, because that's the smallest allocatable space on that filesystem. The ls2.c file is larger than 4 Kb, so there's two 4 Kb blocks allocated for it, hence 8 in the output from du. Since I'm not entirely certain what numbers you are asking about, I'm not turning this into an answer. – Kusalananda Oct 4 '20 at 8:53
  • Further, in du, . (the directory itself) includes itself in the count (also at least one block), but not .. (the parent directory). Everything is in 4's because du (by default) shows KB, not blocks (whose size can vary between different file system types). ls does appear to count precisely all the things it lists, including .., so ls -l, ls -Al and ls -al give three different results for total in the presence of hidden files: this might be considered a bug, or a feature. – Paul_Pedant Oct 4 '20 at 9:48
  • so the extra 4 the du command showed is from . itself right? and I have another problem, why the blocks in stat command is different from du command? – zenxy Oct 4 '20 at 11:10
  • That should be a separate question, but it is brief: in stat, block means sector. You'll need to look up what the format options mean, but try: stat -c '%b %B %o %n' *. – Paul_Pedant Oct 4 '20 at 19:33
  • Thank you sir. I'll look it up. – zenxy Oct 5 '20 at 13:04
0

In below example

4       ./v
4       ./ls1.c
8       ./ls2.c
4       ./chown.c4
24         .

24 . ===> its complete space used in the whole directory

Below are the spaces consumed by individual files

 4       ./v
    4       ./ls1.c
    8       ./ls2.c
    4       ./chown.c4
    24         .
1
  • 1
    Below is an exact copy of above, but with the formatting messed up. – ctrl-alt-delor Oct 25 '20 at 16:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.