The wc
(word count) command is used to count the number of lines, words, and bytes in files or standard input in a Linux system.
-
Syntax:
wc [options] [file...]
-
Examples:
wc file.txt
- Displays the number of lines, words, and bytes in
file.txt
.
wc -l file.txt
- Counts only the lines in
file.txt
.
echo "Hello World" | wc -w
- Counts the words in the string "Hello World".
wc -c file.txt
- Counts the number of bytes in
file.txt
.
- Displays the number of lines, words, and bytes in
-
-l
:- Counts the number of lines.
wc -l file.txt
- Displays only the line count of
file.txt
.
-
-w
:- Counts the number of words.
wc -w file.txt
- Displays only the word count of
file.txt
.
-
-c
:- Counts the number of bytes.
wc -c file.txt
- Displays only the byte count of
file.txt
.
-
-m
:- Counts the number of characters.
wc -m file.txt
- Displays only the character count of
file.txt
.
-
-L
:- Displays the length of the longest line in the file.
wc -L file.txt
- Shows the length of the longest line in
file.txt
.
-
--help
:- Displays help information about the
wc
command.
wc --help
- Shows usage information and options.
- Displays help information about the
-
Combining Options:
- Combine options to get multiple counts in one command.
wc -l -w file.txt
- Displays both the line and word count of
file.txt
.
-
Processing Multiple Files:
- You can pass multiple files to
wc
to get counts for each file and a total.
wc file1.txt file2.txt
- Displays counts for both
file1.txt
andfile2.txt
, followed by a total.
- You can pass multiple files to
-
Using Piping:
wc
can be combined with other commands using pipes to process output.
ls -l | wc -l
- Counts the number of lines of output from
ls -l
, effectively counting the number of items in a directory.
The wc
command is a simple but versatile tool for counting lines, words, bytes, and characters in files or input streams. It’s particularly useful for analyzing text files, processing command output, and performing quick text-based data analysis.