How to count the number of files/directories in Mac/Linux

Custom Search

Often you may want to know the number of files, directories, and specific file types. It is really easy to find that using simple linux/unix commands on a terminal in Mac and Linux. The three linux commands the comes handy in counting the files/dirs are

  • ls: The command “ls” lists the files
  • wc: The command “wc” counts the number of lines or words
  • grep: The command “grep” finds the pattern that we want to search

Combining these three commands with the optional arguments we can count on a terminal in Mac or Linux.
How to find the number of files in a directory?

ls -l | grep ^- | wc -l

How to find the number of sub-directories?

ls -l | grep  ^d | wc -l

How to find the number of pdf files?

ls -l | grep pdf* | wc -l

Similarly to count other file types, like jpg, txt, and png, in a directory, we simply replace “pdf” in the above command with the file type that we are interested in.


Related posts:

  1. Useful Mac OS X/Linux Tips – Part 1
  2. Google Voice and Video Chat Finally Comes to Linux
  3. Five Gmail Tips To Deal With Attachments
  4. Google Chrome Evolves to Become Faster and Stable in Mac and Linux
  5. Google Services Get Command Line Utility, GoogleCL
  6. How to Find MAC Address/Physical Address on Windows, Kindle, and iPad
  7. iPhone 4 Tips and FAQs : Folders and the Total Number of Apps in iPhone 4

Speak Your Mind

*