DU and LS discrepancies

Whether you are using Linux on the desktop or as a server, it's still good that you're using Linux. Linux related questions go here.

Moderator: General Moderators

Post Reply
ghstber
Forum Newbie
Posts: 6
Joined: Fri Aug 11, 2006 8:54 pm

DU and LS discrepancies

Post by ghstber »

I'm writing a script for my job to measure out how large IMAP mailboxes are, and I've run across what I think is a predicament:

The directory I'm measuring (as a test) is as follows:

Code: Select all

root@server [~/imapdtool/testdir]# ls -l
total 92
drwxr-xr-x    2 root     root         4096 Aug 11 15:04 ./
drwxr-xr-x    3 root     root         4096 Aug 10 21:22 ../
-rw-r--r--    1 root     root          121 Aug 11 15:04 error_log
-rw-r--r--    1 root     root        20176 Aug 10 21:22 file1
-rwxr-xr-x    1 root     root           91 Aug 10 21:22 file2
-rw-r--r--    1 root     root         6939 Aug 10 21:22 file3
-rw-r--r--    1 root     root         4824 Aug 10 21:22 file4
-rw-r--r--    1 root     root        10434 Aug 10 21:22 file5
-rw-r--r--    1 root     root         6742 Aug 10 21:22 file6
-rwxr-xr-x    1 root     root          471 Aug 10 21:22 file7*
-rwxr-xr-x    1 root     root           91 Aug 10 21:22 file8*
-rwxrwxrwx    1 root     root         9885 Aug 10 21:22 file9*
I've noticed that if I add up the above file sizes (either manually or through ls -l), the total comes to 67966. However, if I du -s the same directory, it comes up with 90112. What the hay?

So, I read up a bit more on du. Apparently, it measures in blocks, not actual file sizes. Is this correct? If so, I would assume (against my better judgement) that this discrepancy would increase leaps and bounds as the directories get larger. Would there be any way to get du to report the filesize instead?

Thanks in advance,

Jeremy
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Have you tried du -c or du -B 1?
ghstber
Forum Newbie
Posts: 6
Joined: Fri Aug 11, 2006 8:54 pm

Post by ghstber »

Here are the results of both of those commands:

Code: Select all

root@server [~/imapdtool/testdir]# du -c
88      .
88      total
root@server [~/imapdtool/testdir]# du -B 1
90112   .
No go, it seems.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

ls == Listing
du == Disk Usage

Disk usage and file size are not the same thing.

Now, why can't we just use the imap functions to do this job? :)

http://www.php.net/manual/en/function.i ... -quota.php
http://www.php.net/manual/en/function.i ... taroot.php
ghstber
Forum Newbie
Posts: 6
Joined: Fri Aug 11, 2006 8:54 pm

Post by ghstber »

Those functions seem very nice, however we also use cPanel, and I do not have the passwords for these IMAP mailboxes. Thus my current implementation. :(
ghstber
Forum Newbie
Posts: 6
Joined: Fri Aug 11, 2006 8:54 pm

Post by ghstber »

d11wtq wrote:ls == Listing
du == Disk Usage

Disk usage and file size are not the same thing.
Could you possible explain the difference to me? Sorry if this seems like an asinine question.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Hard disks are broken into blocks. Blocks vary in byte size from partioning format to partioning format. Files cannot occupy the same block therefore files will use as many blocks as necessary to completely enclose them.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Blocks vary in size from filesystem to filesystem but can sometimes be specified by the user when you format the drive/partition.

E.g. reiserfs uses block sizes of 4Kb by default. That means, if you have 4 1Kb files, you'll use 16Kb of disk space. If you had 4 3Kb files, you still use 16Kb of disk space.
ghstber
Forum Newbie
Posts: 6
Joined: Fri Aug 11, 2006 8:54 pm

Post by ghstber »

d11wtq, feyd, thanks for clarifying that. I figured it was something like that, but I didn't know multiple files couldn't exist in the same block.
Post Reply