Link to home
Start Free TrialLog in
Avatar of timothy1
timothy1

asked on

Unix Commands

What is the command if any, to retrieve the number of files in the current directory?
ASKER CERTIFIED SOLUTION
Avatar of chris_calabrese
chris_calabrese

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
> If you want only stuff in the current directory, but not sub-directories:
  ls -a|wc -l

> If you want only regular files in the current directory:
  ls -Fa|egrep '[/|@%=]$'|wc -l

> If you want only regular files, but recursively:
   ls -FaR|egrep '[/|@%=]$'|wc -l

;-)
oops, forgot to use -B option for all above ls commands (in case of people are using crazy filenames)
Avatar of chris_calabrese
chris_calabrese

Oops, you're right about the -a.

Don't know if I'd consider that last one an improvement :-)
> .. an improvement
probaly not if you pipe it to wc
but you'll be supprised (just viewing), sometimes ;-)

oops, and just found an example where all the ls|wc suggestions fail (including mine):

      touch 'a\^Jb'; ls|wc
      # where ^J is ctrl-V followed by return

I think a sulution for this is find, or ls -i|awk 'sophisticated prog here'