Linux wc Command
The wc
(word count) command in Linux is used to count lines, words, and characters in a text file or input. It can be very helpful for quickly summarizing the content of files.
Basic Syntax
Common Options
-l
: Counts only the number of lines.-w
: Counts only the number of words.-c
: Counts only the number of bytes (characters).-m
: Counts only the number of characters (useful for multibyte characters).-L
: Displays the length of the longest line in the input.
Examples with Output
Example 1: Counting Lines, Words, and Characters
Suppose you have a file named file.txt
with the following content:
Command
Output
Explanation:
3
is the number of lines in the file.12
is the number of words.65
is the number of characters (including spaces and newline characters).
Example 2: Counting Only Lines
To count only the lines in the file, use the -l
option.
Command
Output
Explanation:
- The output indicates that there are 3 lines in
file.txt
.
Example 3: Counting Only Words
To count only the words, use the -w
option.
Command
Output
Explanation:
- This shows that there are 12 words in
file.txt
.
Example 4: Counting Only Characters
To count the characters (bytes), use the -c
option.
Command
Output
Explanation:
- This indicates that there are 65 characters in
file.txt
.
Example 5: Counting Only the Longest Line
To find the length of the longest line, use the -L
option.
Command
Output
Explanation:
- The output shows that the longest line in
file.txt
is 42 characters long.
Example 6: Counting Multiple Files
You can also count lines, words, and characters in multiple files at once. Suppose you have another file, file2.txt
:
Command
Output
Explanation:
- The output shows counts for each file followed by a total count for all files combined.
Summary of Common wc
Commands
Command | Description |
---|---|
wc file.txt | Counts lines, words, and characters in file.txt . |
wc -l file.txt | Counts only the number of lines in file.txt . |
wc -w file.txt | Counts only the number of words in file.txt . |
wc -c file.txt | Counts only the number of characters in file.txt . |
wc -L file.txt | Displays the length of the longest line in file.txt . |
wc file1.txt file2.txt | Counts lines, words, and characters in multiple files. |
The wc
command is a simple yet powerful tool for summarizing text files and understanding their content in Linux.