AI Hints
Home›Linux Tutorial›Reading Files
Linux · Lesson 33 of 120

less and wc

Reading and counting.

⏱ 2 min read▶ Video at 41:08🎓 Free
Watch this lesson in the Linux courseWatch on YouTube ↗Full Linux course

Sometimes you want to read a long file properly, page by page, and search inside it. And sometimes you just want to know how big it is. Less, and WC, do those jobs.

less and wc
From the video: less and wc
  • Read page by page
  • Or count what's inside
  • less and wc

Reading with less

Less opens a file in a viewer, one screen at a time. Space goes down a page, B goes back up. The arrow keys move one line. Slash, followed by a word, searches for it, and N jumps to the next match. And Q quits. It's the same viewer that man uses for manual pages.

Space / bnext / previous page
Arrowsone line at a time
/wordsearch, n for next
qquit

Counting with wc

WC stands for word count. It prints three numbers. Lines, words, and characters, followed by the name. Our notes have three lines, six words, and thirty characters. Dash L counts just the lines, which is the one you'll use most.

Terminal
wc notes.txt
 3  6 30 notes.txt
wc -l logs/server.log
60 logs/server.log

Counting files

Here's a neat trick. LS Pictures shows two photos. Now LS, then a vertical bar, then WC dash L, counts the items in the folder. The bar is called a pipe. It sends the output of one command into another. Pipes are so useful, they get their own lessons later.

Terminal
ls Pictures
beach.jpg  party.jpg
ls Pictures | wc -l
2

Summary

Less reads long files page by page, Q quits, and WC counts lines, words, and characters. Next, writing text into files from the terminal.

Key takeaways
  • less <file>
  • q to quit
  • wc -l counts lines
Next up: Writing with Redirection — > sends output to a file