LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-06-2008, 01:13 PM   #1
felixrabe
LQ Newbie
 
Registered: Jan 2008
Posts: 2

Rep: Reputation: 0
How come file size differs between ls and df?


Hi there,

I have a large file (a VirtualBox hard disk) which shows a size of about 8 GB in ls and about 4 GB of total disk usage in df. How come?

Code:
$ ls -l disk.vdi 
-rw------- 1 fr fr 8589967872 2007-08-29 15:53 disk.vdi
$ ls -lh disk.vdi 
-rw------- 1 fr fr 8.1G 2007-08-29 15:53 disk.vdi
$ df -h .
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda7              19G  4.8G   13G  28% /media/bak7-fr-2
$ mount | grep sda7
/dev/sda7 on /media/bak7-fr-2 type ext3 (rw,nosuid,nodev,uhelper=hal)
 
Old 01-06-2008, 01:28 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Do you know that disk.vdi is on /dev/sda7?

What does "du disk.vdi" tell you?

since the file is for a VM, is it possible that the file size gets reported differently?
 
Old 01-06-2008, 01:47 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Maybe it is a "sparse file". You can find a definition on the Wikipedia. A common example on many systems is the file /var/log/lastlog
Code:
$ ls -l /var/log/lastlog
-rw-r--r-- 1 root tty 292584 2008-01-06 12:11 /var/log/lastlog

$ du /var/log/lastlog
28      /var/log/lastlog
You can also try the following test using the command find: cd to the directory where the file is stored, then do
Code:
# the following will print the allocated (total) space
find . -name disk.vdi -printf "%s\n"

# the following will print the actual size if sparse
find . -name disk.vdi -printf "%k\n"

Last edited by colucix; 01-06-2008 at 01:58 PM.
 
Old 01-06-2008, 02:53 PM   #4
felixrabe
LQ Newbie
 
Registered: Jan 2008
Posts: 2

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by colucix View Post
Maybe it is a "sparse file". You can find a definition on the Wikipedia. ...
That's totally it, thanks. I'm currently copying the file (over USB 1) around with 'cp -ax' and I'm glad that cp supports sparse files (saved quite some time ).

Code:
$ ls -lh disk.vdi 
-rw------- 1 fr fr 8.1G 2007-08-29 15:53 disk.vdi
$ du -h disk.vdi 
4.2G	disk.vdi
 
Old 01-06-2008, 05:20 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
How would you create a "sparse file"? (Other than the dd method in the Wikipedia article)
 
Old 01-06-2008, 05:57 PM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
I don't know any other *NIX utility to do that. On the other hand you can always use a programming language like C or Python. Here is a simple example in Python
Code:
#!/usr/bin/python
file = open("/tmp/sparsefile", 'w')
file.truncate(1024 * 1048000L)
file.close()
this will create a sparse file of 1G, whose actual size is 0 bytes (since nothing has been written into).
 
Old 01-06-2008, 06:26 PM   #7
xptools
Member
 
Registered: Sep 2005
Location: Canada
Distribution: Debian Testing
Posts: 40

Rep: Reputation: 15
FYI, there can be differences between df and du -s as well.

 
  


Reply

Tags
df, file, ls, size



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
The same font and it's size differs from Windows to Linux. How to solve it ? aboaventura Linux - General 1 12-20-2006 08:46 AM
File size differs procfs General 3 06-06-2006 09:14 AM
mk2efs ext3 block count differs on same partition size tkmbe Linux - General 1 06-29-2005 09:23 AM
File opening/reading speed differs first time and after that Not now, John! Linux - General 2 08-05-2004 09:37 AM
fdisk partition size differs from mounted partition size jimieee Linux - General 3 10-15-2003 03:10 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration