Why inspect files from the terminal?
Many tasks on Linux involve quickly checking what’s inside a file: confirming a configuration value, scanning a log, or counting how many lines a report contains. Terminal tools let you do this fast, even over SSH, without opening a graphical editor.
cat: print small files quickly
cat outputs a file’s contents to the terminal. It’s best for small files because it prints everything at once.
Basic usage
cat notes.txtShow line numbers (useful for referencing)
cat -n notes.txtCombine files into one stream
You can view multiple files in sequence:
cat part1.txt part2.txtBecause cat writes to standard output, you can also feed its output into other commands later (for example, counting lines with wc).
less: page through files safely (search, jump, quit)
less is a pager: it lets you view large files one screen at a time without dumping everything to the terminal. It’s a default choice for logs and long text files.
Continue in our app.
You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.
Or continue reading below...Download the app
Open a file in less
less /var/log/syslogEssential navigation keys inside less
- Move down:
Space(page),Enter(line), or arrow keys - Move up:
b(page up) or arrow keys - Quit:
q
Search within less
Search forward for a word:
/errorThen jump between matches:
- Next match:
n - Previous match:
N
Jump to a location
- Go to start of file:
g - Go to end of file:
G - Go to a specific line number: type the number then
g(example:120g)
Tip: open at the end (handy for logs)
less +G /var/log/syslogThis opens the file positioned at the end, so you can immediately see the most recent entries.
head and tail: preview the beginning or end
head shows the first lines of a file; tail shows the last lines. These are perfect for quick inspection without opening a pager.
Default behavior (first/last 10 lines)
head report.txt
tail report.txtChoose how many lines with -n
head -n 20 report.txt
tail -n 20 report.txtFollow a growing file with tail -f
tail -f keeps running and prints new lines as they are appended to the file (common for live log monitoring).
tail -f /var/log/syslogStop following with Ctrl+C.
Follow and start with the last N lines
This is a common pattern: show the last 50 lines, then keep following.
tail -n 50 -f /var/log/syslogwc: count lines, words, and bytes
wc (word count) summarizes file size in terms of lines, words, and bytes. It’s useful for quick metrics and sanity checks.
Count lines, words, and bytes
wc notes.txtThe output is typically: lines words bytes filename.
Count only lines
wc -l notes.txtCount only words
wc -w notes.txtCount only bytes
wc -c notes.txtCount multiple files at once
wc -l chapter1.txt chapter2.txtwc will also print a total line when given multiple files.
file: detect what kind of file you’re looking at
File extensions are not always reliable. The file command inspects a file’s contents and reports its type (text, image, compressed archive, executable, etc.). This helps you choose the right tool to open or process it.
Identify a file type
file document.txt
file archive.tar.gz
file /bin/lsCommon results you might see
ASCII textorUTF-8 Unicode textfor plain text filesgzip compressed datafor gzipped filesELF 64-bit LSB executablefor Linux binaries
Mini-lab: inspect a log file, follow new entries, and summarize counts
This mini-lab practices quick inspection patterns you’ll use constantly. Use a log file that exists on your system. Common choices include /var/log/syslog (Debian/Ubuntu) or /var/log/messages (some other distributions). If one doesn’t exist, try another.
1) Pick a log file and confirm its type
file /var/log/syslogIf that path doesn’t exist, try:
file /var/log/messages2) Preview the log quickly
Look at the beginning and end to understand the format and recency.
head -n 15 /var/log/syslog
tail -n 15 /var/log/syslog3) Page through the log and search for a keyword
Open in less, then search for something common like error, fail, or a service name.
less /var/log/syslog- Inside
less, type/errorthen pressEnter - Press
nto jump to the next match - Press
Gto jump to the end - Press
qto quit
4) Follow new log entries live
In one terminal, start following the log:
tail -n 20 -f /var/log/syslogLeave it running for a moment. If the system is quiet, you may not see new lines immediately. When you’re done, stop with Ctrl+C.
5) Summarize line and word counts for selected files
Choose a few text files you want to measure (for example, a couple of logs or text documents). Then run wc to compare them.
wc -l /var/log/syslog /var/log/auth.log
wc -w /var/log/syslog /var/log/auth.logIf /var/log/auth.log doesn’t exist on your system, substitute another file you have access to (for example, /var/log/messages).
6) Quick check: use cat only when it’s small
Pick a small config or note file and view it with cat. If the output scrolls too fast, switch to less next time.
cat smallfile.txt