How to use the Linux wc command to count words, characters and row in files
By using the Linux wc command you can display the number of rows, words and characters in a file. The command can also be used on multiple documents and is able to add the results together.
What is the Linux wc command?
If you want to count the amount of characters in a text file, then the Linux wc command is your best option. The command, whose acronym stands for word count, is carried out in the command path and is one of the standard tools included in Linux distributions such as Debian or Ubuntu. wc looks at bytes, words and characters and will give an individual or combined result. While most text programs have a similar option, if you want to get the results directly from the terminal without opening a new program Linux wc is the perfect choice.
How does the Linux wc command work?
The way Linux wc works is easy to explain. When you use the command you will get an exact list of different parameters which you can set by using the available options or leave it to the standard listing. The default setting gives you the number of rows, words and characters one after the other. You can also run the command on several text files at the same time. You will then get an exact listing for the individual documents and, in addition, the overall total of the different rows, words and characters.
What does the Linux wc syntax look like?
The syntax looks as follows:
$ wc [Option] [File]
shellIf you don’t want to use the options, the result will look as mentioned above with rows, words and characters. If you don’t enter a file, the command will automatically go to the standard entry.
What are the options in Linux wc?
You can modify the wc command with various options:
- -c: Counts only the number of bytes in a file.
- -L: Returns the length of the longest row.
- -l: Counts only the number of rows in a file.
- -m: Returns only the characters in a file.
- -w: Counts only the number of words in a file.
Examples of a wc command
To finish off, we will show you some practical examples so you can use the Linux wc command in your daily life.
$ wc example.txt
shellThis will give you the number of rows, words and characters in the example.txt file.
$ wc example.txt index.html
shellYou can use this command to deliver the individual word count as per the above schema for each file. The result will also contain a row with a total for both files.
$ wc -w example.py
shellThis will only show the number of words in the document example.py.