Count files in a Linux directory recursively
Codemia Knowledge Hub ·
To count all files in a Linux directory recursively, run find . -type f | wc -l. find lists every regular file (no directories, symlinks or other special entries) at any depth, and wc -l counts the lines. It is the most reliable one-liner for the job.
Counting with find and wc
| Component | Purpose |
find /path/to/directory | Start searching from the specified directory |
-type f | Match only regular files (not directories, symlinks, sockets, etc.) |
| | Pipe the output to the next command |
wc -l | Count the number of lines (one line per file found) |
Count files in the current directory tree
. is the current working directory, and find descends into every subdirectory automatically.
Count only specific file types
Exclude directories from the search
The -prune version is faster because find never enters the excluded directories. -not -path still descends into them and filters the results afterwards, which matters for folders like node_modules with thousands of nested files.
Handling filenames with newlines
wc -l counts newline characters, so a filename containing a literal newline (rare, but possible on Linux) makes the count wrong. Count null terminators instead:
Or, if your find supports -printf:
This prints one dot per file and counts the characters, so no filename content can affect the result. For most directories find . -type f | wc -l is enough, but use one of these in scripts that handle untrusted or generated filenames.
Other ways to count
ls -lR and grep
This lists files recursively in long format and keeps lines starting with -, the type indicator for regular files. It is less reliable than find:
lsoutput format varies across systems and locales- Hidden files are skipped unless you add
-a - Very large directories may cause
lsto consume excessive memory - Filenames with special characters can produce unexpected output
Use it only for quick interactive checks, never in scripts.
tree
tree ends with a summary line such as 42 directories, 198 files. It is handy for a visual check, but it has to be installed and parsing the summary line in scripts is fragile.
fd
fd is a faster, friendlier alternative to find that traverses directories in parallel with multiple threads, which makes it noticeably faster on large trees.
Breaking the count down
Files per subdirectory
This shows which top-level subdirectory holds the most files, sorted in descending order.
Files by extension
Output:
It extracts each file's extension, counts occurrences and shows the top 20.
Files by depth
Output:
A reusable counting script
Usage:
Performance comparison
Tested on a directory tree with 150,000 files across 12,000 subdirectories:
| Method | Time | Notes |
find . -type f | wc -l | 0.8s | Standard, reliable |
find . -type f -printf '.' | wc -c | 0.7s | Slightly faster (no filename output) |
fd --type f . | wc -l | 0.3s | Parallel traversal |
ls -lR | grep "^-" | wc -l | 2.1s | Slower, less reliable |
tree | tail -1 | 3.4s | Builds full tree representation |
For millions of files, find with -printf '.' or fd performs best.
Mistakes that skew the count
- Leaving out
-type f. Without it,findalso returns directories, symlinks, sockets and other entries. - Parsing
lsin scripts. Its output is locale-dependent and not stable across systems, whilefindreliably prints one path per line. - Following symlinks. Some
findimplementations follow symlinks by default. Usefind -Pto avoid that, or the count may include files outside the target directory. - Not quoting the directory variable.
find $DIR -type fbreaks on paths with spaces. Writefind "$DIR" -type f. - Running
find /without limiting scope. Searching from root walks network mounts and virtual filesystems (/proc,/sys) that can hang or return misleading counts.
Related reading
- Recursively iterate through all subdirectories using pathlib
- Recursively list files in Java
- Recursively print all permutations of a string Javascript
- Red-black tree over AVL tree
- Red-Black Trees
- Red eye reduction algorithm
- Reducing the time complexity of this algorithm
- Redundancy algorithm for reading noisy bitstream

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.