Viewing Files
You can navigate a filesystem and create files. Now let's read them.
File Types¶
As written in the intro, file extensions are not interpreted by Linux. To check what a file actually is:
Viewing Files - "Read-Only" Commands¶
These commands are safe to use as they won't change your files.
cat - Print entire file to screen¶
The cat command (short for "concatenate") prints the entire content of a file to your terminal.
Pros: Great for small files. Cons: Floods your screen with long files.
cat multiple files¶
You can combine files and even create new ones:
Useful options¶
| Option | What it does | Example |
|---|---|---|
-n |
Number all lines | cat -n script.py |
-b |
Number non-blank lines only | cat -b config.conf |
less - Scroll through files page by page¶
less is a pager. It shows one page of text at a time. Perfect for long files like logs or configs.
| Key | Action |
|---|---|
Space |
Move down one page |
b |
Move up one page |
↓ ↑ / j k |
Move line by line |
/word |
Search for "word" (press n for next match) |
q |
Quit and return to the prompt |
lessismore(it replaced an older command calledmoreand added more functionalities). That's the joke.
head and tail - View just the beginning or end¶
These are useful for quickly checking files.
Common Options¶
| Option | What it does |
|---|---|
-n N |
Show N lines instead of the default 10 |
-f |
Follow a file in real-time (tail only) |
Tip
tail -f shows new lines as they're added. Press Ctrl+C to stop following. Invaluable for watching live logs.
| Command | Options | What it does |
|---|---|---|
file |
- | Identify file type |
cat |
-n -b |
Print entire file (+ line numbers) |
less |
- | Scroll through file page by page |
head |
-n N |
First N lines (default 10) |
tail |
-n N -f |
Last N lines (default 10), follow live |
less keys: Space (down page), b (up page), /word (search), q (quit)
Check Your Understanding¶
1. Viewing a long file¶
You have a log file with thousands of lines. You want to browse it page by page.
What command do you use?
Reveal Answer
less logfile.txt
2. Checking a config quickly¶
You just want to see the first 5 lines of a config file.
What command do you use?
Reveal Answer
head -n 5 config.conf
3. Watching a log in real-time¶
A program is writing to debug.log and you want to see new lines as they appear.
What command do you use?
Reveal Answer
tail -f debug.log (press Ctrl+C to stop)
4. Identifying a file¶
You have a file called unknown with no extension. You want to know what it actually is.
What command do you use?
Reveal Answer
file unknown