How to count files (get the number of files) under a directory in Linux?

by Yang Yang on November 18, 2008

in Linux Server Administration Tips

Share This Article:
Subscribe to Kavoir: blog feed

Just perform this command under the directory:

find . -type f | wc -l

and you will get the total number of files under it (recursively). Simply copy and paste that to do the trick.

Or if you just need to count the files directly under the current directory:

ls -f | wc -l

Which is a lot faster than the previous solution.

This should also work on all *nix OS.

Share This Article:
Subscribe to Kavoir: blog feed

You should also read:

{ 11 comments… read them below or add one }

Hans November 23, 2008 at 6:11 pm

I will try this command later when I switched to Linux. I am still in exploration stage in Ubuntu and I am having difficulty every now and then that is why I tend to go back to Windows when I am rushing up something.

Reply

Joe Nathan December 3, 2008 at 2:31 pm

Hi! Are you interested in getting a banner/ header/ logo/ template/ business cards/ mascot designs / directory or article submissions, for your site for no money, in return we require text links from your site http://www.kavoir.com. Approaching you through this form since I could not find out the email address. Please get back to me if you are interested.

Reply

Yang Yang December 8, 2008 at 8:09 pm

@Hans

While many agree that learning new things is exciting, refreshing and often bringing new perspectives, it’s in nature a cost.

Every new approach comes with a cost, to adopt it or not is the question of cost-effectiveness.

Reply

Jackson Matthews December 15, 2008 at 2:11 am

I am going to switch to linux in a few days, most likely sometime before Christmas! I will surely have to save this code somewhere. This will make life much easier. Thanks for the quick tip!

Thank You,

Reply

Mike December 15, 2008 at 11:27 pm

Nice tip. Could you use something like this:

ls | wc -l

as well?

Reply

Chris January 6, 2009 at 11:36 am

Unfortunately this wont work on directories with a lot of files in them; you’ll get the “Argument list too long” error…

Reply

Yang Yang January 6, 2009 at 1:27 pm

@Chris
I used it under a directory with around 18,000 gif images and it didn’t show up the error. What’s the rough number of files you have?

Reply

Ravindra May 27, 2009 at 5:48 pm

ls -l . | egrep -c ‘^-’

Reply

Alex Carter July 9, 2009 at 8:05 pm

Thank you for this tip. At some point I also needed to count files in all subdirectories as well, so I found this solution, may be it will be useful for somebody else:

ls -laR | wc -l

Reply

Yang Yang February 26, 2010 at 3:12 pm

Nice catch!

Reply

Craig Lotter June 29, 2011 at 5:30 pm

Perfect, just what I was looking for! :)

Reply

Leave a Comment

Previous post:

Next post: