The wc (word count) command is a versatile Linux tool that counts lines, words, characters, and bytes in files. While it was originally designed for text analysis, you can combine it with commands such as find, ls, and xargs to count files in directories and handle other data-processing tasks.
This guide explains how to use wc with additional Linux utilities to count files in different contexts—such as recursively in subdirectories, filtered by file extension, size, or modification date. Examples cover everything from simple line-counting to safer, script-friendly file-counting methods suitable for practical scenarios.
wc Command Syntax
The wc command processes input and outputs the number of lines, words, characters, and more. It works directly with files or accepts piped input from other commands.
Syntax:
wc [OPTION]... [FILE]...- OPTION: Defines the type of count (lines, words, bytes, etc.).
- FILE: One or more files to process. If omitted, wc reads from standard input.
Matching infrastructure at centron
No hardware needed to follow along: ccloud³ VMs with full root access start at €3.12 per month, billed by the hour and ready in seconds. Rent a cloud server →
Common wc Options
| Option | Description |
|---|---|
| -l, --lines | Count lines |
| -w, --words | Count words |
| -c, --bytes | Count bytes |
| -m, --chars | Count characters |
| -L, --max-line-length | Show the length of the longest line |
When counting files via tools like ls or find, the -l option is most relevant, since each file or directory appears on its own line.
Count Files in a Directory Using wc
The wc command does not directly count files. Instead, it counts lines, words, and characters. By pairing it with ls or find, you can accurately count files and directories.
Count Files Using ls and wc
Use ls to list items and pipe them into wc -l to count lines. Each line corresponds to a file or directory.
Count all visible files and directories (excluding hidden ones)
$ ls | wc -lCount only regular files
$ ls -l | grep "^- " | wc -lCount only directories
$ ls -l | grep "^d" | wc -lCount files with a specific extension (.txt example)
$ ls *.txt | wc -lNote: ls-based approaches may fail if filenames contain spaces or unusual characters. For robust results, use find.
Count Files Using find and wc
The find command is more accurate and flexible, especially when working with recursive searches.
Count all files recursively
$ find . -type f | wc -lCount files only in the current directory (non-recursive)
$ find . -maxdepth 1 -type f | wc -lCount hidden files
$ find . -type f -name ".*" | wc -lCombine wc with Other Commands
You can merge wc with find to run advanced queries based on extension, size, modification time, and attributes—ideal for auditing and administration.
Count Files by Extension
$ find . -type f -name "*.jpg" | wc -lCount Files Modified Within a Timeframe
$ find . -type f -mtime -7 | wc -lCount Files by Size
$ find . -type f -size +1M | wc -lCount Files by Attributes
Count symbolic links
$ find . -type l | wc -lCount executable files
$ find . -type f -executable | wc -lCount files owned by the current user
$ find . -type f -user $(whoami) | wc -lCount Files Grouped by Extension
$ find . -type f | sed -n 's/.*\.//p' | sort | uniq -c | sort -nrCount Lines, Words, and Bytes Across Files
You can integrate find with xargs wc to count lines, words, and bytes in multiple files at once.
Count text file contents
$ find . -type f -name "*.txt" | xargs wcHandle special filenames safely
$ find . -type f -print0 | xargs -0 wcConclusion
This guide explained how to combine the wc command with utilities like find, ls, and xargs to count files in Linux directories by size, type, attributes, and modification date. These techniques provide accurate and adaptable file-counting suitable for administration and scripting.
Although wc cannot count files by itself, it becomes a powerful tool when paired with properly structured input from other commands. For more details, consult the manual with man wc or check the GNU documentation online.
Testen Sie Ihr Setup auf ccloud³
Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.