Number of files in a directory?

Phil Howard KA9WGN phil at ux1.cso.uiuc.edu
Sun May 19 16:07:36 AEST 1991


mikeh at 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 at ux1.cso.uiuc.edu   |  Guns don't aim guns at  \
\ Lietuva laisva -- Brivu Latviju -- Eesti vabaks  |  people; CRIMINALS do!!  /
 \***************************************************************************/



More information about the Comp.unix.questions mailing list