0
0
Linux CLIscripting~15 mins

wc (word, line, character count) in Linux CLI - Mini Project: Build & Apply

Choose your learning style9 modes available
Counting Words, Lines, and Characters with wc
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use echo "$result" to print the output.