The ā€œwcā€ Command in Linux [15 Practical Examples]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

The wc command in Linux finds out the number of lines, word count, character count, byte count of one or multiple files, and number of lines of the file/files when provided to it as an argument. Moreover, we can combine the wc command with other commands, making the command versatile. In this article, I will show you all sorts of usage of the wc command in Linux with 15 practical examples.

The Linux “wc” Command 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.

The Linux “wc” Command Options

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

man wc

OptionsĀ 

DescriptionĀ 

-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
– -version displays version information
– -help shows help

Note: The options in Linux CLI(Command Line Interface) are all case-sensitive. Be careful in using them.

15 Practical Examples of the ā€œwcā€ Command in Linux

Now I am going to show you 15 hands-on 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. By default, it shows a four-columnar output (number of lines, words, characters, and file name respectively). All you need is to provide a file name as an argument. In my case, it is city. Just follow the steps below to do the same:

  1. At first open the Ubuntu Terminal.
  2. Type the following command in the command prompt, and press ENTER:
    wc city

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. To do the same, run the following command in the prompt:

wc country city

Here you can see, that in the output, the different files are 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: Display Multiple Flies with a Common Naming Pattern

Letā€™s say you have a lot of files. It is tedious to type all the names one by one. But if the files follow a particular pattern, you can use the wc command with the “wildcard pattern” matching. For Instance, the file city and country both have the letter “c”Ā at the beginning. I will use this as a pattern employing the expression c*. You can also do that by yourself by running the command below:

wc c*

Now the files will be listed out like the following:Using pattern to show multiple files

Example 4: Display Word Count Using the ā€œwcā€ Command in Linux

To print only the word count of a file, use the command wc with the option -w that indicates the command to fetch only the word count. The full command is wc -w <file_name>. For example, I have used the below command to display the word count of the file city:

wc -w city

Now, as output, 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 line count of a file, type the wc command with the -l flag like the followingĀ  on the prompt and press ENTER:

wc -l city

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, you have to use the wc command with the -m option. Here’s an example:

wc -m city

Upon running the command, 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, execute the wc command with the -c option like below:

wc -c city

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 use the wc command and the cut command using the piping concept. Now, type the following command in the command prompt and press the ENTER button:

wc -w city | cut -c 1-2

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, change 1-2 to 1-3Ā if you want to see the first three characters.

Similar Readings


Example 9: Display Length of the Longest Line in a Text

The wc<strong>Ā </strong>command in Linux can print the longest line in a text. To do the same, run the wc command with the -L flag in the following format:

wc -L city

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: Display the 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 running the following command:

ls -l /usr/bin | wc -l

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 want to know how many files ended with the extension ā€œ.txtā€. I can do it by running the command in terminal below using the wc and the find command :

find . -name ā€œ*.txtā€ | wc -l

Now see! It shows 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 using the wc and the grep command:

  1. Type the following command in the command prompt and press the ENTER button:
    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.

  2. Now, type Password if necessary and press ENTER again.

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. Run the command below to do the same:

cat city | wc -w

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 run the command below:

ps -e | wc -l

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. To do the same, run the following commands in the command prompt:

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

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.

People Also Ask

Can the “wc” command read from standard input?

Yes. The wc command can read from standard input apart from reading from a file. This is handy when you want to count the words, lines, or characters of the stream of contents that are not saved in a particular file.

Just type the wc command, press ENTER, and then type the text. Finally, type CTRL+D after you are done with typing and the wc command will display the counts on the prompt.

Can I know the version of the Linux “wc” command?

Yes, you can. Use the wc command with the --version option to display the version of the wc command that is currently running on your system.

wc --version

What is the Linux word count command?

The Linux command wc, which stands for word count, is the command that calculates a file’s word, line, character, or byte count passed as an augment to the command. For instance, to print the word count of the file hello.txt, run the command:

wc -w hello.txt

Can the “wc” command in Linux count the output of another command?

Yes, it can. You can use the piping operator ā€œ|ā€ to employ the output of a command as the input to the wc command. For instance, you can use the command ls -l | wc to display the word, line, and character counts of the detailed document list of your current working directory.


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