How many files are in this directory? It sounds simple, but the answer depends on whether you count subdirectories, hidden files, or only certain types. These commands cover the common cases, and help when a directory with millions of files is slowing a server down, or eating inodes.
Count files in the current directory
ls -1 | wc -l # entries (files + directories), not hidden
ls -1A | wc -l # including hidden entries
find . -maxdepth 1 -type f | wc -l # only regular files, including hidden
find . -maxdepth 1 -type d | wc -l # directories (includes "." itself)
ls | wc -l miscounts names containing newlines (rare). find is more reliable.
Count recursively
find /var/www -type f | wc -l # all files in the tree
find /var/www -type d | wc -l # all directories
find /var/www -type f -printf '.' | wc -c # fast, safe with odd file names
Count by file type or extension
find /var/www -type f -name '*.php' | wc -l
find /var/www/uploads -type f -iname '*.jpg' | wc -l
# breakdown of all extensions
find /var/www -type f | sed -E 's/.*\.([^./]+)$/\1/' | sort | uniq -c | sort -rn | head -20
See Linux find command.
Files per directory (find the hotspots)
Which subdirectory has the most files?
for d in /var/www/*/; do echo "$(find "$d" -type f | wc -l) $d"; done | sort -rn | head
# or with du's inode mode (GNU coreutils 8.22+)
du --inodes -x /var | sort -n | tail -20
Typical culprits: PHP session directories, cache folders, mail queues, thumbnail caches, and log directories without rotation.
Inode usage
Each file uses an inode. A filesystem can run out of inodes while it still has free space, and then “No space left on device” appears even though df -h looks fine:
df -i
df -ih /
If IUse% is near 100%, find and clean up directories with huge numbers of small files (above). See disk full troubleshooting.
Very large directories
Listing a directory with millions of files is slow (ls sorts everything first). Faster options:
ls -f /path | wc -l # unsorted
find /path -maxdepth 1 -type f -printf '.' | wc -c
To delete huge numbers of files without “Argument list too long”:
find /var/lib/php/sessions -type f -mmin +1440 -delete
Count lines, words and sizes too
wc -l access.log # lines in a file
find . -name '*.py' -exec cat {} + | wc -l # lines of code (rough)
du -sh /var/www # total size
find /var/www -type f -size +100M # large files
See VPS storage planning.
Keep file counts under control
Directories that grow forever are a maintenance problem waiting to happen. Rotate and compress logs, expire cache and session files with a scheduled find … -delete, and store user uploads in dated subdirectories (for example uploads/2026/10/) rather than one flat folder. Monitoring the inode percentage alongside disk space catches problems early.
Quick reference
| Task | Command |
|---|---|
| Files in this directory | find . -maxdepth 1 -type f | wc -l |
| Recursive files | find DIR -type f | wc -l |
| By extension | find DIR -type f -name '*.ext' | wc -l |
| Per subdirectory | du --inodes -x DIR | sort -n |
| Inode usage | df -i |
| Huge dirs | ls -f DIR | wc -l |
Related: Linux directory structure and VPS storage planning.