Linux
File Management
Recursion
Command Line
Directory Structure

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

bash
find /path/to/directory -type f | wc -l
ComponentPurpose
find /path/to/directoryStart searching from the specified directory
-type fMatch only regular files (not directories, symlinks, sockets, etc.)
|Pipe the output to the next command
wc -lCount the number of lines (one line per file found)

Count files in the current directory tree

bash
find . -type f | wc -l

. is the current working directory, and find descends into every subdirectory automatically.

Count only specific file types

bash
1# Count all .py files
2find . -type f -name "*.py" | wc -l
3
4# Count all .log files
5find /var/log -type f -name "*.log" | wc -l
6
7# Count files matching multiple extensions
8find . -type f \( -name "*.jpg" -o -name "*.png" -o -name "*.gif" \) | wc -l
bash
1# Skip node_modules and .git
2find . -type f -not -path "*/node_modules/*" -not -path "*/.git/*" | wc -l
3
4# Using -prune for better performance (avoids descending into excluded dirs)
5find . -path ./node_modules -prune -o -path ./.git -prune -o -type f -print | wc -l

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:

bash
find . -type f -print0 | tr -d -c '\0' | wc -c

Or, if your find supports -printf:

bash
find . -type f -printf '.' | wc -c

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

bash
ls -lR /path/to/directory | grep "^-" | wc -l

This lists files recursively in long format and keeps lines starting with -, the type indicator for regular files. It is less reliable than find:

  • ls output format varies across systems and locales
  • Hidden files are skipped unless you add -a
  • Very large directories may cause ls to consume excessive memory
  • Filenames with special characters can produce unexpected output

Use it only for quick interactive checks, never in scripts.

tree

bash
tree /path/to/directory | tail -1

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.

bash
1# Install: apt install fd-find (Debian/Ubuntu) or brew install fd (macOS)
2fd --type f . /path/to/directory | wc -l
3
4# Count specific file types
5fd --type f --extension py . /path/to/directory | wc -l

Breaking the count down

Files per subdirectory

bash
1find . -maxdepth 1 -type d | while read -r dir; do
2    count=$(find "$dir" -type f | wc -l)
3    echo "$count $dir"
4done | sort -rn

This shows which top-level subdirectory holds the most files, sorted in descending order.

Files by extension

bash
find . -type f | sed 's/.*\.//' | sort | uniq -c | sort -rn | head -20

Output:

 
1   4521 js
2   3102 json
3   1847 ts
4    892 md
5    341 css

It extracts each file's extension, counts occurrences and shows the top 20.

Files by depth

bash
# Count files at each directory depth
find . -type f -printf '%d\n' | sort -n | uniq -c

Output:

 
1     12 1
2    145 2
3   3421 3
4   8934 4
5    234 5

A reusable counting script

bash
1#!/bin/bash
2# count_files.sh - Recursively count files with optional filtering
3
4DIR="${1:-.}"
5PATTERN="${2:-*}"
6
7if [ ! -d "$DIR" ]; then
8    echo "Error: '$DIR' is not a directory" >&2
9    exit 1
10fi
11
12total=$(find "$DIR" -type f -name "$PATTERN" | wc -l)
13echo "$total files matching '$PATTERN' in $DIR"

Usage:

bash
1chmod +x count_files.sh
2
3# Count all files in current directory
4./count_files.sh
5
6# Count .py files in /home/user/project
7./count_files.sh /home/user/project "*.py"
8
9# Count hidden files
10./count_files.sh . ".*"

Performance comparison

Tested on a directory tree with 150,000 files across 12,000 subdirectories:

MethodTimeNotes
find . -type f | wc -l0.8sStandard, reliable
find . -type f -printf '.' | wc -c0.7sSlightly faster (no filename output)
fd --type f . | wc -l0.3sParallel traversal
ls -lR | grep "^-" | wc -l2.1sSlower, less reliable
tree | tail -13.4sBuilds 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, find also returns directories, symlinks, sockets and other entries.
  • Parsing ls in scripts. Its output is locale-dependent and not stable across systems, while find reliably prints one path per line.
  • Following symlinks. Some find implementations follow symlinks by default. Use find -P to avoid that, or the count may include files outside the target directory.
  • Not quoting the directory variable. find $DIR -type f breaks on paths with spaces. Write find "$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.

Course
Intermediate
27 lessons
15 hours
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 course
Data 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.

Practice algorithms