finding a dir. size in two ways yields diff. results ?

finding a dir. size in two ways yields diff. results ?

Post by Atiqullah Hash » Fri, 04 Mar 1994 03:43:05



Hi,

I used two ways to get a dir. size(every thing under it recursively)
in bytes:

1. Since a dir. is also a file, so used stat() and passed it the path
   of a dir. to get the dir. size in bytes.
   Then looked at the st_size field of stat struct.

2. Used something like this:
   popen("ls -ltR path | awk 'sum=sum+$5; END {print sum}' | sed -n $,$p", "r");
        and read the pipe.

The two values are totally different, why ?.
What is the correct and proper way of reading dir. size.

Thanks.

Atiq.

 
 
 

finding a dir. size in two ways yields diff. results ?

Post by Steven Woodfo » Fri, 04 Mar 1994 09:10:51



>I used two ways to get a dir. size(every thing under it recursively)
>in bytes:
>1. Since a dir. is also a file, so used stat() and passed it the path
>   of a dir. to get the dir. size in bytes.
>   Then looked at the st_size field of stat struct.

>2. Used something like this:
>   popen("ls -ltR path | awk 'sum=sum+$5; END {print sum}' | sed -n $,$p", "r");
>    and read the pipe.
>The two values are totally different, why ?.
>What is the correct and proper way of reading dir. size.

1. When you examine the st_size field of a directory, you usually get the
   number of bytes needed to store the _names_ + _inode number_ of
   the files in that directory (plus anything else the OS needs in order
   to manage it). It has nothing to do with the actual
   sizes of the files or subdirectories within that directory.

2. Your second example is a rather inefficient way to get the right
   result in most cases, but will fail if the directory/subdirectories
   contain special files. Try typing 'ls -lt' in /dev .....

There is a command 'du -s [filename | directory] ...' which does
exactly what you want, only it returns the number of blocks (either 512
or 1024 bytes, depending on your system).

Alternatively, check your manual page for 'scandir()'. This allows you
to easily traverse a file tree in a reasonably portable manner.

Cheers, Steve

 
 
 

finding a dir. size in two ways yields diff. results ?

Post by Barry Margol » Sun, 06 Mar 1994 07:51:25



>I used two ways to get a dir. size(every thing under it recursively)
>in bytes:

>1. Since a dir. is also a file, so used stat() and passed it the path
>   of a dir. to get the dir. size in bytes.
>   Then looked at the st_size field of stat struct.

This is just the size of the directory file, and doesn't include everything
under it.  Here's an analogy:

Suppose you wanted to know how many pages there were in all the books in a
library.  Could you find this out by counting all the entries in the card
catalogue?  No.  But the above operation just tells you the size of the
catalogue, i.e. directory.  

Quote:>2. Used something like this:
>   popen("ls -ltR path | awk 'sum=sum+$5; END {print sum}' | sed -n $,$p", "r");
>    and read the pipe.

That will work.

Quote:>What is the correct and proper way of reading dir. size.

If you were interested in the number of disk blocks rather than bytes, you
could use popen("du -s path", "r").
--
Barry Margolin
System Manager, Thinking Machines Corp.


 
 
 

1. Having trouble understanding diff. I need to comapre two files, and get a clean result!

I am trying to understand what diff is telling me.

If i have two files.

a        a
b        b
c        c
d        f
e        h
f         i
g        k
h        l
i         m
j         n

Then shouldn't diff tell me - using the "<" symbol, that some of the letters
in the first file, aren't in the second file...

$ diff a b
4,5d3
< d
< e
7d4
< g
10d6
< j
$

And if there are the same number of elements in the files, and they are in
order, it appears to be ok.

But, if the files are not in any order, in either or both files,
like this
File A:
a
b
c
d
e
f
g
h
i
j
k
l
m
n

File B:
a
l
c
f
b
i
k
e
m
n
o
p
q
r
s
t
$

Then the result is that it shows b for example as < and > ????  So it says
it missing from the first file, but also missing from the second file???

$ diff a b
2c2
< b
---
4,5d3
< d
< e
7,8c5
< g
< h
---
10d6
< j
12c8
< l
---
15c11,16
<
---
$

So how do I compare file A with file B, and return ONLY a list of elements
from file A that are NOT in file B.?

Dashed if I can find any too that will do it.

Thanks a million if anyone has a reasonable solution please..

Riobert

2. oops with : loop-6 / patch-int-2.4.0.3 and 2.4.2

3. using lofiadm to mount Solaris8Companion CD results in 1 dir(lost+found)

4. LILO Boot loader...

5. Need Checksum utility for NTwhich yields same result as HP-UX "sum"

6. REX

7. Compile: can bzdisk and bzImage yield different results?

8. Colors in Terminal

9. strptime() yields wrong results

10. Script run via cron yields unexpected results.

11. find without the "find: cannot read dir /usr/lost+found: Permission denied"

12. Looking for "diff"-like code to find differences between two files

13. Two dirs with equal names in a dir???