[comp.unix.questions] Number of files in a directory?

mikeh@fsd.cpsc.ucalgary.ca (Michael Hoffos) (05/19/91)

Dumb question time.  I have been using UNIX for many years, but I have
never figured out how to do something: how can you get the number of
files that are in a directory?  This would be really handy for figuring
out how efficient a backup was (you need the number of files backed-up in
order to figure in the header info tar adds for each file).

Any suggestions would be helpful.

Mike Hoffos
--
I am working for the summer at the University of Calgary doing contour
tracing and thinning algorithm research.  My thoughts, deeds, and opinions
do not reflect the U of C's, nor do I speak for it.

phil@ux1.cso.uiuc.edu (Phil Howard KA9WGN) (05/19/91)

mikeh@fsd.cpsc.ucalgary.ca (Michael Hoffos) writes:

>Dumb question time.  I have been using UNIX for many years, but I have
>never figured out how to do something: how can you get the number of
>files that are in a directory?  This would be really handy for figuring
>out how efficient a backup was (you need the number of files backed-up in
>order to figure in the header info tar adds for each file).

Since you mention tar, I figure you also want to include all the files
in all the subdirectories, the subdirectories themselves, and symlinks.

find . -print | wc -l

However, this might still be misleading in the case of making an estimate
for tar when you have hard links.  The tar program detects the hard link
and writes out which file it is linked to, not the whole file.  I am not
sure how much space that will take.

Of course the ULTIMATE estimate is:

    tar (appropriate options) | wc -c

but that gets to be time consuming and wasteful.  In cases where I really
needed this I found it faster to write the file to disk instead (if there
is space) and check the length of the file.  It seems "wc" is slow.
-- 
 /***************************************************************************\
/ Phil Howard -- KA9WGN -- phil@ux1.cso.uiuc.edu   |  Guns don't aim guns at  \
\ Lietuva laisva -- Brivu Latviju -- Eesti vabaks  |  people; CRIMINALS do!!  /
 \***************************************************************************/

cpcahil@virtech.uucp (Conor P. Cahill) (05/19/91)

mikeh@fsd.cpsc.ucalgary.ca (Michael Hoffos) writes:


>Dumb question time.  I have been using UNIX for many years, but I have
>never figured out how to do something: how can you get the number of
>files that are in a directory?  This would be really handy for figuring

The following will always work:

	for a single directory:		ls -a | wc -l
	for a directory hierarchy:	find [dirname] -print | wc -l

If on a v7 style filesystem you can get the size of the directory and
divide it by 16, but that only works on a single directory,  is non-portable
and definately doesn't work for BSD style file systems.

>out how efficient a backup was (you need the number of files backed-up in
>order to figure in the header info tar adds for each file).

A better way to do this is to run a tar -tv of the tape and use the
number of lines to figure the number of entries, use the size of 
each item to calculate the minimum amount of tape space that would
be required.  Then all you need is the size of the tar header and
the blocking factor (also provided by the tar  -tv) and you should be
able to determine the efficiency of the backup with respect to 
tape file size.

-- 
Conor P. Cahill            (703)430-9247        Virtual Technologies, Inc.
uunet!virtech!cpcahil                           46030 Manekin Plaza, Suite 160
                                                Sterling, VA 22170 

ires@kaspar.UUCP (Bruce R Larson) (05/20/91)

>mikeh@fsd.cpsc.ucalgary.ca (Michael Hoffos) writes:
>>... how can you get the number of files that are in a directory?

In article < ... > cpcahil@virtech.uucp (Conor P. Cahill) writes:
>The following will always work:
>
>	for a single directory:		ls -a | wc -l

Because so many of us alias `ls' to things like `ls -xF' it's a good
idea to issue a full path name to `ls' in the command above.

	/bin/ls -a | wc -l

Don't forget that `.' and `..' are being counted.

Bruce
--
Bruce R. Larson
Integral Resources, Milton MA
Internet:  blarson@ires.com
Uucp:  ..!{world|uunet}!ires.com!blarson
-- 
Bruce R. Larson
Integral Resources, Milton MA
Internet:  blarson@ires.com
Uucp:  ..!{world|uunet}!ires.com!blarson

spotter@eve.wright.edu (Master Vampire) (05/21/91)

In article <116@kaspar.UUCP> ires@kaspar.UUCP (Bruce R Larson) writes:
>>mikeh@fsd.cpsc.ucalgary.ca (Michael Hoffos) writes:
>>>... how can you get the number of files that are in a directory?
>In article < ... > cpcahil@virtech.uucp (Conor P. Cahill) writes:
>>The following will always work:
>>	for a single directory:		ls -a | wc -l
>Because so many of us alias `ls' to things like `ls -xF' it's a good
>idea to issue a full path name to `ls' in the command above.
>	/bin/ls -a | wc -l
>Don't forget that `.' and `..' are being counted.

Well, to correct for '.' and '..', you can use 

	/bin/ls -A | wc -l 

Or, at least, I assume '-A' will work on most system.  It is supposed to list
every file, except '.' and '..'  

Steve

--
anagram@desire.wright.edu | "Role becomes the actor, she's addicted to applause
spotter@eve.wright.edu    |  The stage a world because she never leaves it."
			  |  -Rush
<Std.Dsc> WSU doesn't approve of anything I do, but that hasn't stopped me yet.

martin@mwtech.UUCP (Martin Weitzel) (05/24/91)

In article <1991May19.111727.18637@virtech.uucp> cpcahil@virtech.uucp (Conor P. Cahill) writes:
:mikeh@fsd.cpsc.ucalgary.ca (Michael Hoffos) writes:
:>[...]
:>how can you get the number of
:>files that are in a directory? 
[...]
:If on a v7 style filesystem you can get the size of the directory and
:divide it by 16, but that only works on a single directory,  is non-portable
:and definately doesn't work for BSD style file systems.

And it will not work if you have deleted some files from the directory,
as directories don't shrink once they have been big (except you have a
special utility to compress them).
-- 
Martin Weitzel, email: martin@mwtech.UUCP, voice: 49-(0)6151-6 56 83

shane@inferno.peri.com (Shane Bouslough) (05/30/91)

From article <1146@mwtech.UUCP>, by martin@mwtech.UUCP (Martin Weitzel):
> In article <1991May19.111727.18637@virtech.uucp> cpcahil@virtech.uucp (Conor P. Cahill) writes:
> :mikeh@fsd.cpsc.ucalgary.ca (Michael Hoffos) writes:
> :>[...]
> :>how can you get the number of
> :>files that are in a directory? 
> [...]
> :If on a v7 style filesystem you can get the size of the directory and
> :divide it by 16, but that only works on a single directory,  is non-portable
> :and definately doesn't work for BSD style file systems.
> 
> And it will not work if you have deleted some files from the directory,
> as directories don't shrink once they have been big (except you have a
> special utility to compress them).

However, if you're going to get the size of a directory, you'll probably
open it anyway, so why not just read each entry and count those not deleted?

> -- 
> Martin Weitzel, email: martin@mwtech.UUCP, voice: 49-(0)6151-6 56 83


-- 
Shane Bouslough    |  ...!rutgers!mcdhup!inferno!shane           516-467-0500
Periphonics Corp.  |                                             Ride Bike!
4000 Veterans Hwy. |  "We're talking Mega-Ecstasy-Bliss!!!"
Bohemia, NY 11716  |                              -David Lister, Red Dwarf