Counting files in the current directory

As the year comes to a close, I like to look back and see how many blog posts I
have written for the year. My blog is powered by Jekyll and I like to
keep my posts organized by year, in directories.

With all of my posts in one place, all I need to do is list ls out the files
in a given year’s directory, and then pipe | the results to the word count
command wc with the argument that tells it to count just the number of lines
-l.

The command in all of it’s glory looks like this:

ls | wc -l

With the result being just a plain ol’ integer.

This is great if you know that there are no directories in the directory you’re
trying to count files in. If there are directories in there, or if you’re just
not sure, you can swap out ls for the find command:

find . -type f | wc -l

Neither method will traverse into directories and simply counts the files in the
current directory. I’ll save those more advanced commands for another post!

Josh Sherman - The Man, The Myth, The Avatar

About Josh

Husband. Father. Pug dad. Musician. Founder of Holiday API, Head of Engineering and Emoji Specialist at Mailshake, and author of the best damn Lorem Ipsum Library for PHP.


If you found this article helpful, please consider buying me a coffee.