Tutorials  /  Linux Basics

Count Files in Linux with wc Command

Ccentron Redaktion · September 2025 ·5 min read ·Linux Basics, Tutorial

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:

Code
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.
VM

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)

Console
$ ls | wc -l

Count only regular files

Console
$ ls -l | grep "^- " | wc -l

Count only directories

Console
$ ls -l | grep "^d" | wc -l

Count files with a specific extension (.txt example)

Console
$ ls *.txt | wc -l

Note: 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

Console
$ find . -type f | wc -l

Count files only in the current directory (non-recursive)

Console
$ find . -maxdepth 1 -type f | wc -l

Count hidden files

Console
$ find . -type f -name ".*" | wc -l

Combine 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

Console
$ find . -type f -name "*.jpg" | wc -l

Count Files Modified Within a Timeframe

Console
$ find . -type f -mtime -7 | wc -l

Count Files by Size

Console
$ find . -type f -size +1M | wc -l

Count Files by Attributes

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

Console
$ find . -type f | sed -n 's/.*\.//p' | sort | uniq -c | sort -nr

Count 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

Console
$ find . -type f -name "*.txt" | xargs wc

Handle special filenames safely

Console
$ find . -type f -print0 | xargs -0 wc

Conclusion

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.

Jetzt 200 € Guthaben sichern

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.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Linux Basics
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren