df vs du for directory sizes


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users df vs du for directory sizes
# 1  
Old 07-12-2011
df vs du for directory sizes

Is it better to use df or du to calculate directory sizes? I tried both and got different numbers with both.

Code:
$ du -h /home
1.7G    /home/bob1
1.7G    /home

$ df -h /home
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VG-lv_home
                       25G  1.9G   22G   8% /home

# 2  
Old 07-12-2011
df reports file system disk space usage. Thus, given an argument, FILE, it will report the disk space usage of the filesystem containing FILE. Note that it is not reporting the disk space usage of the directory represented by FILE.

du on the other hand, given an argument FILE, reports the disk space usage of the directory represented by FILE. Presumably, it performs a recursive directory traversal of FILE adding the reported size of each file until the traversal is complete. In contrast with df, du may potentially report cumulative file sizes from files on multiple filesystems.

But, to answer your question more directly--it is best to use du. In many cases (depending on the number of files in the directory represented by FILE), du will take many times longer to execute than df due to the aforementioned directory traversal so expect to wait a little.
# 3  
Old 07-14-2011
du reports usage for the files in directories, while df reports on the filesystem itself. Filesystem overhead like a journal, allocation tables, etc. that take space on the filesystem will show as being used by df, but will not show with du, as they are not userspace accessible. Make a new, empty filesystem and du will should nothing but the cluster(s) for the directory(ies) in it, where df will show only the userspace accessible files on the filesystem.

example of a 2G drive ext3, so journal and lost+found directory:
Code:
$df -h /mnt
/dev/sdb1             1.9G   35M  1.8G   2% /mnt
$du -sh /mnt
20K    /mnt

the same with mkfs.vfat (no journal, small allocation table, smaller allocation of for the single directoy):
Code:
$df -h /mnt
/dev/sdb1             1.9G  4.0K  1.9G   1% /mnt
$du -sh /mnt
4.0K    /mnt

My guess is that you've got 200M worth of journal and filesystem allocation information, so you show 1.7G on df (1.7 GB of files) and 1.9G on du (1.7 + 200M filesystem stuff)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Partition Sizes

Good Morning, What's a good way to get partition/slice sizes down to the byte on Solaris 9? I've tried a few ways, but only see results like 8.21GB which rounds the number. Thanks! (5 Replies)
Discussion started by: Stellaman1977
5 Replies

2. Shell Programming and Scripting

How to list files names and sizes in a directory and output result to the file?

Hi , I'm trying to list the files and output is written to a file. But when I execute the command , the output file is being listed. How to exclude it ? /tmp file1.txt file2.txt ls -ltr |grep -v '-' | awk print {$9, $5} > output.txt cat output.txt file1.txt file2.txt output.txt (8 Replies)
Discussion started by: etldeveloper
8 Replies

3. UNIX for Dummies Questions & Answers

Compare two file sizes.

Hi everyone! I need to compare two file sizes. One of them (size) will be stored in a flat file and the other coming from a listed file. I can now get the first file size using: SIZE=`ls -l $DOCTYPE | awk '{print $5}'` 1. How can I store this value in a flat file? 2. How... (2 Replies)
Discussion started by: mrreds
2 Replies

4. Shell Programming and Scripting

Help with file sizes

I have 2 big files in the size of gb. They are same with respect to content, both are “,” delimited. Now both of them are created by two different processes but has the same logic. The problem is they are differing only in few bytes for e.g one file is 202195751 bytes other is 202195773. So... (2 Replies)
Discussion started by: dsravan
2 Replies

5. UNIX for Dummies Questions & Answers

Directories sizes

Hello everyone, can anybody help me in finding a way to obtain a list of all the directories and their sizes. I would like to be able to run this and obtain an output like a tree structure with each branch saying how much space it is taking up . Hope you can point me in the right direction.... (1 Reply)
Discussion started by: gio001
1 Replies

6. Shell Programming and Scripting

Directory sizes loop optimization

I have the following script: #!/usr/bin/ksh export MDIR=/datafiles NAME=$1 SERVER=$2 DIRECTORY=$3 DATABASE=$4 ID=$5 export dirlist=`/usr/bin/ssh -q $ID@$SERVER find $DIRECTORY -type d -print` for dir in $dirlist do SIZE=`</dev/null /usr/bin/ssh -q $ID@$SERVER du -ks $dir` echo... (6 Replies)
Discussion started by: la_womn
6 Replies

7. Shell Programming and Scripting

to compare total directory structure and get sizes of all f on two different servers

Hello every one, Iam newbie to this forum and shell programming &scripting. i needed to compare each and every folder of two separate servers. Actually I have copied some directory structure from one server to second server, to build on second server the files all should be copied... (3 Replies)
Discussion started by: mannam srinivas
3 Replies

8. Shell Programming and Scripting

Script for checking and reporting file sizes in a directory.

Hi, Need help for a Script for checking and reporting database file sizes in a directory. Request you to please give your valuable inputs. Thanks a lot in advance. Best Regards, Marconi (1 Reply)
Discussion started by: marconi
1 Replies

9. UNIX for Dummies Questions & Answers

Help on adding file sizes

Hi I need to take a list of files that are defined by an ls -ltr or grep for particular file names - and add up the byte size colum which is field 5 seperated by a space. I tried to do this but I think I am way off: for file in 'ls -ltr | grep 20070916 | nawk -F" " '{temp+=5} END {print... (1 Reply)
Discussion started by: llsmr777
1 Replies

10. UNIX for Dummies Questions & Answers

Directory sizes

Can someone tell me how to read these damn sizes. i mean, i prefer to see sizes in MB but that is not the case when you do an ls -l on directories. i have a had time converting these to MB just for verification purposes, what would a directory size like this = 3499990308 represent in MB or... (3 Replies)
Discussion started by: TRUEST
3 Replies
Login or Register to Ask a Question