Introduction
Thewc
command is a part of the coreutils Linux package containing the GNU core utilities. Use wc
to count the number of characters, words, lines, and bytes in a file or standard input.This tutorial will provide details about the wc
command and its options. The article also includes helpful examples to demonstrate how wc
works together with other commands.
Prerequisites
- A system running Linux.
- Access to the command line/terminal.
Linux wc Command Syntax
Thewc
command takes the following syntax:wc [options] [location/file]
By default, the output shows the number of new lines, words, and bytes in a file, followed by the file name.
wc [options] [location/file1] [location/file2] [location/file3]
The output shows the information for each file, followed by the total number of lines, words, and bytes.
wc
from printing the file name:wc < [file/location]

wc
:cat [file/location] | wc

Linux wc Command Options
Thewc
command takes the following options:Option | Description |
---|---|
-c, --bytes | Print the number of bytes. |
-m, --chars | Print the number of characters. |
-l, --lines | Print the number of lines. |
--files0-from=[file] | Read the input from the files specified by NUL-terminated names in the file. If - is provided instead of the file, the command reads from standard input. |
-L, --max-line-length | Print the length of the longest line. |
-w, --words | Print the number of words. |
--help | Show help. |
--version | Show version information. |
Linux wc Examples
The examples below illustrate the use of thewc
command.Use wc with the find Command
Use the find command to provide output forwc
. The example below lists the number of characters for each file in the /etc
folder whose filename starts with 30
:find /etc -name '30*' -print0 | wc -m --files0-from=-
The output of find
is piped to wc
, which then outputs the relevant stats.
Show Stats for a List of Files
Thewc
command can read from a file with file names to provide the stats for each file in the list. For wc
to be able to read the file correctly, the names in the file need to be NUL-terminated.Note: A NUL-terminated string is a string that ends with a null-char, the character whose all bits are zero.
find
to create a file containing a NUL-terminated list of files located in the current directory:find * -print0 > search.txt
The following command reads the file and provides the byte count for each of the files:wc -c --files0-from=search.txt

Use wc to Count Files and Directories
To find the number of files and directories in the current directory, pipe the ls command towc
:ls | wc -l
The -l
option counts the number of lines in the ls
output. This number corresponds to the total number of files and directories.
Perform wc Counts Across Multiple Files
Usewc
to count characters, words, lines, and bytes across multiple files. For example, to see the total word count of every TXT file in a directory, type:cat *.txt | wc -w
The cat
command pipes to wc
the contents of all the TXT files in the directory. wc -w
counts the total number of words.
Find the Longest Line in All the Files
The-L
option prints the length of the longest line for each file. If more than one file is specified, the total
row shows the longest line across all files.For example, to find the longest line in all the TXT files in a directory, type:wc -L *.txt
wc
processes the TXT files and, for each file, prints the number of characters in the longest line.
Conclusion
This tutorial presented thewc
command and its options. You also learned how wc
works in conjunction with other Linux commands.Refer to the Linux Commands Cheat Sheet article for more command examples.