wc: Print Newline, Word, and Byte Counts
August 12th, 2024 2:00 PM Mr. Q Categories: Command
Description:
The wc (word count) command is used to count the number of lines, words, and bytes (or characters) in a file. It can also be used to count the number of bytes or characters only.
Command:
wc [options] file
Sample Input
file.txt:
Hello world
This is a sample file.
Sample Output:
2 7 34 file.txt
Explanation:
2: Number of lines in the file.7: Number of words in the file.34: Number of bytes (or characters) in the file.
Options:
-l: Count lines only.-w: Count words only.-c: Count bytes only.-m: Count characters (useful for UTF-8 encoded files).
Example with -l Option:
To count lines only:
wc -l file.txt
Sample Output with -l:
2 file.txt
Example with -w Option:
To count words only:
wc -w file.txt
Sample Output with -w:
7 file.txt
Example with -c Option:
To count bytes only:
wc -c file.txt
Sample Output with -c:
34 file.txt
Example with -m Option:
To count characters (considering multi-byte characters):
wc -m file.txt
Sample Output with -m:
30 file.txt
This command is useful for quickly obtaining file metrics such as size, number of lines, and number of words.