File Content Viewing (cat, less, head, tail)
Category: Linux Command Basics
Type: Linux Commands
Generated on: 2025-07-10 03:05:21
For: System Administration, Development & Technical Interviews
File Content Viewing Cheatsheet (cat, less, head, tail)
Section titled “File Content Viewing Cheatsheet (cat, less, head, tail)”This cheatsheet covers essential Linux commands for viewing file content: cat, less, head, and tail. It provides practical examples, common options, and advanced usage scenarios for system administrators and developers.
1. Command Overview
Section titled “1. Command Overview”-
cat(concatenate): Displays the entire file content to standard output. Useful for small files or when piping to other commands. -
less: A pager program that allows you to view large files one screen at a time. Offers navigation, searching, and other features. Preferred for viewing large files as it doesn’t load the whole file into memory at once. -
head: Displays the first n lines of a file. Useful for quickly checking the file header or first few lines of a log file. -
tail: Displays the last n lines of a file. Most commonly used for monitoring log files in real-time.
2. Basic Syntax
Section titled “2. Basic Syntax”cat [OPTIONS] [FILE]...less [OPTIONS] [FILE]...head [OPTIONS] [FILE]...tail [OPTIONS] [FILE]...3. Practical Examples
Section titled “3. Practical Examples”3.1. cat
-
Display the content of a file:
Terminal window cat myfile.txtSample Output (myfile.txt):
This is the first line.This is the second line.This is the third line. -
Concatenate multiple files:
Terminal window cat file1.txt file2.txt file3.txtThis will print the contents of
file1.txt,file2.txt, andfile3.txtsequentially to the terminal. -
Create a new file by concatenating existing files:
Terminal window cat file1.txt file2.txt > newfile.txtWARNING: This will overwrite
newfile.txtif it already exists. Use>>to append instead (see below). -
Append to an existing file:
Terminal window cat file1.txt >> existingfile.txtThis appends the content of
file1.txtto the end ofexistingfile.txt.
3.2. less
-
View a file with
less:Terminal window less largefile.logNavigation:
Spacebar: Scroll down one page.b: Scroll up one page.j: Scroll down one line.k: Scroll up one line.g: Go to the beginning of the file.G: Go to the end of the file./pattern: Search forpattern(pressnfor next,Nfor previous).q: Quitless.
-
View output from a command with
less:Terminal window ls -l /etc | less
3.3. head
-
Display the first 10 lines of a file:
Terminal window head myfile.txtSample Output (myfile.txt - first 3 lines):
This is the first line.This is the second line.This is the third line. -
Display the first 5 lines of a file:
Terminal window head -n 5 myfile.txthead -5 myfile.txt # Shorthand -
Display the first few bytes of a file (rarely used):
Terminal window head -c 10 myfile.txt # Display the first 10 bytes
3.4. tail
-
Display the last 10 lines of a file:
Terminal window tail myfile.txtSample Output (myfile.txt - last 3 lines):
This is the last third line.This is the last second line.This is the last line. -
Display the last 5 lines of a file:
Terminal window tail -n 5 myfile.txttail -5 myfile.txt # Shorthand -
Follow a log file in real-time (most common use):
Terminal window tail -f /var/log/syslogThis will continuously display new lines added to the
/var/log/syslogfile. PressCtrl+Cto stop following. -
Follow a log file and display more context:
Terminal window tail -n 20 -f /var/log/nginx/access.logThis will display the last 20 lines and then follow the log file in real-time.
-
Multiple files with tail:
Terminal window tail -f file1.log file2.logThis will follow both file1.log and file2.log, prefixing each line with the filename.
4. Common Options
Section titled “4. Common Options”| Command | Option | Description | Example |
|---|---|---|---|
cat | -n | Number all output lines. | cat -n myfile.txt |
cat | -b | Number non-blank output lines. | cat -b myfile.txt |
cat | -s | Suppress repeated empty output lines. | cat -s myfile.txt |
less | -N | Display line numbers. | less -N myfile.txt |
less | -S | Chop long lines instead of wrapping. | less -S myfile.txt |
less | +/pattern | Start less at the first occurrence of pattern. | less +/error logfile.txt |
head | -n / - | Number of lines to display. Default is 10. | head -n 20 myfile.txt or head -20 myfile.txt |
head | -c | Number of bytes to display. | head -c 50 myfile.txt |
tail | -n / - | Number of lines to display. Default is 10. | tail -n 20 myfile.txt or tail -20 myfile.txt |
tail | -f | Follow the file as new content is added. | tail -f /var/log/syslog |
tail | -F | Follow the file by name, even if it’s rotated. More robust than -f. | tail -F /var/log/nginx/access.log |
tail | +NUM | Output beginning with line NUM, not from the end. | tail +5 myfile.txt (lines 5 to end) |
5. Advanced Usage
Section titled “5. Advanced Usage”-
Using
catwithgrepto find specific lines:Terminal window cat myfile.txt | grep "error"This displays only the lines in
myfile.txtthat contain the word “error”. -
Using
tailwithgrepto monitor log files for specific events:Terminal window tail -f /var/log/nginx/error.log | grep "php"This monitors the Nginx error log and displays only lines containing “php”.
-
Combining
headandtailto extract a specific range of lines:Terminal window head -n 100 myfile.txt | tail -n 10This extracts lines 91 to 100 from
myfile.txt. First,headgets the first 100 lines, thentailgets the last 10 of those 100. -
Using
lessto search for multiple patterns:Terminal window less myfile.txt/pattern1n # Find next occurrence of pattern1/pattern2n # Find next occurrence of pattern2 -
Following multiple log files with
tailand usinggrep:Terminal window tail -f /var/log/apache2/access.log /var/log/apache2/error.log | grep -E 'error|warning'This command follows both Apache log files and uses
grepwith extended regular expressions (-E) to filter for lines containing either “error” or “warning”. This is very useful for real-time monitoring of potential issues. -
Filtering out unwanted data with
grep -v:Terminal window tail -f /var/log/nginx/access.log | grep -v "favicon.ico"This follows the Nginx access log but filters out lines containing “favicon.ico”, which are often irrelevant and clutter the output.
6. Tips & Tricks
Section titled “6. Tips & Tricks”-
Use
lessfor viewing any file larger than a few hundred lines. It’s more efficient thancat. -
Use
tail -Finstead oftail -ffor log files that are rotated.tail -Fwill continue to follow the log even after it’s been renamed or moved. -
Remember to use
Ctrl+Cto exittail -forless. -
Use aliases to shorten frequently used commands:
Terminal window alias tlf='tail -F' # tail -Falias l='less -N -S' # less with line numbers and chop long linesAdd these lines to your
.bashrcor.zshrcfile to make them permanent. -
Check file size before using
cat:Terminal window ls -l myfile.logIf the file is very large (e.g., gigabytes), avoid using
cat. Uselessorhead/tailinstead. -
Use
set +mto prevent background jobs from printing to the terminal when usingtail -fand disconnecting from SSH. This is useful when monitoring logs on a remote server and disconnecting without stopping the tail command.
7. Troubleshooting
Section titled “7. Troubleshooting”catdisplays binary data: The file is likely a binary file, not a text file. Use a hex editor (e.g.,hexdump,xxd) to view the binary data.tail -fdoesn’t show new lines:- The application may not be writing to the log file, or the log file may be buffered.
- Try flushing the buffers of the application writing to the log file.
- Ensure you have permissions to read the file.
lessdisplays strange characters: The file may have an unsupported character encoding. Try specifying the encoding with the-roption (e.g.,less -r myfile.txt).- Permission denied: Ensure you have read permissions for the file. Use
ls -lto check permissions. tail -Fstops working after log rotation: The application rotating the logs may not be using the correct method. Ensure it’s either usingcopytruncateor properly signaling the process to reopen the log file.
8. Related Commands
Section titled “8. Related Commands”grep: Search for patterns within files.wc: Count lines, words, and characters in a file.find: Search for files based on various criteria.sed: Stream editor for text manipulation.awk: Pattern scanning and processing language.echo: Display a line of text. Useful for creating small files.tee: Read from standard input and write to standard output and files. Useful for both displaying and saving output. Example:cat myfile.txt | tee output.txtjournalctl: View and manage systemd journal logs (for systemd-based systems).
This cheatsheet provides a comprehensive overview of the cat, less, head, and tail commands, covering both basic and advanced usage scenarios. Remember to adapt the examples to your specific needs and always be cautious when using commands that can modify files.