Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Difference between du and adding up the bytes from ls -laR

Status
Not open for further replies.

wube

MIS
Sep 6, 2001
4
0
0
US
Can someone expalin the size difference between the two commands :

1. du -ks .
2. ls -laR | awk '{ sum += $5 } END { print sum/1024 }'
- $5 is the 5th column that displays the byte count

I thought that at least the two figures should be close since both are supposed to display the total space used.

Any help in enlightening my confused mind would be much appreciated.

TIA
wube
 
I think du is using blocks , in other words if a file is actually 3333 characters , it reserving a 4096 byte block.
 
How far apart are the numbers?

the problem with ls -alR is that it will follow links and count files with multiple links more than once.

 
It's also possible that a file has been removed, but is still held open by a process somewhere, in which case the space won't be freed up until that process ends. lsof is handy in those situations. Annihilannic.
 
mrregan is right: du counts blocks.
From man du:
The du command gives the number of blocks contained in all files
and directories recursively within each directory and file
specified by the list in the names argument. The block count
includes the indirect blocks of the file.
To have a closer result with ls, try this:
Code:
ls -laR | awk '{sum+=($5+1023)/1024}END{print sum}'
However this short awk script don't count the indirect blocks, ie the extra space required to store the block numbers occuppied by a big file.
Depending of the options accepted by ls, you can try this:
Code:
ls -asiR | awk 'NF>2{sum+=$2}END{print sum/2}'

Hope This Help
PH.
 
by default du displays blocks but wube specified the -k option.....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top