A common question that comes up when you’re first getting comfortable with the command line: how do you count just the directories in your current folder?

The quick answer is:

ls -1 | wc -l

This pipes a one-per-line listing of everything in the current directory into wc -l, which counts lines. Simple enough.

But there’s a catch. This counts everything – files, symlinks, hidden items, the lot. If you only want directories, you need to be a bit more selective.

A better approach uses find:

find . -maxdepth 1 -type d | wc -l

Here’s what each part does:

  • find . searches from the current directory
  • -maxdepth 1 limits the search to just this folder (no recursion)
  • -type d filters for directories only
  • | wc -l counts the results

One thing to note: this will include the current directory itself (.) in the count, so you’ll be one over if you’re expecting a pure tally of subdirectories. Subtract one if that matters to you.

For a quick-and-dirty count where you don’t mind everything being included, the first command works fine. When you need precision, reach for find.