What is the wc command in Linux?
The wc command displays statistical information about a file such as the number of lines, words, characters.
Trivia: wc stands for word count.
The syntax for the wc command is:
wc [options] [files]
wc command has the following options:
- –l : Prints the number of lines only
- –w : Prints the number of words only
- -c : Prints the number of bytes only
- –m : Prints the count of characters (different than the number of bytes for non-text files)
- –L : Prints the length of the longest line in the file
- —files0-from=F : Get the file names from file F (file names must be separated by the NULL character)
6 practical examples of wc command in Linux
If you use wc command with just the input file name(s), without any options, it will show you the count of the lines, words and bytes at the same time.
wc agatha.txt 20 80 457 agatha.txt
In the above output:
- 20 is the number of lines
- 80 is the number of words
- 457 is the number of bytes
Now that you are aware of the wc command options, let’s see some examples of wc command.
1. Count the number of lines in a file
If you just want to know the number of lines in a text file, you can use the wc command with option ‘l’. Basically, it counts the number of newlines in the file.
wc -l agatha.txt 20 agatha.txt
2. Count the number of words in a file
If you just want to know the number of words in a text file, you can use the wc command with option ‘w’. It will display the number of whitespace-delimited words.
wc -w agatha.txt 80 agatha.txt
3. Count the number of bytes and characters in a file
If it’s a regular text file, the number of bytes and characters should be the same. But it will differ for the non-text files.
To display the number of bytes in a file, use wc command with option ‘c’:
wc -c agatha.txt 457 agatha.txt
To display the number of characters in a file, use wc command with option ‘m’:
wc -m agatha.txt 457 agatha.txt
I know you must be thinking that option ‘c’ is more suited for the task of counting characters but Unix/Linux commands have always been strange.
4. Display length of the longest line of a file
The ‘L’ option of wc command displays the length (number of characters) of the longest line of a file.
wc -L agatha.txt 31 agatha.txt
5. Display number of lines, words, characters for multiple files
You can use more than one file with wc command. It will display the output for each of the files one by one along with the total count in all the files.
For example, if I want to display the number of lines of two files, it would be like this:
wc -l agatha.txt sherlock.txt 20 agatha.txt 12 sherlock.txt 32 total
6. Use wc with other commands using pipes
What you saw so far was the straightforward wc command option examples. You can further utilize wc with the output of other commands using the wonderful pipes.
For example, you can redirect the output of ls command to wc and thus you can count the total number of files and sub-directories in the given given directory.
ls | wc -l
The possibilities are endless. You just need to use your little grey cells to utilize wc command in various situations.
Bonus Tip: Remove the filename from wc command output
You might have noticed that wc command output consists of the file names. If just want to get the number without the filename, you may use it with the cut command and get rid of the filename from the output.
wc -l agatha.txt | cut -d ' ' -f 1
You can also get rid of the filename by using the wc command in this way:
wc -l < agatha.txt
I hope you liked this tutorial on using wc command in Linux. You can also learn how to count the number of files in a directory in Linux by combining wc command and ls command.
If you have questions or suggestions, please leave a comment below. If you liked the article, please share it on social media and help us reach more people.