0
0
Linux CLIscripting~5 mins

wc (word, line, character count) in Linux CLI - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want to know how many lines, words, or characters are in a text file. The wc command helps you count these quickly from the command line.
When you want to check how many lines a log file has to estimate its size.
When you need to count words in a text document to meet a writing requirement.
When you want to find out how many characters are in a configuration file.
When you want to quickly see the size of output from another command by counting lines or words.
When you want to verify if a file is empty by checking if it has zero lines or characters.
Commands
This command counts the number of lines, words, and bytes in the file named sample.txt and shows all three counts.
Terminal
wc sample.txt
Expected OutputExpected
10 50 300 sample.txt
This command counts only the number of lines in sample.txt. Use -l when you only want line count.
Terminal
wc -l sample.txt
Expected OutputExpected
10 sample.txt
-l - Count lines only
This command counts only the number of words in sample.txt. Use -w when you only want word count.
Terminal
wc -w sample.txt
Expected OutputExpected
50 sample.txt
-w - Count words only
This command counts only the number of bytes in sample.txt. Use -c when you want byte count.
Terminal
wc -c sample.txt
Expected OutputExpected
300 sample.txt
-c - Count bytes only
Key Concept

If you remember nothing else, remember: wc counts lines, words, and bytes in files or input quickly and can show all or just one count with flags.

Common Mistakes
Running wc without specifying a file or input.
wc waits for input from the keyboard and seems to hang, confusing beginners.
Always provide a filename or pipe input to wc, for example: wc filename.txt or echo "text" | wc
Using multiple flags together like wc -lw without knowing it shows combined counts.
The output can be confusing because it shows multiple counts in one line without clear labels.
Use one flag at a time for clarity, or use wc without flags to see all counts clearly.
Summary
wc counts lines, words, and bytes in text files or input.
Use flags -l, -w, or -c to count only lines, words, or bytes respectively.
Always provide a file or input to wc to avoid waiting for keyboard input.