Berkeley CSUA MOTD:Entry 24538
Berkeley CSUA MOTD
 
WIKI | FAQ | Tech FAQ
http://csua.com/feed/
2025/07/08 [General] UID:1000 Activity:popular
7/8     

2002/4/23 [Computer/SW/Languages/Perl] UID:24538 Activity:high
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