📖 Scenario: You are working on a small project where you need to analyze text files to find out how many lines, words, and characters they contain. This is useful when checking reports, logs, or any text data.
🎯 Goal: Learn how to use the wc command to count lines, words, and characters in a text file.
📋 What You'll Learn
Create a text file named sample.txt with specific content.
Use a variable to store the filename.
Use the wc command with options to count lines, words, and characters.
Display the output clearly.
💡 Why This Matters
🌍 Real World
Counting lines, words, and characters is common when checking text files like logs, reports, or code files to understand their size or content quickly.
💼 Career
Many IT and scripting jobs require using command-line tools like wc to automate text processing and reporting tasks efficiently.
Progress0 / 4 steps
1
Create the text file sample.txt
Create a text file named sample.txt with exactly these three lines of text: Hello world This is a test file Counting words and lines
Linux CLI
Hint
Use echo with redirection > and >> to create and add lines to the file.
2
Store the filename in a variable
Create a variable called filename and set it to the string sample.txt.
Linux CLI
Hint
Use filename="sample.txt" to assign the filename.
3
Use wc to count lines, words, and characters
Use the wc command with options -l, -w, and -m on the file stored in the variable filename. Store the output in a variable called result.
Linux CLI
Hint
Use command substitution with $( ) to save the output.
4
Display the counting result
Print the content of the variable result to show the lines, words, and characters count of sample.txt.
Linux CLI
Hint
Use echo "$result" to print the output.
Practice
(1/5)
1. What does the wc command do in Linux?
easy
A. Counts lines, words, and characters in text
B. Changes file permissions
C. Lists files in a directory
D. Displays disk usage
Solution
Step 1: Understand the purpose of wc
The wc command is used to count lines, words, and characters in text files or input.
Step 2: Compare with other commands
Other options like changing permissions or listing files are done by different commands (chmod, ls), not wc.
Final Answer:
Counts lines, words, and characters in text -> Option A
Quick Check:
wc = counts lines, words, characters [OK]
Hint: Remember: wc = word count tool for text [OK]
Common Mistakes:
Confusing wc with ls or chmod
Thinking wc changes files
Assuming wc shows disk usage
2. Which of the following is the correct syntax to count only the number of lines in a file named file.txt?
easy
A. wc -w file.txt
B. wc -l file.txt
C. wc -c file.txt
D. wc -m file.txt
Solution
Step 1: Identify option for line count
The -l option with wc counts lines in the file.
Step 2: Verify other options
-w counts words, -c counts bytes, and -m counts characters, so they are incorrect for line count.
Final Answer:
wc -l file.txt -> Option B
Quick Check:
-l means lines [OK]
Hint: Use -l to count lines, not -w or -c [OK]
Common Mistakes:
Using -w or -c instead of -l
Forgetting the filename
Mixing up -m and -c options
3. Given a file example.txt containing the text:
Hello world
This is a test
What is the output of wc example.txt?
medium
A. 2 6 27 example.txt
B. 3 5 24 example.txt
C. 2 5 24 example.txt
D. 3 6 25 example.txt
Solution
Step 1: Count lines, words, and characters in the file
Lines: 2 ("Hello world" and "This is a test") Words: 6 ("Hello", "world", "This", "is", "a", "test") Characters: Count all characters including spaces and newlines. "Hello world" = 11 chars, newline = 1, "This is a test" = 14 chars, newline = 1, total = 27 chars.
5. You want to count only the characters (not bytes) in a UTF-8 encoded file data.txt. Which command should you use?
hard
A. wc -c data.txt
B. wc -w data.txt
C. wc -l data.txt
D. wc -m data.txt
Solution
Step 1: Understand difference between -c and -m
-c counts bytes, which may differ from characters in UTF-8 files because some characters use multiple bytes. -m counts characters, correctly handling multibyte UTF-8 characters.
Step 2: Choose correct option for character count
To count characters (not bytes), use wc -m.
Final Answer:
wc -m data.txt -> Option D
Quick Check:
-m counts characters, not bytes [OK]
Hint: Use -m for characters, -c counts bytes (important for UTF-8) [OK]
Common Mistakes:
Using -c and expecting character count
Confusing words (-w) with characters
Ignoring UTF-8 multibyte character effects
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.