Zsh Tutorial #4: View & Edit Files (cat, less, head, tail, nano) | macOS Terminal Guide
Video: Zsh Tutorial #4: View & Edit Files (cat, less, head, tail, nano) | macOS Terminal Guide by Taught by Celeste AI - AI Coding Coach
Zsh Lesson 4: View and Edit Files — cat, less, head, tail, nano
cat filedumps contents.head -N filefirst N lines,tail -N filelast N.less fileopens a pager (q to quit, / to search).nano filefor a simple terminal editor. Redirection>writes,>>appends.
You can read files without leaving the terminal. Five commands cover almost everything.
cat: dump file contents
echo 'Hello, World!' > greeting.txt
echo 'This is line 2' >> greeting.txt
echo 'This is line 3' >> greeting.txt
cat greeting.txt
# Hello, World!
# This is line 2
# This is line 3
cat path prints the whole file to the terminal. Short for "concatenate" — really designed for joining multiple files:
cat file1.txt file2.txt > combined.txt
For a single file, it's just "dump it."
cat -n adds line numbers:
cat -n greeting.txt
# 1 Hello, World!
# 2 This is line 2
# 3 This is line 3
For huge files, don't cat them — they'll fill your terminal. Use less or head/tail instead.
Redirection: > and >>
echo 'first line' > file.txt # OVERWRITES
echo 'second line' >> file.txt # APPENDS
>— write stdout to file. Truncates the file first.>>— append stdout to file.
# Save command output to a file
ls -la > listing.txt
date > timestamp.txt
# Append to a log
echo "$(date): user logged in" >> activity.log
Lesson 16 covers redirection in depth (stderr, /dev/null, here-docs).
head: first N lines
seq 30 | xargs -I{} echo 'Line {}: Content' > numbers.txt
head numbers.txt
# (first 10 lines — default)
head -5 numbers.txt
# (first 5)
head -N file prints the first N lines. Default N is 10.
For first N bytes:
head -c 100 file.bin
tail: last N lines
tail numbers.txt
# (last 10 lines — default)
tail -5 numbers.txt
# (last 5)
Most useful for logs:
tail /var/log/system.log
For watching a file as it grows (live tail):
tail -f /var/log/system.log
# follows updates — Ctrl-C to stop
-f follows the file. -F is similar but reopens the file if rotated. Essential for log debugging.
head + tail = "lines N-M"
head -15 numbers.txt | tail -5
# (lines 11-15)
The classic Unix idiom: pipe head to tail for a window in the middle of a file.
less: the pager
For files larger than your screen:
less longfile.txt
Opens an interactive pager. Inside less:
- SPACE — next page.
- b — previous page.
- j / k — down/up one line.
- /word then Enter — search forward.
- ?word then Enter — search backward.
- n — next match.
- N — previous match.
- g — top.
- G — bottom.
- q — quit.
Same keys as man. Once you internalize them, navigation is faster than scrolling.
less is preferred over more (an older, less-featured pager). They share most key bindings.
wc: count lines, words, bytes
wc longfile.txt
# 50 150 900 longfile.txt
# (lines, words, characters)
wc -l longfile.txt
# 50 longfile.txt
Quick way to get a file's size in lines.
nano: terminal editor
nano myfile.txt
Opens myfile.txt in nano, a beginner-friendly terminal editor. Inside nano:
- Type — your text.
- Ctrl-O then Enter — write (save).
- Ctrl-X — exit (prompts to save if modified).
- Ctrl-K — cut current line.
- Ctrl-U — paste.
- Ctrl-W — search.
- Ctrl-G — help (full keybindings).
nano shows the most-used shortcuts at the bottom of the screen — ^ means Ctrl. So ^X Exit means "Ctrl-X to exit."
For more powerful editing, learn vim (or nvim) eventually — way more keystrokes, way more leverage. But nano is fine for quick edits.
Other editors
vim file.txt # vim — modal, powerful, steep learning curve
nvim file.txt # neovim — modern vim
code file.txt # VS Code (if installed)
open -a "TextEdit" file.txt # macOS GUI app
For shell scripting, terminal editors (nano, vim, nvim) are best. They keep you in the same window.
Working with file contents
A typical pipeline:
# How many lines start with "ERROR" in a log?
grep "^ERROR" logfile.txt | wc -l
# What are the most recent 20 entries?
tail -20 logfile.txt
# Show me lines 100-110 of a config
head -110 config.yaml | tail -11
# Dump a small file
cat ~/.zshrc
# Browse a long file
less /etc/services
Each command does one thing; pipes connect them.
Binary files
cat and less are for text. For binary (images, executables):
# DON'T do this — corrupts your terminal:
cat photo.jpg
# Use file to inspect type
file photo.jpg
# photo.jpg: JPEG image data, ...
# Use hexdump for raw bytes
hexdump -C photo.jpg | head
If you accidentally cat a binary and get garbage in the terminal, type:
reset
Or close and reopen the terminal.
Common stumbles
cat on a huge file. Fills the terminal, scrolls forever. head -100 or less instead.
> overwrites without warning. command > file always truncates file first. To prevent accidents, set noclobber:
setopt noclobber
echo new > existing.txt # zsh: file exists: existing.txt
echo new >| existing.txt # force-overwrite
head -5 on Linux vs head -n 5. Both work on macOS. The -n 5 form is POSIX; the -5 form is more common in shell.
tail -f on a non-rotating file. Hangs forever. Ctrl-C to stop.
Forgot -r for cat? No — cat doesn't have a recursive option. For "dump every file in a directory," use find:
find . -type f -exec cat {} \;
Editor save doesn't work. Permission denied → file owned by another user. Use sudo nano file.
less doesn't quit. Press q. If stuck, Ctrl-C then q.
What's next
Lesson 5: customizing Zsh. Aliases, env vars, PROMPT, .zshrc.
Recap
cat file dumps; head -N first N lines, tail -N last N; head | tail for middle. less file pages (q quits, / searches). nano file for simple editing (Ctrl-O save, Ctrl-X exit). Redirection: > writes (truncates), >> appends. wc -l file for line count. For huge files, never cat — use less or head/tail.
Next lesson: customizing Zsh.