Some practical examples of wc command in Linux, how to count the number of lines, words and also characters of a text file.
What is wc command in Linux?
The wc command displays statistical information about the file, such as the number of lines, words, characters.
General information: wc stands for the number of words.
The syntax for the wc command is:
wc [options] [files]
The wc command has the following options:
- -l: Prints only the number of lines
- -w: Print only word count
- -c: Print only the number of bytes
- -M: Prints the number of characters (other than the number of bytes for non-text files)
- -L: Prints the length of the longest line in the file
- -files0-from = F: Get filenames from file F (filenames must be NULL delimited)
5 practical examples of wc command in Linux
In this example we are going to use the files alex.txt and andreyex.txt. You can load these files using the wc command specified in the examples in this article.
Counting the number of characters in the text online: https://charactercount.org/ru, a convenient tool for counting the number of characters in the text, you will always know how many characters are in the text and words, and it is also possible to use autosave when working with text.
If you use the wc command with only input filenames, without any parameters, it will show you the number of lines, words, and bytes at the same time.
wc alex.txt 15 45 366 alex.txt
In the above form:
- 15 – number of lines
- 45 – word count
- 366 – number of bytes
Now that you know the options for the wc command, let’s see some examples of the wc command.
1. Count the number of lines in the file
If you just want to know the number of lines in a text file, you can use the wc command with the ‘l’ option. Basically, it counts the number of lines in the file.
wc -l alex.txt 20 alex.txt
2. Count the number of words in the file
If you just want to know the number of words in a text file, you can use the wc command with the ‘w’ option. It will display the number of words with spaces.
wc -w alex.txt 80 alex.txt
3. Count the number of bytes and characters in the file
If it is a regular text file, the number of bytes and characters must be the same. But this will be different for non-text files.
To display the number of bytes in a file, use the wc command with the ‘c’ option:
wc -m alex.txt 366 alex.txt
To display the number of characters in a file, use the wc command with the ‘m’ option:
wc -m alex.txt 366 alex.txt
We know that you must be thinking that the “c” option is more suitable for a character counting task, but Unix / Linux commands have always been weird.
4. Display length of the longest line of the file
The “L” parameter of the wc command displays the length (number of characters) of the longest line in the file.
wc -L alex.txt 31 alex.txt
5. Display the number of lines, words, characters for multiple files.
You can use more than one file with the wc command. It will display the output for each of the files, one at a time, along with the total in all files.
For example, if we want to display the number of lines from two files, it would look like this:
wc -l alex.txt andreyex.txt 20 alex.txt 12 andreyex.txt 32 total
Bonus tip: wc with other commands using pipes (|)
What you’ve seen so far was a simple example of a wc command. You can optionally use wc with the output of other commands by using pipes (|).
For example, you can redirect the output of the ls command to wc and so that you can count the total number of files and subdirectories in a given given directory.
ls | wc -l
The possibilities are endless. You just need to use your little gray boxes to use the wc command in different situations.
You may have noticed that the output of the wc command consists of filenames. If you just want to get the number without the filename, you can use it with the cut command and get rid of the filename from the output.