learn to Use a WC (Word Count) Command in Linux
In this article we are going to know how to count number of lines, Words, Character in Linux with use of command called WordCount in short ‘wc’. From this command we are find out number of newline count, word count, byte and characters count in a files specified by the file arguments.
The syntax of wc command as shown below:
# wc [options] filenames
The following are the options and usage provided by the command :
- wc -l : Prints the number of lines in a file.
- wc -w : Prints the number of words in a file.
- wc -c : Displays the count of bytes in a file.
- wc -m : Prints the count of characters from a file.
- wc -L : Prints only the length of the longest line in a file.
So, let’s see how we can use the ‘wc’ command with their few available arguments and examples in this article. We have used the ‘tom.txt’ file for testing the commands.
1. A Basic Example of WC Command
The ‘wc’ command without passing any parameter will display a basic result of ‘tom.txt’ file. The three numbers shown below are 10 (number of lines), 9 (number of words) and 69 (number of bytes) of the file.
# wc tom.txt
2. Count Number of Lines
To count number of newlines in a file use the option ‘-l’, which prints the number of lines from a given file. Say, the following command will display the count of newlines in a file. In the output the first filed assigned as count and second field is the name of file.
# wc -l tom.txt
3. Display Number of Words
Using ‘-w’ argument with ‘wc’ command prints the number of words in a file. Type the following command to count the words in a file.
# wc -w tom.txt
4. Count Number of Bytes and Characters
When using options ‘-c’ and ‘-m’ with ‘wc’ command will print the total number of bytes and characters respectively in a file.
# wc -c tom.txt
# wc -m tom.txt
5. Display Length of Longest Line
The ‘wc’ command allow an argument ‘-L’, it can be used to print out the length of longest (number of characters) line in a file. So, we have the longest character line (‘Scientific Linux’) in a file.
#wc -L tom.txt
6. Check More WC Options
For more information and help on the wc command, simple run the ‘wc –help’ or ‘man wc’ from the command line.
# wc --help
from all the following commands we are knowing about the wc command & how it works .
With the help of these are knowing how to count number of lines, Words, Character in Linux.