FUNDAMENTALS A Complete Guide for Beginners
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]...
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
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:
- At first open the Ubuntu Terminal.
- 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.
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. 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.
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:
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.
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.
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.
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!
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.
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: 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.
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.
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.
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:
- 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.
- 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.
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.
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 run the command below:
ps -e | wc -l
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. 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.
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
- The āpasteā Command in Linux [6 Practical Examples]
- The āsplitā Command in Linux [6 Practical Examples]
- The āsedā Command in Linux [7 Practical Examples]
- The ātrā Command in Linux [6 Practical Examples]
- The āuniqā Command in Linux [6 Practical Examples]
- The āprintfā Command in Linux [8 Practical Examples]