
The wc
(word count) command is a powerful utility in Linux used to count lines, words, characters, and bytes in files. Although primarily designed for analyzing text, you can combine it with commands like find
, ls
, and xargs
to count files in directories and perform other data-processing tasks.
This article explores how to use wc
with other Linux tools to count files in various contexts, such as recursively through subdirectories, by file extension, size, or modification date. The examples range from basic line-counting to safer and more script-friendly file-counting techniques suited for real-world usage.
wc
Command Syntax
The wc
command reads input and returns the number of lines, words, characters, and more. You can use it with files or pipe its input from other commands.
wc [OPTION]... [FILE]...
OPTION
: Specifies what type of count to perform (lines, words, bytes, etc.).FILE
: One or more files to analyze. If omitted,wc
reads from standard input.
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 in a directory using tools like ls
or find
, the -l
option is the most relevant, as each file or directory appears on its own line.
Count Files in a Directory Using wc
The wc
command doesn’t count files by itself. It counts lines, words, and characters. However, you can combine it with tools like ls
or find
to count files and directories effectively.
Count Files Using ls
and wc
Use ls
to list contents and pipe the output to wc -l
to count the lines. Each line representing a file or directory.
Count all visible files and directories (non-hidden).
console$ ls | wc -l
This counts all visible entries in the current directory. Each listed item becomes one line in the output, which
wc -l
counts.Count only regular files (excluding directories).
console$ ls -l | grep "^-" | wc -l
This filters for lines that begin with a dash (
-
), indicating regular files.Count only directories.
console$ ls -l | grep "^d" | wc -l
This filters lines that begin with
d
, representing directories.Count files with a specific extension (example,
.txt
)console$ ls *.txt | wc -l
This counts only files ending with
.txt
in the current directory. It doesn’t search subdirectories.
The ls-based methods may break or give inaccurate results if filenames contain special characters like newlines or spaces. Use find
for more robust results.
Count Files Using find
and wc
The find
command is more reliable and flexible when counting files, especially in nested directories.
Count all files recursively (including subdirectories):
console$ find . -type f | wc -l
This finds all regular files from the current directory downward and counts them.
Count files only in the current directory (non-recursive):
console$ find . -maxdepth 1 -type f | wc -l
This limits the search to one directory level.
Count hidden files (starting with a dot):
console$ find . -type f -name ".*" | wc -l
This returns the count of all hidden files, including those in subdirectories.
Combine wc
and Other Commands to Count Files
You can pair wc
with find
to create powerful queries that count files based on specific attributes like extension, modification time, or file size. These combinations are useful for auditing and system maintenance.
Count Files by Extension
To count all files with a specific extension (such as .jpg) in the current directory and its subdirectories:
$ find . -type f -name "*.jpg" | wc -l
This counts every .jpg
file, regardless of depth. Replace .jpg
with any other extension as needed.
Count Files Modified Within a Time Period
To count files modified in the last 7 days:
$ find . -type f -mtime -7 | wc -l
The -mtime -7
option filters files modified less than 7 days ago. You can adjust the number to match your desired timeframe.
Count Files by Size
To count files larger than 1 megabyte:
$ find . -type f -size +1M | wc -l
+1M
matches files greater than 1MB.- You can also use +1G, +500k, etc., for other sizes.
The find
-based methods are more accurate and flexible than ls
-based ones, especially for recursive searches and filtering by file metadata.
Count Files by Attributes
You can filter files based on their properties using find
, then count them using wc
.
Count symbolic links.
console$ find . -type l | wc -l
Count executable files.
console$ find . -type f -executable | wc -l
Count files owned by the current user.
console$ find . -type f -user $(whoami) | wc -l
Count Files Grouped by Extension
You can list how many files exist for each file extension using a combination of find
, sed
, and uniq
.
$ find . -type f | sed -n 's/.*\.//p' | sort | uniq -c | sort -nr
This command extracts extensions, counts their occurrences, and sorts the result by frequency.
Count Lines, Words, and Bytes Across Files
You can combine
find
withxargs wc
to count lines, words, and bytes across multiple files.console$ find . -type f -name "*.txt" | xargs wc
This returns a line-by-line breakdown and a total summary:
12 34 567 file1.txt 45 67 890 file2.txt 57 101 1457 total
To handle filenames with special characters or newlines, use null delimiters:
console$ find . -type f -print0 | xargs -0 wc
This safely handles unusual filenames.
Conclusion
In this article, you learned how to use the wc
command alongside tools like find
, ls
, and xargs
to count files in a Linux directory based on size, type, attributes, and modification time. These combinations allow for precise and flexible file counting tailored to various administrative and scripting needs.
Although wc
does not count files by itself, it becomes a powerful tool when used with well-structured input from other commands. For detailed usage of wc
, refer to its manual page by running man wc
, or visit the GNU wc documentation online.