you need to know the number of files (-f is true) in a the current directory, and you want to do it in one line--here's your chance!

it works by printing the number of entities processed - the number of entities that are that are not files (!-f)

Update: fixed thanks to jmcnamara

perl -e'opendir(D,shift)?print((map{$i+=!-f}readdir(D))-$i):die' your_ +dir_here
perl -le'opendir(D,".")?print((map{$i+=!-f}readdir(D))-$i):die'

Replies are listed 'Best First'.
Re: number of files in directory - one liner
by jmcnamara (Monsignor) on Mar 27, 2002 at 15:41 UTC

    That's nice.

    However, readdir() requires that you either chdir() or prepend the path. So you would have to do something like this: perl -le'$d=pop;opendir(D,$d)||die$!.$/;print~~grep{-f"$d/$_"}readdir D' dir

    Or for just the current dir: perl -le'opendir D,".";print~~grep{-f}readdir D'

    --
    John.

      you are correct. mine will work fine for the current directory, but give invalid results elsewhere. i'll update my code a bit.

      ~Particle ;̃

•Re: number of files in directory - one liner
by merlyn (Sage) on Mar 27, 2002 at 15:28 UTC
      nope. this lists directories and symlinks too. i'm trying to list just files. jmcnamara has an uglier solution, but it works ;)

      ~Particle ;̃