The “wc” Command in Linux [15 Practical Examples]

The wc command in Linux shows the number of lines, words, characters and byte count of one or multiple files. Moreover, we can combine the wc command with other commands, which makes the wc command versatile. In this article, I will try to show you all sorts of usage of the wc command in Linux with examples.

A. Description

The “wc” of wc command comes from “Word Count”. It is a common command in Unix and Unix-like operating systems. The basic usage of the command is to show the word count, characters count and the number of lines of file/files provided to it as an argument. Moreover, it has some useful options and can take standard input generated by other commands.

B. Syntax

The syntax of the wc command in Linux is very simple. The syntax is wc, followed by one or multiple options and one or multiple file names.

wc [OPTION]... [FILE]...

Note: In the above syntax OPTION and File are enclosed by a square bracket and followed by 3 dots representing that multiple options and file names can be provided at the same time.

C. Options

The wc command in Linux comes with a bunch of options, and different option serves different purpose. You can check those from the manual page by yourself. Type the command below:

man wc

Useful Options

  • -w, –words (displays the word counts)
  • -l, –lines (shows the line counts)
  • -m, –chars (displays the character counts)
  • -c, –bytes (shows the byte counts)
  • -L, –max-line-length (prints the length of the longest line)
  • –files0-from (takes input from the files specified by Nul-terminated names in a file)
Note: The options in Linux CLI(Command Line Interface) are all case-sensitive. Be careful in using them.

Practical Examples of the “wc” Command in Linux

Now I am going to show you some practical examples of the wc command in Linux. Throughout these examples, I will use two files named city and country. You can download them from below and check your results with me.

Example 1: Display Word, Line and Character Count for a Single File

It is the most basic use of the wc command. You need to provide a file name as an argument. In my case, it is city. Just follow the steps below to do the same:

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

wc city

➌ Now, press the ENTER button.

Output >

You will see four columns in the output like below. The first column denotes the number of lines in the file city, whereas the last three columns represent the number of words, characters, and the name of the file respectively.Showing line, word and character count for a single file using the wc command in linux


Similar Readings


Example 2: Display Word, Line, and Character Count for Multiple Files

You can provide multiple files to the wc command in Linux. I will use both city and country files this time. Follow the steps below:

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

wc country city

➌ Now, press the ENTER button.

Output >

Here you can see, in the output different file is listed separately and there is an extra line at the end showing the grand total.Showing line, word and character count for multiple files

Example 3: Multiple Flies with a Common Naming Pattern

Let’s say you have a lot of files. It is really tedious to type all the names one by one. But if the files follow a particular pattern, you can do the following. Now you can see, the file city and country both have the letter “c*” at the beginning. I will use this as a pattern. You can do that by yourself by following the steps below:

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

wc c*

➌ Now press the ENTER button.

Output >

Now the files will be listed out like the followingUsing pattern to show multiple files

Example 4: Display Word Count Using the “wc” Command in Linux

To see only the word count, follow the steps below:

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

wc -w city

➌ Now, press the ENTER button.

Output >

Now there are only two columns instead of four columns. The first one denotes word counts and the second one denotes file name.Printing word count using the wc command in linux

Example 5: Display the Number of Lines Using the “wc” Command in Linux

If you only want to print lines number, follow the steps below:

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

wc -l city

➌ Now, press the ENTER button.

Output >

Now you can see the number of lines in the file city.Printing number of lines using the wc command in Linux

Example 6: Display Number of Characters Using the “wc” Command in Linux

To see only the number of characters follow the steps below:

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

wc -m city

➌ Now, press the ENTER button.

Output >

As you can see, the number of characters in the output.Printing number of characters using the wc command in Linux

Example 7: Display the Number of Bytes Using the “wc” Command in Linux

To see only the number of bytes follow the steps below:

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

wc -c city

➌ Now, press the ENTER button.

Output >

Don’t be surprised if your bytes number and characters number are similar!Showing number of bytes

Note: ASCII encoding uses one byte per character. That is why we see count and byte number are the same. However, for other encodings, we might see a different result.

