As a Linux power user, your terminal skills revolve around working with files. Configuration files, system logs, scripts – they make up the lifeblood of your Linux OS.
Knowing how to efficiently view, parse, and manipulate files through the command line is essential for administering systems and unlocking the full potential of Linux.
In this jam-packed guide, you’ll learn the six file viewing commands every Linux guru needs to know:
- cat – Print files to the terminal
- nl – Number lines of files
- more – View files page-by-page
- less – Scroll through files
- head – See start of files
- tail – See end of files
These versatile commands allow inspecting even the longest log and configuration files with ease.
I’ll explain the key options of each tool, demonstrate practical examples, and share expert tips for wielding files like a Linux virtuoso. Let‘s get viewing!
Why File Viewing is Crucial in Linux
Before jumping into the commands, it helps to understand why peering into files is so fundamental for Linux administration.
See, Linux stores system configurations, executed programs, network services, user accounts and more as plain text files rather than burying settings in hard-to-access databases. This gives you direct control over every aspect through file edits.
But to configure, optimize, debug, and secure your system, you need to see what’s happening inside those files!
Common reasons to view files include:
- Understanding system operations: Configuration files dictate functionality
- Troubleshooting crashes/errors: Log files record what went wrong
- Monitoring real-time activity: Watch logs like Apache access logs
- Writing/testing scripts: Validate code works before executing
- Granting permissions to users: Set file/folder access correctly
Without cat, less, grep, and other file viewers, administering Linux would be flying blind.
Now let’s uncover the six that every Linux professional needs to know…
cat – Print Files to Terminal
The cat
(concatenate) command prints the contents of files directly to the terminal. This offers a quick peek for short text configuration and logs.
cat By Example
cat
is as simple as supplying the target file:
$ cat grocery-list.txt
eggs
milk
cheese
apples
It happily prints multiple files sequentially:
$ cat todo-list.txt grocery-list.txt
Finish Linux blog post
Clean gutters
Buy milk
Buy eggs
Or accepts input redirected from other commands:
$ ps aux | grep mongod | cat
mongod 0.1 4.6 285480 90332 ? Ssl Oct27 0:35 mongod
With no arguments, cat
even echos stdin to stdout itself:
$ echo "foo bar" | cat
foo bar
This makes cat
a flexible file printing Swiss army knife.
Key cat Options
While known for its simplicity, cat
has useful options including:
-n
– Number all output lines-s
– Suppress duplicate consecutive newlines-E
– Display$
at end of each line
Manually numbering lines helps when referencing specific logs.
See man cat
for all options.
When Should I Use cat?
cat
shines when dealing with short, simple configuration files and plaintext you want to directly print to the terminal.
It‘s not ideal for long, complex logs since it lacks easy scrolling. For those, keep reading!
less – Scroll Through Files
The less
command opens a text file viewer that allows scrolling up and down through contents. This is perfect for navigating longer, complex logs and configurations.
less
builds on similar paging commands like more
by adding more (pun intended) functionality.
Scrolling Through Files with less
Invoke less
by passing a filename:
$ less /var/log/syslog
Now you can navigate through contents with:
- Up/Down Arrows – Scroll line-by-line
- Page Up/Down – Scroll page-by-page
- G – Jump to end of file
- q – Quit back to prompt
For example, to inspect the end of syslog
:
(press G)
Jump to end
<scroll up to find error>
(press q to quit)
This beats old-school scrolling in cat
!
Search Files with less
What if you need to parse through thousands of lines looking for a specific entry?
Just use the search feature!
Hit /
then enter a term to find the next match, with n
advancing to successive matches.
Let‘s search Apache logs for GET requests from a suspicious IP:
/125.16.23.10
(press n to find next match)
Now you can easily inspect relevant access attempts.
Key less Commands & Options
That covers the basics – but less
has many more handy commands:
Navigation
- Ctrl-F – Scroll down one window
- Ctrl-B – Scroll back up one window
Search
- n – Find next search match
- N – Find previous search match
Viewing
- -N – Show line numbers
- -S – Chop long lines instead of wrapping
See the full breadth with man less
.
Should I Use less?
Less is right for reviewing longer, structured logs and configs that exceed your terminal height.
The ability to interactively search and scroll makes parsing through thousands of lines a breeze!
head – View Start of Files
Sometimes you only want a sneak peek at the beginning of a configuration file or log. That‘s where the head
command comes in handy!
By default head
displays the first 10 lines of a file. But you can customize the amount to your needs.
Head By Example
Here‘s a peek at our todo list with head
:
$ head todo-list.txt
Organize garage
File taxes
Finish Linux blog post
Clean gutters
Prep garden beds
Buy groceries
Plan vacation
Renew SSL certificate
Install software update
Want more than 10 lines? Use -n
to define a custom count:
$ head -n 15 todo-list.txt
(shows first 15 lines)
You can also view byte-count limits with -c
instead of lines.
head
works great with pipes for peeking at command output:
$ sudo lsof -n | head
COMMAND PID
init 1
systemd 42
kthreadd 92
When Should I Use head?
head
allows quickly peeking at starts of files to preview contents or check syntax. Useful before opening large files.
It‘s also helpful for sampling command output before redirecting to a file or another command.
tail – View End of Files
If head
displays the start of files, then tail
shows the end. This makes tail
perfect for peeking at the most recent logs.
Like head
, it defaults to 10 lines but is configurable.
Tail By Example
Here‘s a snippet from the end of a lengthy installation log:
$ tail install.log
Installing application files [COMPLETE]
Optimizing system settings [COMPLETE]
Installation finished! [SUCCESS]
Get more lines with -n
just like head
:
$ tail -n 15 install.log
(shows last 15 lines)
But tail
has another superpower…
Follow Log File Changes in Real-time
For logs continually being written to like Apache access logs, tail
allows following the changing file.
The -f
option lets you monitor logs in real-time:
$ tail -f /var/log/apache2/access.log
[connection from 123.123.234.10]
[connection from 192.168.50.10]
[connection from 172.10.10.55]
Now you can watch logs stream in live – extremely valuable for troubleshooting!
When Should I Use tail?
Anytime you want to inspect the end of log files, tail
does the trick.
Combined with real-time -f
, it becomes a log monitoring powerhouse for diagnosing issues.
cat vs less vs more – How to Choose
By this point, you may be wondering…
Should I use cat
, less
or legacy more
for viewing different file types?
Here is a helpful breakdown:
- cat – Short single files you want to print to terminal
- more – Simple terminal paging but limited features
- less – Robust file viewer with scrolling and search
In summary:
- For quick peeks, use
cat
- For basic paging,
more
(orless
) - For navigating/searching large files, reach for
less
!
Choosing the right tool for the job saves you precious time.
nl – Number Lines
Sometimes during log searches, you may need to reference specific lines when reporting an issue.
That‘s where the nl
(number lines) command comes in extremely handy!
nl in Action
nl
works just like cat
but prepends line numbers:
$ nl todo-list.txt
1 Organize garage
2 File taxes
3 Finish Linux blog post
Now you have numbered context for pinspointing error log entries without counting manually.
Customize Line Numbers
Change the starting number with -v
:
$ nl -v 10 todo-list.txt
10 Organize garage
11 File taxes
Left align with -n ln
:
$ nl -n ln install.log
1 Installing package files [COMPLETE]
2 Configuring system [IN PROGRESS]
More numerically semantic formats are also available.
Explore with man nl
!
When Should I Use nl?
nl
brings line numbering right to the terminal. Extremely valuable when needed to provide precise file references.
Expert Tips for Linux File Viewing Mastery
You now have an indispensable file viewing toolkit: cat
, nl
, more
, less
, head
and tail
.
Let‘s wrap up with some pro tips for getting the most from these commands:
- Learn regex for faster log searches with
less
andgrep
- Bookmark man pages for quick reference while working
- Pipe viewers together e.g.
cat access.log | less
- Use
>
and>>
to write file output to new logs - Explore graphical viewers like
gjyview
for imaging - Master your editor like Vim or Emacs for advanced viewing
File viewing is a gateway into advanced Linux skills like scripting, monitoring, compilation and more.
Implement these tools and techniques in your daily workflow to level up faster!
Let me know in the comments if you have any other favorite file viewing tricks in Linux!
Frequently Asked File Viewing Questions
Still hungry for more Linux file viewing knowledge? Here are some commonly asked questions:
Q: How can I check a file‘s type before viewing it?
Use the file
command. It prints the file classification (text, executable, image etc).
Q: What‘s the easiest way to search through huge log files?
Pipe commands together! Send your logs through grep
to match text or sed
to filter. Then pipe the output into less
or tail -f
.
Q: Can I searchcompressed logs too?
Yes! Use zgrep
and zless
to search gzipped log files without explicitly decompressing.
Q: How do I view contents of a 20GB database dump file!?
less
is your friend here – it handles large files well. But it may take awhile to load 20GB into memory!
Alternatively, pipe snippets through head
and tail
or use strings
to extract human-readable portions.
Q: Can I view images/documents through the terminal?
You bet! Use xdg-open
or graphical viewers like display
(images) and gv
(documents) to open files from terminal.
Master Linux File Viewing with Confidence
You made it to the end!
In this comprehensive guide, you learned the six essential Linux commands for viewing files:
- Print with
cat
- Page through files with
less
andmore
- See file beginnings and ends with
head
andtail
- Monitor real-time logs with
tail -f
- Number lines with
nl
- Plus pro power-user tips for advanced usage!
These form the basis for so many critical admin tasks – from configuration to debugging and scripting.
Implement the techniques shown here in your everyday terminal work. Soon manipulating files will feel natural as reading a book or riding a bike!
I hoped you enjoyed this whilrwind tour of Linux file viewers. Let me know if you have any other favorites that weren‘t covered!
Happy viewing 🙂