Unit 3 路 Linux^2 路 Lesson 25 of 34
wc
Count on me.
wc stands for word count, and it counts up the lines, words, and characters in a file. Great for when your English teacher says the essay needs to be at least 500 words.
[user@linux]$ wc notes.txt
12 58 301 notes.txt
That's 12 lines, 58 words, and 301 characters.
Counting just one thing
Most of the time you'll only care about one of those thingies, so you can use certain flags.
wc + pipes = 馃
Where wc really shines is with pipes (remember | from Redirect Me?). Want to know how many lines in a log file have "error" in them?
[user@linux]$ grep "error" log.txt | wc -l
grep finds every line with "error" in it, and wc counts them up. Teamwork makes the dream work.
And since you just learned redirection, you can save the answer too:
[user@linux]$ grep "error" log.txt | wc -l > errors.txt
Try it
There's a folder called count in your home directory with two files in it:
auth.log, every login attempt on this box today, someACCEPTEDand someFAILEDessay.txt, Alex's English essay, which Alex swears is at least 500 words
There are thousands of lines in there. You are not counting these by hand.
Live Linux terminal
This lesson comes with a real Ubuntu machine that runs in your browser, so you type every command yourself. Create a free account to do this part.
Do this lesson for real
With a free account you get the terminal, the challenges and the checks that go with every lesson in Intro to Linux, and your progress is saved as you go. CyberAcad is a 501(c)(3) nonprofit, so there is nothing to pay.