TechBuzzPro

How to Use cat, less, and head to Read Text Files in Linux

· 5 min read · Apps & Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Reading text files is one of the most common tasks you'll do in the Linux terminal. Three commands—cat, less, and head—cover almost every scenario: quickly dumping a file to the screen, paging through a long file, or peeking at just the first few lines. This guide walks through each command with practical examples, so you can pick the right tool for the job.

What You'll Need

  • A Linux system (or a terminal emulator on macOS/WSL) with a shell like Bash.
  • Basic familiarity with navigating directories (cd, ls).
  • A text file to practice with—any .txt, .log, or config file works.

Step-by-Step

  1. Open a terminal. Press Ctrl+Alt+T (most Linux desktops) or use your application menu.
  2. Create a sample file for practice. Run printf 'Line 1\nLine 2\nLine 3\n' > sample.txt. This makes a small file with three lines.
  3. View the whole file with cat. Type cat sample.txt and press Enter. The entire content prints to the terminal. cat is best for short files. For longer files, the output scrolls off the screen.
  4. Add line numbers with cat -n. Run cat -n sample.txt to display line numbers, which helps when referencing specific lines.
  5. Read the first few lines with head. Type head sample.txt. By default, it shows the first 10 lines. To show a specific number, use head -n 5 sample.txt for the first 5 lines.
  6. Read the last few lines with tail (bonus). Though not in the main title, tail is the counterpart to head: tail -n 5 sample.txt shows the last 5 lines. This is handy for logs.
  7. Page through a long file with less. Run less sample.txt. The file opens in a full-screen viewer. Use the arrow keys or Page Up/Page Down to scroll. Press q to quit back to the prompt.
  8. Search inside less. While in less, type a forward slash / followed by a search term, e.g., /Line, then press Enter. It jumps to the first match. Press n for the next match, N for the previous one.
  9. Jump to the beginning or end in less. Press g to go to the first line, and G to go to the last line.
  10. Open a file directly at a specific line with less. Use less +20 sample.txt to start at line 20. This is useful for large logs.
  11. Combine commands with pipes. For example, head -n 20 sample.txt | less shows the first 20 lines in a pager, so you can scroll if needed.
  12. Exit less properly. Press q. If you're stuck, Ctrl+C often cancels the current operation, then q exits.

Common Mistakes

  • Using cat on huge files. It floods the terminal and can freeze your session. Use less instead.
  • Forgetting the dash in -n. cat n sample.txt will try to open a file named n and fail.
  • Typing head -5 instead of head -n 5. The former works on some systems but is deprecated; the latter is portable.
  • Pressing Ctrl+C in less and expecting to exit. It may cancel a search, but you still need q to quit.
  • Assuming less is only for files. It can also read from a pipe: ls -l | less.

Tips

  • Use cat with > to create files quickly: cat > notes.txt then type and press Ctrl+D to save.
  • In less, press v to open the current file in your default text editor (like nano or vim).
  • Use head and tail together to view a range of lines: head -n 50 file.txt | tail -n 10 shows lines 41–50.
  • For real-time log monitoring, use tail -f. It updates as the file grows—press Ctrl+C to stop.
  • Set less options in your shell config. For example, export LESS='-N' always shows line numbers.

Troubleshooting

  • "No such file or directory" — Check your spelling and the file path. Use ls to list files in the current directory.
  • Terminal gets flooded with output from cat. Press Ctrl+C to stop. Then use less instead.
  • less doesn't open, or shows "terminal is not fully functional" — This happens when TERM is not set. Run export TERM=xterm and try again.
  • Search in less finds nothing. Make sure you're typing the pattern correctly; it's case-sensitive unless you use -i option (start with less -i).
  • You see binary characters or gibberish. The file may not be plain text. Use file sample.txt to check its type.
  • Can't remember which command to use? Rule of thumb: short file → cat, long file → less, just the start → head, just the end → tail.

Frequently Asked Questions

  • What's the difference between cat and less? cat prints the whole file to the terminal at once; less shows it one screen at a time with navigation and search.
  • Can less edit files? No, it's a pager (viewer). To edit, press v to open the file in your default editor.
  • How do I see line numbers in less? Start with less -N file or press -N while in less to toggle.
  • Is more the same as less? more is older and less flexible. less supports backward navigation and search, so it's generally preferred.
  • Can I use head to read the middle of a file? Not directly, but you can pipe: head -n 100 file | tail -n 20 shows lines 81–100.
  • What does cat stand for? It stands for "concatenate" — it can join multiple files together, but it's commonly used just to display one file.