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]...
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)
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.
Download this file to work with the “wc” command in LinuxExample 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.
Similar Readings
- The “grep” Command in Linux [10+ Practical Examples]
- The “sort” Command in Linux [16 Practical Examples]
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.
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 following
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.
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.
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.
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!
Note: ASCII encoding uses one byte per character. That is why we are seeing count and byte number is 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.
Note: The cut command is showing the first two characters. You can change it according to your need. For example, if you want to see the first three characters, change “1-2” to “1-3”.
Similar Readings
- The “nano” Command in Linux [13 Practical Examples]
- The “cut” Command in Linux [8 Practical Examples]
- The “jed” Command in Linux [8 Practical Examples]
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.
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 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.
Example 11: Combining “find” command with “wc” command
There is a directory named walid in 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.
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.
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.
Similar Readings
- The “vi” Command in Linux [6 Practical Examples]
- The “vim” Command in Linux [8 Practical Examples]
- The “egrep” Command in Linux [10+ Practical Examples]
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.
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 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.
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