How to Use cat, less, and head to Read Text Files in Linux
In this article
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
- Open a terminal. Press
Ctrl+Alt+T(most Linux desktops) or use your application menu. - 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. - View the whole file with
cat. Typecat sample.txtand press Enter. The entire content prints to the terminal.catis best for short files. For longer files, the output scrolls off the screen. - Add line numbers with
cat -n. Runcat -n sample.txtto display line numbers, which helps when referencing specific lines. - Read the first few lines with
head. Typehead sample.txt. By default, it shows the first 10 lines. To show a specific number, usehead -n 5 sample.txtfor the first 5 lines. - Read the last few lines with
tail(bonus). Though not in the main title,tailis the counterpart tohead:tail -n 5 sample.txtshows the last 5 lines. This is handy for logs. - Page through a long file with
less. Runless sample.txt. The file opens in a full-screen viewer. Use the arrow keys orPage Up/Page Downto scroll. Pressqto quit back to the prompt. - Search inside
less. While inless, type a forward slash/followed by a search term, e.g.,/Line, then press Enter. It jumps to the first match. Pressnfor the next match,Nfor the previous one. - Jump to the beginning or end in
less. Pressgto go to the first line, andGto go to the last line. - Open a file directly at a specific line with
less. Useless +20 sample.txtto start at line 20. This is useful for large logs. - Combine commands with pipes. For example,
head -n 20 sample.txt | lessshows the first 20 lines in a pager, so you can scroll if needed. - Exit
lessproperly. Pressq. If you're stuck,Ctrl+Coften cancels the current operation, thenqexits.
Common Mistakes
- Using
caton huge files. It floods the terminal and can freeze your session. Uselessinstead. - Forgetting the dash in
-n.cat n sample.txtwill try to open a file namednand fail. - Typing
head -5instead ofhead -n 5. The former works on some systems but is deprecated; the latter is portable. - Pressing
Ctrl+Cinlessand expecting to exit. It may cancel a search, but you still needqto quit. - Assuming
lessis only for files. It can also read from a pipe:ls -l | less.
Tips
- Use
catwith>to create files quickly:cat > notes.txtthen type and pressCtrl+Dto save. - In
less, pressvto open the current file in your default text editor (likenanoorvim). - Use
headandtailtogether to view a range of lines:head -n 50 file.txt | tail -n 10shows lines 41–50. - For real-time log monitoring, use
tail -f. It updates as the file grows—pressCtrl+Cto stop. - Set
lessoptions 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
lsto list files in the current directory. - Terminal gets flooded with output from
cat. PressCtrl+Cto stop. Then uselessinstead. lessdoesn't open, or shows "terminal is not fully functional" — This happens when TERM is not set. Runexport TERM=xtermand try again.- Search in
lessfinds nothing. Make sure you're typing the pattern correctly; it's case-sensitive unless you use-ioption (start withless -i). - You see binary characters or gibberish. The file may not be plain text. Use
file sample.txtto 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
catandless?catprints the whole file to the terminal at once;lessshows it one screen at a time with navigation and search. - Can
lessedit files? No, it's a pager (viewer). To edit, pressvto open the file in your default editor. - How do I see line numbers in
less? Start withless -N fileor press-Nwhile inlessto toggle. - Is
morethe same asless?moreis older and less flexible.lesssupports backward navigation and search, so it's generally preferred. - Can I use
headto read the middle of a file? Not directly, but you can pipe:head -n 100 file | tail -n 20shows lines 81–100. - What does
catstand for? It stands for "concatenate" — it can join multiple files together, but it's commonly used just to display one file.