Viewing Files
You don't always need a text editor to see what's in a file. In fact, most of the time you just want a quick look.
cat - Concatenate
The simplest file viewer:
cat dumps the entire file to the screen.
Multiple Files
That's actually why it's called "concatenate" - it joins files together.
With Line Numbers
When cat Fails
cat is bad for large files - it dumps everything at once. If you cat a 10GB log file, you'll regret it.
less - Paginated Viewing
For larger files, use less:
Navigation in less:
- Space / Page Down - next page
- b / Page Up - previous page
- g - go to beginning
- G - go to end
- / + text - search forward
- ? + text - search backward
- n - next search result
- N - previous search result
- q - quit
Less is More
There's also a more command, but less is better (yes, "less is more"). less can scroll backwards; more can't.
head - First Lines
See just the beginning of a file:
Default is 10 lines. Use -n to specify how many.
tail - Last Lines
See just the end:
tail -f (Follow)
This is gold for watching logs in real-time:
New log entries appear as they're written. Press Ctrl+C to stop.
Debugging in Production
When something's broken and you're watching logs:
tail -f /var/log/nginx/error.log
Then trigger the error in another window and watch what happens.
Combining head and tail
Get lines from the middle:
Quick Comparison
| Command | Best For |
|---|---|
cat | Small files, quick dumps |
less | Large files, navigation needed |
head | Beginning of files |
tail | End of files |
tail -f | Watching live logs |
Modern Alternative: bat
bat is like cat but with syntax highlighting, line numbers, and git integration:
sudo apt install bat # Then use: batcat (or bat on other distros)
Once you try bat, plain cat feels primitive.
Viewing Binary Files
What happens if you cat a binary file?
Your terminal might break. Reset it with:
For binary files, use xxd or hexdump:
Which command would you use to watch a log file update in real-time?
Key Takeaways
catfor small files, quick viewinglessfor large files with navigationhead -n Xfor first X linestail -n Xfor last X linestail -ffor live log monitoring- Don't
catbinary files - it corrupts your terminal
Next: identifying file types.