4/23 Is there some fast way to find the number of files in a
directory? I'm trying to do this in a dir with over
over 10,000 files and "ls -l | wc -l" is taking forever.
\_ ls | wc -l
\_ ls -1 | wc -l
\_ echo * | wc -w (assuming no spaces in filenames)
\_ This won't work if there's a lot of file names.
\_ obgetabettershell
\_ Ok, what's your religion? Some of us work in real places
and don't feel like installing tons of 'kewl' crap on
do this that work everywhere with the l8st kewl sh3ll.
every box. There's perfectly good standard unixy ways to
do this that work everywhere without the l8st kewl sh3ll.
\_ obPerl
\_ perl -we 'use strict; my $d = shift @ARGV; if (defined $d &&
opendir(D,$d)) { my $n=0; foreach (readdir(D)) {
$n++ if (-f "$d/$_"); } closedir(D) ;
print "$n\n"; }' <dir>
\_ Wouldn't it be nice to be able to access $. for
directory reads?
\_ yes
You can make is shorter if you leave out -w, use strict, my
and closedir.
\_ find . -ls | wc -l RTFM on your version of 'find' for details.
\_ find <dir> -type f -print | wc -l |