Unix Commands

What is the command if any, to retrieve the number of files in the current directory?
timothy1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chris_calabreseCommented:
If you want to count all files/directories recursively from the current directory:
  find . -print | wc -l

If you want only stuff in the current directory, but not sub-directories:
  ls | wc -l

If you want only regular files in the current directory:
  ls -F | grep -v '/$' | wc -l

If you want only regular files, but recursively:
  find . -type f -print | wc -l
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ahoffmannCommented:
> 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

;-)
0
ahoffmannCommented:
oops, forgot to use -B option for all above ls commands (in case of people are using crazy filenames)
0
chris_calabreseCommented:
Oops, you're right about the -a.

Don't know if I'd consider that last one an improvement :-)
0
ahoffmannCommented:
> .. 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'
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Unix OS

From novice to tech pro — start learning today.