wc Linux Command with Examples

April 5, 2022

Introduction

The wc 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.

wc Linux command with examples.

Prerequisites

  • A system running Linux.
  • Access to the command line/terminal.

Linux wc Command Syntax

The 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.

The default output of the wc command.

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.

Using the wc command with multiple files.

Use input redirect to stop wc from printing the file name:

wc < [file/location]
Using input redirect with the wc command.

Alternatively, use the cat command to list the contents of the file, then pipe the output to wc:

cat [file/location] | wc
Piping the output of the cat command to wc.

Linux wc Command Options

The wc command takes the following options:

OptionDescription
-c, --bytesPrint the number of bytes.
-m, --charsPrint the number of characters.
-l, --linesPrint 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-lengthPrint the length of the longest line.
-w, --wordsPrint the number of words.
--helpShow help.
--versionShow version information.

Linux wc Examples

The examples below illustrate the use of the wc command.

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

find /etc -name '30*' -print0 | wc -m --files0-from=-

The output of find is piped to wc, which then outputs the relevant stats.

Listing the number of characters for every file meeting the search criteria.

Show Stats for a List of Files

The 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.

Use 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
The wc command showing the byte count for files.

Use wc to Count Files and Directories

To find the number of files and directories in the current directory, pipe the ls command to wc:

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.

Finding the number of files and directories in the current directory.

Perform wc Counts Across Multiple Files

Use 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

The cat command pipes to wc the contents of all the TXT files in the directory. wc -w counts the total number of words.

Finding the total word count of multiple files.

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.

Using the -L option to find the longest line in each file.

The last row shows the character count of the longest line in all the files.

Conclusion

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.

Was this article helpful?
YesNo
Marko Aleksic
Marko Aleksić is a Technical Writer at phoenixNAP. His innate curiosity regarding all things IT, combined with over a decade long background in writing, teaching and working in IT-related fields, led him to technical writing, where he has an opportunity to employ his skills and make technology less daunting to everyone.
Next you should read
How to Use the Linux xargs Command
February 11, 2021

In this tutorial, you will learn how to use the Linux xargs command to manipulate the standard input and work with other...
Read more
How to Use the Linux tee Command
January 28, 2021

When the user executes a command in a Linux interactive shell, the output displays in the text terminal.
Read more
How to Grep for Multiple Strings, Patterns or Words
May 5, 2020

Grep is a powerful utility available by default on UNIX-based systems. The name stands for...
Read more
Linux Commands Cheat Sheet
February 21, 2020

Linux commands may seem intimidating at first glance if you are not used to using the terminal...
Read more