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.
- A system running Linux.
- Access to the command line/terminal.
Linux wc Command Syntax
wc 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.
To view stats for multiple files, list the files in a single command:
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.
Use input redirect to stop
wc from printing the file name:
wc < [file/location]
Alternatively, use the cat command to list the contents of the file, then pipe the output to
cat [file/location] | wc
Linux wc Command Options
wc command takes the following options:
|Print the number of bytes.|
|Print the number of characters.|
|Print the number of lines.|
|Read the input from the files specified by NUL-terminated names in the file. If |
|Print the length of the longest line.|
|Print the number of words.|
|Show version information.|
Linux wc Examples
The examples below illustrate the use of the
Use wc with the find Command
Use the find command to provide output for
wc. The example below lists the number of characters for each file in the
/etc folder whose filename starts with
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
wc 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 to
ls | wc -l
-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
wc 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
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
-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.
The last row shows the character count of the longest line in all the files.
This tutorial presented the
wc 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.