Another quick command line question: how do you count just the files in a directory, excluding subdirectories and other stuff?

The classic answer:

ls -l . | egrep -c '^-'

This lists everything in long format (ls -l), then counts lines that start with a hyphen – which is how ls marks regular files. Directories show up as d, symlinks as l, and so on, so the regex filters them out.

It works, but it’s a bit of a trick and relies on understanding ls output format. A more straightforward approach uses find:

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

This searches from the current directory, limits itself to one level deep (no recursion into subdirectories), and only matches regular files (-type f). The result gets piped to wc -l for a count.

Like the directory-counting trick, this includes . in the search but not in the results since it’s a directory, not a file. So the count should be accurate for files in just the current folder.

If you want to include subdirectories as well, drop the -maxdepth 1:

find . -type f | wc -l

That’ll give you a grand total of every file in the current tree. Handy for getting a sense of project size at a glance.