The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-07-2004
yanhu yanhu is offline
Registered User
  
 

Join Date: Mar 2004
Location: New York
Posts: 8
Question ls- l and du

Hi everyone:
I was wondering if anyone can tell me why the size of a file listed using "du" and "ls -l" is different. Thanks in advance..
  #2 (permalink)  
Old 03-07-2004
kduffin's Avatar
kduffin kduffin is offline Forum Advisor  
UN1X
  
 

Join Date: Nov 2003
Location: Maryland
Posts: 449
An 'ls -l' will show the size of the file in bytes. 'du' shows the size of the file in 512-byte units, rounded up to the next 512-byte unit.

Cheers,

Keith
  #3 (permalink)  
Old 04-19-2007
tantric tantric is offline
Registered User
  
 

Join Date: Apr 2007
Posts: 23
Can anyone think of a scenario in which size reported by ls -l is less than the size reported by du.

The other way round is possible, if the file has holes, ls -l will report a size more than du (since du calculates the actual disk usage)

But I cannot understand why du reports a size more than ls -l.

For e.g:

% ls -l file12MB.pdf
-rw-r--r-- 1 vqwe 11650342 Apr 19 16:34 file12MB.pdf
% du -k file12MB.pdf
11404 file12MB.pdf

du shows file size is 11404 Kb.
ls shows file size is 11650342 bytes = 11377.2 Kb

How is this possible ?

Is it that du counts the space occupied by Indirect blocks as well ???

awaiting your response
  #4 (permalink)  
Old 04-19-2007
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,131
Quote:
Originally Posted by tantric
Can anyone think of a scenario in which size reported by ls -l is less than the size reported by du.

The other way round is possible, if the file has holes, ls -l will report a size more than du (since du calculates the actual disk usage)
That is pretty much the usual case. Take a McKusick style filesystem with 4k blocks and 1k fragments. I create a file with a single byte. ls says the length is 1 byte. du says the length is 2 blocks. Here is the definition of a stat structure. ls uses st_size while du uses st_blocks. (ls will report both numbers if you use "ls -ls".) The relationship between these two numbers depends on the filesystem. With most filesystems, yes, indirect blocks count. Some filesystems may have a way to preallocate data blocks to a file in advance of the need.

Btw, your second case is often called "sparse files".
  #5 (permalink)  
Old 01-07-2009
GKnight GKnight is offline
Registered User
  
 

Join Date: Nov 2005
Posts: 35
I have an even more interesting case of ls disagreeing with du:


Code:
[server1-/var/crash]# ls -lh 10.1.1.100-2009-01-05-14:01
total 1018M
-rw-------  1 netdump netdump 332K Jan  5 14:56 log
-rw-------  1 netdump netdump  16G Jan  5 14:52 vmcore

[server1-/var/crash]# du -h 10.1.1.100-2009-01-05-14:01/*
336K    10.1.1.100-2009-01-05-14:01/log
1017M   10.1.1.100-2009-01-05-14:01/vmcore

These files were created by netdump on linux. The filesystem type is ext3.

So it basically looks like I have a 16GB file that only occupied about 1GB of disk space.

Is vmcore a so-called "sparse file"?

I've only seen something similar once before, those were vmlog files created by vmware ESX host on an NFS mount (du would report 0 but ls would show a 1GB file).
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:02 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0