Use head to see the beginning of a file, tail to see the end, and cat to display the entire file or join files together. For most quick checks, head and tail are safer than cat because they avoid flooding your terminal with thousands of lines.
What Each Command Does
All three commands are standard GNU core utilities found on virtually every Linux distribution. They read text files and write them to standard output (your terminal by default).
- cat (concatenate) prints the whole file from the first byte to the last. It can also merge multiple files in order:
cat file1 file2 > combined. - head prints the first 10 lines by default. Use
-nto change the count, e.g.,head -n 5 file. - tail prints the last 10 lines by default. Use
-nfor a custom count, and-fto follow a file as it grows (essential for logs).
How They Differ in Practice
The key difference is scope. cat reads everything, which is fine for small config files but problematic for large logs or binary data. head and tail read only a slice, so they start faster and use less memory—though for truly huge files, all three still scan from the beginning unless you use tail -n on a file that supports seeking (most regular files do, so tail jumps straight to the end).
Another practical difference: cat with no arguments reads from standard input, which makes it useful for quickly typing multi-line text into a file: cat > notes.txt (press Ctrl+D to finish). Neither head nor tail does that.
How to Choose: A Simple Decision Tree
Ask yourself what you need to see:
- Do you need the entire file, or to combine files? → cat
- Do you only need the top of the file (headers, config starts)? → head
- Do you need the bottom (recent log entries, last lines of a CSV)? → tail
- Are you monitoring a log that keeps growing? → tail -f
If you are unsure how long the file is, start with head or tail. You can always run cat later if you need the middle.
Common Mistakes and Pitfalls
The most frequent mistake is using cat on a large file and having the terminal scroll for minutes. For example, a 500 MB log file will print every line, which can freeze your session. Use head or tail instead.
Another issue: cat on a binary file (like an image or executable) will output garbage and can mess up your terminal settings. If you accidentally do this, run reset to fix the display.
Also, remember that tail -f never exits on its own. Press Ctrl+C to stop following. Without the -f, tail exits normally.
Real-World Examples
Here are typical uses you will encounter:
- Check the first few lines of a CSV to see column headers:
head -n 3 data.csv - View the last error in a system log:
tail -n 20 /var/log/syslog - Watch a web server log live:
tail -f /var/log/nginx/access.log - Combine two text files into one:
cat part1.txt part2.txt > full.txt - Quickly create a file with a few lines:
cat > todo.txtthen type and press Ctrl+D
Alternatives and Related Tools
If you need to view a file interactively, less is a better choice than cat—it shows one screen at a time and lets you scroll both ways. For searching, use grep directly on the file rather than piping cat into it (a common anti-pattern called “useless use of cat”). For example, grep error file.log is more efficient than cat file.log | grep error.
For extracting specific line ranges, sed -n '10,20p' file works well. And if you need to see both the beginning and end of a file, you can run head and tail separately, or use sed -n '1,5p;$p' file for a quick peek.
Performance and Resource Use
For most files, performance differences are negligible. But on very large files (gigabytes), head stops reading after the requested lines, so it finishes almost instantly. tail also stops early because it can seek to the end. cat reads the entire file, which takes time and disk I/O. If you are piping into another program, avoid cat unless you actually need to concatenate multiple files.
Frequently Asked Questions
Q: Can I use head and tail together to see a specific range of lines?
Yes. For example, to see lines 11–20, use head -n 20 file | tail -n 10. This pipes the first 20 lines into tail, which then shows the last 10 of those.
Q: What does ‘tail -f’ do exactly?
It follows the file, meaning it prints new lines as they are appended. This is invaluable for monitoring logs in real time. Use Ctrl+C to stop.
Q: Why does cat sometimes show numbers or special characters?
That is not cat itself but the terminal interpreting control characters. If the file contains escape sequences or binary data, you may see garbled output. Use cat -v to display non-printing characters in a readable way, or avoid cat on binary files.
Q: Is there a way to see the file size before using cat?
Yes, run ls -lh file or wc -l file to see the line count. If the file has more than a few hundred lines, prefer head or tail.
Q: Can I use these commands on compressed files?
Not directly. Use zcat for gzip files, zless for viewing, or zgrep for searching. For .bz2 files, use bzcat and bzgrep.
Q: What is the difference between ‘cat file’ and ‘cat < file'?
Functionally they are the same for a single file. The first passes the file as an argument; the second redirects standard input. The latter is rarely needed but can be useful when you want to avoid command-line length limits or when the filename starts with a dash (use cat -- -file instead).
Related
General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.
More in this bed
- Custom Alphanumeric Passcode iPhone: How to Set
- HomePod Software Update: How to Install
- How to Shuffle Photos in Slideshow on iPhone
- Quick Look Not Working on Mac? Fixes That Work
- How to See Deleted WhatsApp Messages on iPhone
- QUIC Protocol Error in Windows 10: Fixes