LFCS 13 š§ Viewing Files ā cat, less, head, tail
A file has content. The content can be smallāa configuration line, a hostnameāor largeāa log file with millions of entries, a kernel source tree. The tools for reading the content are the ones that differ by the size and the purpose. The cat is for the small files and the concatenation. The less is for the large files and the navigation. The head and the tail are for the beginning and the end, and the tail -f is for the live logs. The LFCS exam expects these tools to be automatic, and the exam tasks often require reading the configuration, the logs, and the output.
Key point: cat file prints the whole file to the standard output. less file opens the file in a pager with the navigation. head file prints the first 10 lines. tail file prints the last 10 lines. tail -f file follows the file as it grows, which is the tool for the live logs. cat is for the small files and the concatenation; less is for the large files; head and tail are for the specific parts.
Why the viewing tools matter
The size problem. A cat of a 10 GB log file floods the terminal and consumes the memory. The less reads the file incrementally and shows one screen at a time. The tool should match the size.
The navigation problem. The cat prints the whole file and returns to the prompt. There is no way to scroll back with the keyboard in most terminals, and the search is impossible. The less provides the scroll, the search, and the navigation. The less is the tool for the exploration.
The specific-part problem. The configuration is often at the top of the file, and the errors are often at the bottom. The head and the tail show the specific part without the whole file. The tail -f is the live view of the file as it grows.
The concatenation problem. The cat concatenates the multiple files into the standard output. The cat file1 file2 > combined combines the files. The cat is the tool for the combination, and the less is the tool for the reading.
The pipeline problem. The cat is used in the pipelines, though the cat file | command is often redundant with the command < file. The cat is for the cases where the file is the argument and the output is the pipe. The cat is the tool for the simple cases, and the dedicated tools are for the complex.
a. The cat command
The cat prints the file to the standard output.
$ cat file.txt
The whole content is printed. The command returns to the prompt. The output can be piped to another command.
$ cat file.txt | grep "error"
The cat prints the file, and the grep filters the lines. The same result with the grep "error" file.txt is shorter and avoids the extra process.
The multiple files are concatenated.
$ cat file1.txt file2.txt file3.txt
The three files are printed in order. The output is the concatenation.
The -n option numbers the lines.
$ cat -n file.txt
1 first line
2 second line
3 third line
The -b option numbers the non-empty lines only.
$ cat -b file.txt
1 first line
2 second line
The -s option squeezes the multiple blank lines into one.
$ cat -s file.txt
The -A option shows the non-printing characters.
$ cat -A file.txt
line1$
line2$
line3$
The $ is the end of the line. The -A is the equivalent of the -vET. The option is for the files that may have the hidden characters, the carriage returns, or the tabs.
The -E option shows the $ at the end of each line. The -T option shows the tabs as ^I. The -v option shows the non-printing characters.
$ cat -E file.txt
line1$
line2$
The cat with the redirection creates the file.
$ cat > newfile.txt
This is the content.
Press Ctrl+D to end.
The cat reads the standard input and writes it to the file. The Ctrl+D is the end of the input.
The cat with the append.
$ cat >> newfile.txt
More content.
The >> appends the content to the file. The > overwrites.
The cat with the here-document.
$ cat << EOF > config.txt
line1
line2
EOF
The << is the here-document. The EOF is the delimiter. The lines between the cat and the EOF are the content. The pattern is for the scripted file creation.
The tac command is the cat reversed.
$ tac file.txt
The lines are printed in the reverse order. The tac is the tool for the log files where the last entry is the most recent.
b. The less command and the pager
The less opens the file in a pager.
$ less file.txt
The file is displayed one screen at a time. The navigation keys are the same as the man pages.
| Key | Action |
|---|---|
Space | Next page |
b | Previous page |
j | Next line |
k | Previous line |
G | Go to end |
g | Go to beginning |
/pattern | Search forward |
?pattern | Search backward |
n | Next match |
N | Previous match |
q | Quit |
The less is the tool for the large files. The cat of the large file is the mistake.
The less with the -N option shows the line numbers.
$ less -N file.txt
The less with the -S option disables the line wrapping. The long lines are truncated, and the horizontal scroll is available with the arrow keys.
$ less -S file.txt
The less with the -F option quits automatically if the file fits on one screen.
$ less -F file.txt
The -F is useful in the scripts where the less should behave like the cat for the small files and the pager for the large.
The less with the + option opens at the specific line.
$ less +100 file.txt
The file opens at the line 100. The +G opens at the end. The +/pattern opens at the first match.
$ less +G file.txt
$ less +/error file.txt
The less is the more replacement. The more is the older pager with fewer features. The less is the standard.
The less with the input from the pipe.
$ command | less
The output of the command is paged. The pattern is for the long output that should not flood the terminal.
The less with the multiple files.
$ less file1.txt file2.txt
The files are opened in sequence. The :n goes to the next file, and the :p goes to the previous.
c. The head and tail commands
The head prints the first 10 lines.
$ head file.txt
The -n option specifies the number of lines.
$ head -n 20 file.txt
$ head -20 file.txt
The -c option specifies the number of bytes.
$ head -c 100 file.txt
The first 100 bytes are printed.
The tail prints the last 10 lines.
$ tail file.txt
The -n option specifies the number of lines.
$ tail -n 20 file.txt
$ tail -20 file.txt
The -c option specifies the number of bytes.
$ tail -c 100 file.txt
The -f option follows the file as it grows.
$ tail -f /var/log/syslog
The command prints the last 10 lines and then waits for the new lines. The new lines are printed as they are written. The Ctrl+C stops the follow.
The tail -f is the tool for the live logs. The tail -F is the same but retries if the file is rotated.
$ tail -F /var/log/syslog
The -F is the --follow=name --retry. The option handles the log rotation: when the file is renamed and the new file is created, the -F follows the new file. The -f follows the file descriptor, and the rotation breaks it.
The tail with the -n +N starts at the line N.
$ tail -n +2 file.txt
The output starts at the line 2 and continues to the end. The +2 is the “start at the line 2” syntax. The pattern is for the files with the header line that should be skipped.
The tail -f with the multiple files.
$ tail -f file1.log file2.log
The output is prefixed with the file names. The pattern is for the multiple logs that should be monitored together.
The head and the tail combined for the middle.
$ head -n 20 file.txt | tail -n 10
The first 20 lines are piped to the tail, which prints the last 10 of those. The result is the lines 11-20. The pattern is for the specific range without the sed.
The sed alternative.
$ sed -n '11,20p' file.txt
The sed prints the lines 11-20. The sed is the tool for the arbitrary range, and the head | tail is the combination for the simple case.
The tail with the -f and the grep.
$ tail -f /var/log/syslog | grep "error"
The live log is filtered for the “error” lines. The grep buffers the output, and the --line-buffered option makes it print the lines as they arrive.
$ tail -f /var/log/syslog | grep --line-buffered "error"
The --line-buffered is the option that makes the grep flush the output on each line. Without it, the output is buffered and the lines are printed in the blocks.
Complete Example Session
# ============================================
# PART 1: BASIC CAT
# ============================================
cat file.txt
# ============================================
# PART 2: CAT WITH NUMBERS
# ============================================
cat -n file.txt
cat -b file.txt
# ============================================
# PART 3: CAT WITH NON-PRINTING
# ============================================
cat -A file.txt
# ============================================
# PART 4: CAT TO CREATE
# ============================================
cat > newfile.txt
# Type the content, Ctrl+D to end
# ============================================
# PART 5: HERE-DOCUMENT
# ============================================
cat << EOF > config.txt
line1
line2
EOF
# ============================================
# PART 6: LESS
# ============================================
less file.txt
# Space, b, /pattern, n, q
# ============================================
# PART 7: LESS WITH OPTIONS
# ============================================
less -N file.txt
less +G file.txt
less +/error file.txt
# ============================================
# PART 8: HEAD
# ============================================
head file.txt
head -n 20 file.txt
head -c 100 file.txt
# ============================================
# PART 9: TAIL
# ============================================
tail file.txt
tail -n 20 file.txt
tail -n +2 file.txt
# ============================================
# PART 10: TAIL -F AND GREP
# ============================================
tail -f /var/log/syslog
tail -F /var/log/syslog
tail -f /var/log/syslog | grep --line-buffered "error"
The ten parts covered the basic cat, the numbers, the non-printing, the create, the here-document, the less, the less options, the head, the tail, and the tail -f with the grep.
Quick Reference
cat Options
| Option | Effect |
|---|---|
cat file | Print the file |
cat -n | Number all lines |
cat -b | Number non-empty |
cat -s | Squeeze blank lines |
cat -A | Show non-printing |
cat -E | Show $ at end |
cat -T | Show tabs as ^I |
less Keys
| Key | Action |
|---|---|
Space | Next page |
b | Previous page |
j/k | Next/previous line |
G/g | End/beginning |
/pattern | Search forward |
n/N | Next/previous match |
q | Quit |
head and tail
| Command | Effect |
|---|---|
head file | First 10 lines |
head -n 20 | First 20 lines |
head -c 100 | First 100 bytes |
tail file | Last 10 lines |
tail -n 20 | Last 20 lines |
tail -n +2 | From the line 2 |
tail -f file | Follow |
tail -F file | Follow with retry |
Common Patterns
| Pattern | Purpose |
|---|---|
cat file1 file2 > combined | Concatenate |
cat << EOF > file | Here-document |
command | less | Page the output |
tail -f log | grep pattern | Live filter |
head -20 file | tail -10 | Lines 11-20 |
sed -n '11,20p' file | Lines 11-20 |
Best Practices
ā Do This:
# Use less for the large files
less /var/log/syslog # ā
# Use tail -f for the live logs
tail -f /var/log/syslog # ā
# Use tail -F for the rotated logs
tail -F /var/log/syslog # ā
# Use head for the first lines
head -n 20 file.txt # ā
# Use the here-document for the scripted file creation
cat << EOF > config.txt
line1
EOF
# ā
# Use the line-buffered grep with the tail -f
tail -f log | grep --line-buffered "error" # ā
# Use the sed for the arbitrary range
sed -n '11,20p' file.txt # ā
ā Don’t Do This:
# Don't cat the large files
cat /var/log/syslog # floods the terminal # ā
# Don't forget the -F for the rotated logs
tail -f /var/log/syslog # breaks on rotation # ā ļø
# Don't use the unquoted patterns
grep $pattern file # word splitting # ā ļø
# Don't use the cat in the pipe when the command accepts the file
cat file | grep "x" # use: grep "x" file # ā ļø
# Don't use the head | tail for the complex ranges
head -100 file | tail -50 # use: sed -n '51,100p' # ā ļø
# Don't forget the line-buffered grep with the tail -f
tail -f log | grep "error" # buffered, no output # ā ļø
# Don't cat the binary files
cat /bin/ls # garbage output # ā
Common Pitfalls
| Pitfall | Why It Happens | Fix |
|---|---|---|
| Terminal flooded | cat on the large file | Use less |
| Log rotation breaks follow | -f follows the descriptor | Use -F |
| Grep output buffered | No --line-buffered | Add the option |
| Binary garbage | cat on the binary | Use less or xxd |
| Wrong range | head | tail off-by-one | Use sed |
| Blank lines | cat -s missing | Add the option |
Real-World Examples
1. Read a Config
cat /etc/hostname
2. View a Log
less /var/log/syslog
3. First Lines
head -n 20 /etc/passwd
4. Last Lines
tail -n 20 /var/log/syslog
5. Follow a Log
tail -f /var/log/syslog
6. Follow with Retry
tail -F /var/log/syslog
7. Live Filter
tail -f /var/log/syslog | grep --line-buffered "error"
8. Concatenate
cat part1.txt part2.txt > combined.txt
9. Here-document
cat << EOF > config.txt
server=localhost
port=8080
EOF
10. Skip the Header
tail -n +2 data.csv
Visual
Tool Selection
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
ā SMALL FILE (< 1 screen) ā
ā cat file.txt ā
ā ā
ā LARGE FILE ā
ā less file.txt ā
ā ā
ā FIRST LINES ā
ā head file.txt ā
ā ā
ā LAST LINES ā
ā tail file.txt ā
ā ā
ā LIVE LOG ā
ā tail -f file.txt ā
ā ā
ā The tool matches the size and the purpose. ā
ā ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
tail -f vs tail -F
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
ā tail -f ā
ā ā
ā Follows the file descriptor. ā
ā Log rotation: the file is renamed, the new file is created.ā
ā The -f keeps the old descriptor, and the new file is ā
ā ignored. ā
ā ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¤
ā ā
ā tail -F ā
ā ā
ā The --follow=name --retry. ā
ā Log rotation: the file is renamed, the new file is created.ā
ā The -F detects the new file and follows it. ā
ā ā
ā The -F is the tool for the rotated logs. ā
ā ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
head and tail Ranges
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
ā head -n 20 file.txt ā
ā ā Lines 1-20 ā
ā ā
ā tail -n 20 file.txt ā
ā ā Lines N-19 to N ā
ā ā
ā tail -n +2 file.txt ā
ā ā Lines 2 to N ā
ā ā
ā head -20 file.txt | tail -10 ā
ā ā Lines 11-20 ā
ā ā
ā sed -n '11,20p' file.txt ā
ā ā Lines 11-20 (cleaner) ā
ā ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
Live Log Filter
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
ā tail -f /var/log/syslog | grep "error" ā
ā ā
ā The tail writes the lines. ā
ā The grep buffers the output. ā
ā The lines appear in the blocks, not the live. ā
ā ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¤
ā ā
ā tail -f /var/log/syslog | grep --line-buffered "error" ā
ā ā
ā The tail writes the lines. ā
ā The grep flushes on each line. ā
ā The lines appear live. ā
ā ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
Summary
| Item | Value |
|---|---|
| cat | Print the whole file |
| less | Pager |
| head | First 10 lines |
| tail | Last 10 lines |
tail -f | Follow the file |
tail -F | Follow with retry |
head -n N | First N lines |
tail -n N | Last N lines |
tail -n +N | From the line N |
cat -n | Number the lines |
less +G | Open at the end |
grep --line-buffered | Live filter |
Key takeaways:
- The
catis for the small files and the concatenation. Thecat file1 file2 > combinedcombines the files. Thecaton the large file floods the terminal. - The
lessis the pager. The navigation, the search, and the horizontal scroll. Thelessis the tool for the large files and the exploration. - The
headand thetailshow the specific part. Theheadfor the beginning and thetailfor the end. Thetail -n +Nstarts at the line N. Thehead | tailcombination is for the simple range, and thesedis for the clean range. - The
tail -ffollows the file, and thetail -Fhandles the rotation. The-ffollows the file descriptor. The-Ffollows the file name and retries. The-Fis the tool for the rotated logs. - The
grep --line-bufferedis the live filter. Without the option, thegrepbuffers the output, and the lines appear in the blocks. The--line-bufferedflushes on each line. - The
lessis themanpager. The navigation keys are the same. The/patternsearch, thennext, and theqquit. Thelessis the tool for the exploration. - The
catwith the here-document is the scripted file creation. Thecat << EOF > filecreates the file with the content between thecatand theEOF. The pattern is for the scripts and the provisioning.
Remember: The tool matches the size and the purpose. The cat for the small files, the less for the large, the head and the tail for the specific parts. The tail -F for the rotated logs, and the grep --line-buffered for the live filter. The cat is the simple tool, and the less is the exploration. The LFCS tasks require the reading of the configuration, the logs, and the output, and the choice of the tool is the detail that makes the reading efficient.
Stop using slow, ad-bloated tool sites! š¤®
š Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
ā
Finance (Mortgage, Interest, Inflation)
ā
Tech (Base64, JSON, Dev Suite, IP)
ā
Health (BMI, BMR, TDEE)
ā
Productivity (Timer, Workspace, QR)
ā”ļø Fast & Private
š No data leaves your device
š 100% Free
š Use it now: https://tools.kandz.me
š Bookmark itāyouāll need it later!