04 - Reading Files¶
What this session is¶
About 30 minutes. You'll learn to read text files in the terminal - without opening an editor.
cat: show a whole file¶
cat (short for "concatenate") prints a file's contents to your terminal. For small files, perfect. For large ones, it scrolls off the top - useful tools for that below.
You can cat multiple files at once:
For files with binary content (images, executables), cat produces gibberish. Stick to text.
less: page through a file¶
For files too big to view all at once:
Inside less:
- Space - next page.
- b - previous page.
- Arrow keys - line by line.
- /pattern - search forward.
- ?pattern - search backward.
- n / N - next/previous match.
- g - top.
- G - bottom.
- q - quit.
less is the file viewer in Linux. Use it for anything over ~50 lines.
(Memory note: there's also more, which is older and inferior. Use less.)
head and tail: first/last N lines¶
head file.txt # first 10 lines
head -n 5 file.txt # first 5 lines
tail file.txt # last 10 lines
tail -n 20 file.txt # last 20 lines
tail -f file.log # follow - print new lines as they appear (great for log files)
tail -f is one of the most-used commands for watching log files in real time. Ctrl-C to stop.
wc: count lines/words/bytes¶
wc file.txt # lines, words, bytes
wc -l file.txt # just lines
wc -w file.txt # just words
wc -c file.txt # just bytes
Useful in combinations: "how many lines in this log?" wc -l app.log. We'll see it piped with other commands in page 07.
file: what type is this?¶
Tells you what kind of file it is - ASCII text, PNG image, ELF 64-bit LSB executable, etc. Useful when you've downloaded something and aren't sure what you got.
xxd / hexdump: see bytes¶
For looking at binary files:
Shows hex + ASCII side by side. Rarely needed at this level; mentioned for recognition.
A note on encoding¶
Most text files are UTF-8. Occasionally you'll meet files with different encodings (Windows-1252, etc.) that look corrupted in your terminal. Use iconv to convert, or set LANG / LC_ALL for your shell session. Beyond beginner scope.
Exercise¶
-
Open a long config file with
Scroll, search (typeless:/tcpand press Enter), navigate with arrows, quit withq. -
Show the last 20 lines of your bash history:
-
Count the lines in
/etc/services: -
What kind of file is
/usr/bin/ls? -
Show the first 5 lines of
What's in that file? (Each line is a user account.)/etc/passwd: -
Bonus: if you have a system log accessible (
/var/log/syslogon Debian/Ubuntu,/var/log/system.logon macOS), follow it withtail -f. Watch it for a minute (sometimes you'll see new log lines appear). Ctrl-C to stop.
What you might wonder¶
"Why use less instead of an editor?"
- Faster - opens instantly for huge files.
- Doesn't accidentally modify anything.
- Available on every Linux/Unix system.
You'll meet less constantly in real work - man (page 05) uses it; git log uses it; many tools pipe through it.
"What's the difference between tail -f and just tail?"
Plain tail shows the last N lines and exits. tail -f keeps the terminal open and shows new lines as they're added. Use -f for live monitoring.
"cat file | less vs less file - which?"
less file directly. The piped form works but creates a useless extra process. However, command | less is common when paging through any command's output that's too long.
Done¶
- Show files with
cat(small) orless(any size). - Navigate
less(Space, /, q). - Grab first/last lines with
headandtail. - Follow logs with
tail -f. - Count with
wc. - Identify file types with
file.