Bird
Raised Fist0
Linux CLIscripting~20 mins

wc (word, line, character count) in Linux CLI - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
WC Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of wc with multiple files
What is the output of the following command if file1.txt contains 3 lines, 10 words, and 50 characters, and file2.txt contains 2 lines, 5 words, and 20 characters?

wc file1.txt file2.txt
Linux CLI
wc file1.txt file2.txt
A
 3 10 50 file1.txt
 2 5 20 file2.txt
 6 15 70 total
B
 3 10 50 file1.txt
 2 5 20 file2.txt
 5 15 70 total
C
 3 10 50 file1.txt
 2 5 20 file2.txt
 5 15 60 total
D
 3 10 50 file1.txt
 2 5 20 file2.txt
 5 15 70 file2.txt
Attempts:
2 left
💡 Hint
Remember that wc sums lines, words, and characters for the total line.
💻 Command Output
intermediate
1:30remaining
Counting words only with wc
What is the output of this command if input.txt contains the text "Hello world from Linux CLI"?

wc -w input.txt
Linux CLI
wc -w input.txt
A4 input.txt
B6 input.txt
CError: file not found
D5 input.txt
Attempts:
2 left
💡 Hint
Count the words separated by spaces.
📝 Syntax
advanced
1:00remaining
Correct usage of wc to count lines
Which option correctly counts only the number of lines in file.txt?
Awc -l file.txt
Bwc -c file.txt
Cwc -w file.txt
Dwc --lines file.txt
Attempts:
2 left
💡 Hint
The option for lines is a single letter.
🔧 Debug
advanced
1:30remaining
Identify the error in wc command usage
What error will this command produce?

wc -x file.txt
Linux CLI
wc -x file.txt
A
wc: invalid option -- 'x'
Try 'wc --help' for more information.
B0 file.txt
CCounts lines, words, and bytes normally
Dwc: file.txt: No such file or directory
Attempts:
2 left
💡 Hint
Check if -x is a valid wc option.
🚀 Application
expert
2:30remaining
Using wc in a script to count total words in multiple files
You want to write a script that sums the total number of words across three files: a.txt, b.txt, and c.txt. Which command will correctly output only the total word count?
Awc -w a.txt b.txt c.txt | grep total | awk '{print $2}'
Bwc -w a.txt b.txt c.txt | head -1 | awk '{print $1}'
Cwc -w a.txt b.txt c.txt | tail -1 | awk '{print $1}'
Dwc -w a.txt b.txt c.txt | awk '{sum += $1} END {print sum}'
Attempts:
2 left
💡 Hint
The total line is the last line of wc output when multiple files are given.

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

  1. Step 1: Understand the purpose of wc

    The wc command is used to count lines, words, and characters in text files or input.
  2. Step 2: Compare with other commands

    Other options like changing permissions or listing files are done by different commands (chmod, ls), not wc.
  3. Final Answer:

    Counts lines, words, and characters in text -> Option A
  4. 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

  1. Step 1: Identify option for line count

    The -l option with wc counts lines in the file.
  2. Step 2: Verify other options

    -w counts words, -c counts bytes, and -m counts characters, so they are incorrect for line count.
  3. Final Answer:

    wc -l file.txt -> Option B
  4. 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

  1. 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.
  2. Step 2: Match counts with options

    2 6 27 example.txt matches exactly: 2 lines, 6 words, 27 characters.
  3. Final Answer:

    2 6 27 example.txt -> Option A
  4. Quick Check:

    Lines=2, Words=6, Characters=27 [OK]
Hint: Count lines, words, chars carefully; wc shows all three by default [OK]
Common Mistakes:
  • Miscounting words or lines
  • Ignoring newline characters in count
  • Assuming output order is different
4. You run wc -l file.txt but get an error: wc: invalid option -- 'x'. What is the likely cause?
medium
A. The file file.txt does not exist
B. You forgot to specify the filename
C. You typed an invalid option like -x by mistake
D. The wc command is not installed

Solution

  1. Step 1: Analyze the error message

    The error says "invalid option -- 'x'", meaning wc received an option -x which it does not recognize.
  2. Step 2: Identify common causes

    This usually happens if you mistype an option, for example -x instead of -l or -w.
  3. Final Answer:

    You typed an invalid option like -x by mistake -> Option C
  4. Quick Check:

    Invalid option error means wrong flag [OK]
Hint: Check option spelling carefully if wc shows invalid option error [OK]
Common Mistakes:
  • Confusing file not found with invalid option error
  • Assuming missing filename causes invalid option error
  • Ignoring typo in command options
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

  1. 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.
  2. Step 2: Choose correct option for character count

    To count characters (not bytes), use wc -m.
  3. Final Answer:

    wc -m data.txt -> Option D
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard