Skip to content
Go back

Directory FileCount

Updated:
By SumGuy 5 min read
Directory FileCount

How many files are actually in here?

So you’ve got a directory with thousands of files and you need an exact count. You’re not gonna sit there and count them by hand — that’s what we have Linux for. The problem is: there are like six ways to do this, and they all give you slightly different answers depending on what you’re actually trying to count.

Let me walk you through the arsenal. Pick the right tool for your situation (spoiler: it’s usually find).

The simplest approach: ls | wc -l

Here’s the most naive way:

Terminal window
$ ls -1 images | wc -l
2847

The catch: This counts everything in the directory — files, subdirectories, symlinks, the lot. Also, if you have a hidden file (starts with .), it won’t show up unless you use ls -1a.

Why it’s useful: When you just need a ballpark number and you don’t care about the details.

The gotcha: If your directory is huge (like, tens of thousands of items), piping through wc -l can be slow. Also, one weird edge case — if your directory is empty, ls returns nothing and wc -l outputs 0, which happens to be correct, but only by accident.

The real solution: find with type filters

When you actually need reliability, use find. It’s slower but way smarter:

Terminal window
$ find images -maxdepth 1 -type f | wc -l
2041

This counts only files in the images directory, not subdirectories. The -maxdepth 1 keeps it from recursing into subdirs.

What if you want subdirectories too?

Terminal window
$ find images -maxdepth 1 -type d | wc -l
15

Or symlinks?

Terminal window
$ find images -maxdepth 1 -type l | wc -l
82

And you can combine types:

Terminal window
$ find images -maxdepth 1 \( -type f -o -type l \) | wc -l
2123

This counts files and symlinks, but skips directories.

Recursive counts: everything in the tree

Drop the -maxdepth 1 and you’re counting the whole directory tree:

Terminal window
$ find images -type f | wc -l
47291

That’s 47,291 files across all subdirectories. Useful for debugging inode exhaustion or figuring out why your S3 bucket has way more objects than you expected.

Per-subdirectory counts: which folder is the hog?

Want to find which subdirectory is eating all your inodes?

Terminal window
$ for dir in images/*/; do
count=$(find "$dir" -type f | wc -l)
echo "$count $dir"
done

Or sort by count to find the biggest offenders:

Terminal window
$ for dir in images/*/; do
count=$(find "$dir" -type f | wc -l)
echo "$count $dir"
done | sort -rn | head -10

This gives you the top 10 subdirectories by file count. Handy when you’re trying to figure out where all your build artifacts ended up.

Handling weird filenames: spaces, newlines, and chaos

Here’s the thing about ls and wc -l: they die if you have filenames with newlines in them. (Yes, that’s a thing. No, I don’t know why you’d do that, but it happens.)

The safe way is to use find with -print0 and xargs:

Terminal window
$ find images -maxdepth 1 -type f -print0 | xargs -0 wc -l | tail -1

The -print0 outputs NUL-terminated lines instead of newline-terminated ones, so even filenames with spaces, newlines, and weird characters get counted correctly. The tail -1 pulls out the total at the end.

The weird one: tree

If you’ve got tree installed (and honestly, who doesn’t?), there’s a sneaky one-liner:

Terminal window
$ tree -a --noreport images | tail -1
2041 directories, 47291 files

The --noreport skips the normal summary, then pipes to tail -1 to grab the last line, which is the count. The -a includes hidden files. Doesn’t work as well as find, but it’s cute.

Why you need this

Inode exhaustion: Your filesystem has a fixed number of inodes (file slots). You might run out of space to store new files even though your disk has terabytes free. Knowing your file count tells you if you’re getting close to the limit.

S3 bucket inventory: AWS charges you per object, not by storage size. If you’re dumping millions of small files into S3, a file count tells you what your month’s bill is gonna look like.

Debugging build artifacts: Ever wonder why your CI pipeline is taking forever? Often it’s because the artifact cache has ballooned to millions of tiny files. A quick count shows you if that’s the culprit.

Disk space mysteries: du tells you bytes, but sometimes you need to know if the problem is a few giant files or billions of tiny ones. A file count + disk usage gives you the average file size, which tells you everything.

The rule of thumb

Pick the one that matches your situation. And next time your 2 AM alert says “disk full”, you’ll know exactly whether you’re dealing with one 100 GB file or five million 1 KB files.


Share this post on:

Send a Webmention

Written about this post on your own site? Send a webmention and it'll show up above once verified.


Previous Post
Bash for loops sequential counting
Next Post
The Embedding Model Choice Nobody Explains

Discussion

Powered by Garrul . Sign in with GitHub or Google, or post anonymously.

Related Posts