Counting files in the current directory

Josh Sherman
1 min read
Command-line Interface

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!

Join the Conversation

Good stuff? Want more?

Weekly emails about technology, development, and sometimes sauerkraut.

100% Fresh, Grade A Content, Never Spam.

Related Articles