Syntax
head [OPTIONS] [FILE...]
tail [OPTIONS] [FILE...]
Description
The head and tail commands display the first or last portion of text input, respectively. They’re essential for previewing file contents, examining log files, and limiting output in pipelines.
Both commands default to showing 10 lines and can read from files or standard input.
Nash implements these commands entirely in-memory within the virtual filesystem.
head - Display First Lines
Options
Show the first N lines of input.Can also be written as -nN (without space): -n5 or -n 5
Examples
Default (10 lines)
echo -e "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12" | head
Custom line count
echo -e "apple\nbanana\ncherry\ndate" | head -n 2
Compact syntax
echo -e "a\nb\nc\nd\ne" | head -n3
From file
echo -e "line 1\nline 2\nline 3\nline 4\nline 5" > test.txt
head -n 2 test.txt
tail - Display Last Lines
Options
Show the last N lines of input.Can also be written as -nN (without space): -n5 or -n 5
Examples
Default (10 lines)
echo -e "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12" | tail
Custom line count
echo -e "apple\nbanana\ncherry\ndate" | tail -n 2
Compact syntax
echo -e "a\nb\nc\nd\ne" | tail -n3
From file
echo -e "line 1\nline 2\nline 3\nline 4\nline 5" > test.txt
tail -n 2 test.txt
Pipeline Examples
Preview long output
Shows first 5 entries.
Check recent log entries
Shows last 20 log lines.
Top N after sorting
cat scores.txt | sort -rn | head -n 3
Shows top 3 scores.
Bottom N after sorting
cat scores.txt | sort -n | head -n 3
Shows bottom 3 scores (or use tail -n 3 with reverse sort).
Sample data
cat large-dataset.csv | head -n 100 > sample.csv
Creates a 100-row sample.
Preview and analyze
Shows CSV headers.
Skip header and show data
cat data.csv | tail -n +2 | head -n 5
Note: Nash’s tail doesn’t support +N syntax; use other commands to skip lines.
Middle section
cat file.txt | head -n 20 | tail -n 10
Shows lines 11-20.
Practical Use Cases
Check file structure
Preview CSV column headers.
Recent errors
grep ERROR app.log | tail -n 10
Last 10 error messages.
Validate output
command-with-lots-of-output | head -n 5
Quick sanity check of output format.
Create test subset
head -n 1000 large-file.txt > test-data.txt
Monitor log files
View recent access log entries.
Compare beginnings
echo "File 1:"
head -n 3 file1.txt
echo "\nFile 2:"
head -n 3 file2.txt
Find patterns in sections
head -n 1000 data.txt | grep pattern | wc -l
Count pattern matches in first 1000 lines.
cat results.txt | sort -rn | head -n 10
Top 10 results (highest values).
Advanced Examples
echo "=== Header ==="
head -n 3 document.txt
echo "\n=== Footer ==="
tail -n 3 document.txt
Sliding window
# Lines 21-30
cat file.txt | head -n 30 | tail -n 10
Percentage sample
TOTAL=$(cat data.txt | wc -l)
SAMPLE=$((TOTAL / 10))
head -n $SAMPLE data.txt
Takes top 10% of lines.
Last N errors
grep -i error logs.txt | tail -n 5
First occurrence
grep pattern file.txt | head -n 1
Finds first matching line.
Multiple files
head -n 5 file1.txt file2.txt file3.txt
Shows first 5 lines of each file.
Combining head and tail
First N
Last N
Middle section
All but first N
cat numbers.txt | head -n 3
Shows first 3 lines.cat numbers.txt | tail -n 3
Shows last 3 lines.cat numbers.txt | head -n 20 | tail -n 10
Shows lines 11-20.cat numbers.txt | tail -n +4
Note: Nash’s tail doesn’t support +N syntax.
Tips
Default behavior
Both commands default to 10 lines:
head file.txt # First 10 lines
tail file.txt # Last 10 lines
Syntax flexibility
These are equivalent:
head -n 5 file.txt
head -n5 file.txt
File vs stdin
Both work with files or pipes:
head -n 3 data.txt # From file
cat data.txt | head -n 3 # From stdin
Combining with grep
Filter then limit:
grep pattern file.txt | head -n 10
Both commands handle empty input gracefully:
echo -n "" | head # No output
echo -n "" | tail # No output
Line counting edge cases
If input has fewer lines than requested:
echo -e "a\nb" | head -n 10 # Shows 2 lines (all available)
echo -e "a\nb" | tail -n 10 # Shows 2 lines (all available)
Common Patterns
Quick preview
See what’s in a file without opening it fully.
Recent activity
Check latest log entries.
Top N results
sort -rn scores.txt | head -n 5
Leaderboard: top 5 scores.
head -n 100 large-file.txt > sample.txt
Create small test file from large dataset.
Skip and process
tail -n +2 data.csv | head -n 10
Note: Nash’s tail doesn’t support +N syntax; use alternatives.
Memory efficiency: Nash’s head can stop reading once it has N lines, but tail must read all input to find the last N lines. For very long input, tail will use more memory than head.
cat - Display entire files
wc - Count lines
grep - Filter lines
sort - Order lines before head/tail
sed - Extract specific line numbers