wc — Linux basic command
Linux / Basic Linux commands / wc.html
A command that counts the number of lines, words, and bytes in a file.
Basic Information
| Command Name | wc (word count) |
|---|---|
| One-Line Introduction | A command that counts the number of lines, words, and bytes in a file. |
| Provided By | GNU coreutils (pre-installed in all Linux distributions) |
| Official Documentation | https://man7.org/linux/man-pages/man1/wc.1.html |
| Supported OS | Linux, macOS, BSD, WSL |
| Installation | Pre-installed (no separate installation required) |
| Category | Basic Linux Command (Tags: line count, word count, count, linux, basic) |
What is this command?
wc stands for word count, and it is a command that counts and outputs the number of lines, words, and bytes in a file or input. It is frequently used to quickly gauge the size of text files or to count the number of results from other commands.
You can select only the necessary information using the -l (line), -w (word), and -c (byte) options, and it is often used with pipes.
What problem does it solve?
- Check the number of lines in a file.
- Determine the size of a text file.
- Count the number of results from a command.
- Check the line count of a code file.
Main Features
Counting Lines (-l)
Counts and outputs the number of lines in a file.
- Use Case: When checking the number of lines in a log file or the line count of code.
- Result Obtained: The exact line count is output.
- Limitations: If the last line does not end with a newline, it is counted as 1 line.
Counting Words (-w)
Counts and outputs the number of words separated by spaces.
- Use Case: When checking the word count of a document.
- Result Obtained: The word count is output.
- Limitations: Korean text is counted based on spaces between words.
Counting Bytes (-c)
Outputs the total number of bytes in a file.
- Use Case: When checking the file size.
- Result Obtained: The byte count is output.
- Limitations: Multi-byte characters are counted in byte units.
Maximum Line Length (-L)
Outputs the length of the longest line.
- Use Case: When inspecting the format of a text file.
- Result Obtained: The maximum line length is output.
- Limitations: Handling of tab characters may vary depending on the environment.
Installation and Execution
Verify Pre-installation
It is included by default in all Linux distributions.
which wc
wc --version
Separate Installation (Lightweight Environment)
It is usually already installed.
sudo apt install coreutils # Debian/Ubuntu
Practical Usage Examples
Basic Count
Counts lines, words, and bytes all together.
# Output all information
touch sample.txt
echo "Hello World Example" > sample.txt
wc sample.txt
# Result: 1 3 23 sample.txt
# lines words bytes filename
Counting Specific Items
Selects and outputs only the needed items.
# Lines only
touch sample2.txt
echo -e "line1\nline2\nline3" > sample2.txt
wc -l sample2.txt
# Result: 3 sample2.txt
# Words only
echo "one two three four" | wc -w
# Result: 4
# Bytes only
echo "Hello" | wc -c
# Result: 6
Using with Pipes
Counts the number of results from other commands.
# Count the number of files in a list
ls /etc/*.conf | wc -l
# Count the number of search results
grep -r "TODO" . | wc -l
# Count the number of processes
ps aux | wc -l
# Count log lines
tail -1000 access.log | wc -l