Linux wc Command
Learn Linux wc Command with examples
Published
- Linux wc Command
- wc Command Syntax
- wc Command Example
- wc Command Multiple File Example
- echo Command with wc Pipe
- ls Command with wc Pipe
- wc -l option
- wc -w option
- wc -c option
- wc -m option
Linux wc Command
In Linux we use “wc” command count the number of words in a file. The wc command on Linux is short for word count.
The wc command can count the number of lines, words, characters, and bytes from standard input or in a file.
wc Command Syntax
~$ wc OPTION... [FILE]...
Option | Description |
-l --lines |
Print the number of lines. |
-c --bytes |
Print the number of bytes. |
-L --max-line-length |
Print the length of the longest line. |
-m --chars |
Print the number of characters. |
-w --words |
Print the number of words. |
wc Command Example
~$ wc logfile.txt
3 15 66 example.txt
The output explains that the file contains the following.
- 3 new lines
- 15 words
- 66 bytes.
wc Command Multiple File Example
~$ wc file_1.txt file_2.txt
1 2 13 file_1.txt
4 29 169 file_2.txt
5 31 182 total
wc Command Pipe with Other Commands
echo Command with wc Pipe
echo command used to print message or store message to a file using » operator. We can use echo with message and pipe with wc to count the lines, words, and bytes.
~$ echo "How many words are in this sentence?" | wc
2 15 30
ls Command with wc Pipe
We can use ls -l with wc -l pipe to counts the number of files in a directory.
~$ ls -l | wc -l
14
wc -l or –lines option
Use the -l or –lines option if you only want to determine the number of lines in a file.
~$ wc -l example.txt
4 example.txt
wc -w or –words option
wc -w or –words option can be used if you only want to determine the number of words in a file.
~$ wc -w file_1.txt
wc -c or –byte option
Use the -c or –bytes option if you only want to determine the number of bytes in a file.
~$ wc -c file_1.txt
wc -m or –chars option
wc -m or –chars option count the number of characters in a file. It will be the same number as the bytes.
~$ wc -m file_1.txt