Example 8: Show Only First Column Using the “wc” and “cut” Command in Linux

If you don’t want to see the file name, you can do the following. We will use the cut command here.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

wc -w city | cut -c 1-2

➌ Now, press the ENTER button.

Output >

In the output, there is only the number of words. No file name is shown this time.Displaying only the first column

Note: The cut command is showing the first two characters. You can change it according to your needs. For example, if you want to see the first three characters, change “1-2” to “1-3”.

Similar Readings


Example 9: Length of the Longest Line in a Text

The df command in Linux can print the longest line in a text. Follow the steps below to do the same:

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

wc -L city

➌ Now, press the ENTER button.

Output >

The longest line of our city file has 8 characters.Showing the length of the longest line in a text using the wc command in Linux

Example 10: Number of Items in a Directory

You can print the number of items in a directory by piping the output of the ls command into the wc command. For this example, I will use /usr/bin directory. You can do it by yourself by doing the following:

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

ls -l /usr/bin | wc -l

➌ Now, press the ENTER button.

Output >

You will see the output as the following image. The number might vary from system to system.Printing number of directories

Example 11: Combining “find” Command with “wc” Command

There is a directory named walid on my machine. It contains two files with the extension “.txt” at the end. Let’s say I don’t know how many files ended with the extension “.txt” and want to know it. I can do it by following the steps below:

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

find . -name “*.txt” | wc -l

➌ Now, press the ENTER button.

Output >

Now see! It is showing 2 in the output.Piping the output of the find command into the wc command

Example 12: Combining the “grep” Command with “wc” Command

In the /var/log directory in Linux, there are a lot of log files. Now, I want to know the number of log files having the word “error” in them. I can do it by following the steps below:

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

sudo grep -c "error" *.log | wc -l

Note: To access some log files, you need root privileges. That’s why I put sudo at the beginning.

➌ Now, press the ENTER button

➍ Tye Password if necessary.

➎ Press ENTER again.

Output >

In my log files, there are 26 files with the word “error” inside. You might get a different result. Don’t worry.Piping the output of the grep command into the wc command

Example 13: Combining “cat” Command with “wc” Command

You can pipe the output of the cat command into the wc command in Linux as well. Follow the steps below to do the same:

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

cat city | wc -w

➌ Now, press the ENTER button.

Output >

You will see the number of words in the city file. It is another way to get rid of the file name in the output.Using the cat command and wc command together


Similar Readings


Example 14: Count the Number of Processes Using the “wc” Command in Linux

The ps command with the option “-e” lists the currently running processes in a machine. You can get the total number of processes by piping the output into the wc command in Linux. Now follow the steps below:

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

ps -e | wc -l

➌ Now, press the ENTER button.

Output >

They are 292 processes currently running on my machine. You might get a different result.Displaying number of processes using the wc command in Linux

Example 15: Read Input from Files Specified by NUL-terminated Names in a File

The wc command in Linux allows you to put multiple null-terminated filenames in a file and the wc command will take input from that file. Firstly, I used the find command to create a file named input containing null-terminated names. Then I used the “–files0-from” option to read input from the input file. Follow the steps below:

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

find * -print0 > input
wc --files0-from=input

➌ Now, press the ENTER button.

Output >

My directory walid has two files, hello.txt and hi.txt. The find command created an input file containing all the filenames of walid directory. Then the wc command took input from the input file and showed us the number of lines, word count, character count etc.The wc command in linux is taking input from files specified by NUL-terminated names in a File

Conclusion

The wc command in Linux is quite a handy tool for all sorts of users. You have realized it already, right? Hopefully, you have practiced all the examples above. Eventually, you will be very efficient in using the wc command.


Similar Readings

Rate this post
Walid Al Asad

Hello Everyone! I am Walid Al Asad. Currently, I am working at a tech company named Softeko as a Linux Content Developer Executive. I live in Dhaka, Bangladesh. I have completed my BSc. in Mechanical Engineering from Bangladesh University of Engineering and Technology (BUET). You can find me on LinkedIn, and ResearchGate. Read Full Bio

Leave a Comment