In one minute
- Commands to read files of any size and to search through them fast.
- When something breaks, the answer is almost always written in a file: a log, a config or an error report. These are the commands you will use most during incidents.
- Pick the tool by the job:
catfor small files,lessfor big ones,head/tailfor the start or end,grepto search inside files, andfindto search for files. Then join tools together with the pipe|. - 👉 Every incident ("why did it fail?"), every config check ("what is this set to?") and every "where is that file?" moment.
Reading whole files: cat
cat prints a whole file on the screen.
The quickest way to read a short file.
cat /etc/hosts127.0.0.1 localhost10.0.1.10 web110.0.1.11 web210.0.2.10 db1Use cat only for short files. On a 2 GB log it floods your screen. Use the tools below instead.
Paging through big files: less
less shows a file one screen at a time.
So you can read and search huge files without flooding the screen.
Open the file, then use keys to move around. On a real server:
less /var/log/syslog| Key | What it does |
|---|---|
| Space / b | Page down / page up |
/error | Search forward for "error" |
n / N | Next / previous match |
G / g | Jump to the end / start |
q | Quit |
less does not load the whole file into memory, so it is safe even on huge logs.
Remember it: "Less is more": it shows you a little at a time. q = quit.
The start and end of a file: head and tail
head shows the first lines of a file. tail shows the last lines.
Logs add new lines at the bottom, so tail shows you the newest events. head is handy to peek at the top of a file, such as column names.
head -n 3 /var/log/syslogtail -n 3 /var/log/syslogNov 28 09:06:05 lab app[2211]: INFO database connectedNov 28 09:12:30 lab kernel: [ 1201.33] eth0: link upNov 28 09:15:10 lab app[2211]: ERROR payment service returned 503head -n 20 fileshows the first 20 lines (10 if you leave out-n).tail -n 20 fileshows the last 20 lines, which are the newest log entries.tail -f filefollows the file: it keeps printing new lines as they are written. Use it to watch a log live while you reproduce a bug. Press Ctrl+C to stop.
tail -f /var/log/syslogRemember it: Head = top of the body. Tail = bottom.
-f= follow.
Searching inside files: grep
grep prints only the lines that contain a word or pattern.
A log can have a million lines. grep finds the 5 lines you care about in a second.
grep what where.
grep ERROR /var/log/syslogNov 28 09:06:02 lab app[2211]: ERROR database connection timeoutNov 28 09:15:10 lab app[2211]: ERROR payment service returned 503The options you will use every day:
| Option | Meaning | Example |
|---|---|---|
-i | Ignore upper/lower case | grep -i error finds ERROR, Error and error |
-n | Show line numbers | grep -n timeout app.log |
-c | Count matching lines | grep -c ERROR app.log |
-v | Opposite: lines that do not match | grep -v DEBUG app.log |
-r | Search every file in a folder | grep -r "password" /etc/app/ |
-A 3 / -B 3 | Also show 3 lines after / before each match | To see the lines around an error |
-E | Allow "this OR that" patterns | grep -E "ERROR|WARN" app.log |
grep -n -i warn /var/log/sysloggrep -c ERROR /var/log/sysloggrep -v INFO /var/log/syslogRemember it:
-iignore case,-nnumbers,-ccount,-v"very opposite",-rrecursive (whole folder).
Patterns (regex): a little goes a long way
A regex (regular expression) is a pattern made of special characters.
It lets you search for things like "lines that start with a date" or "lines that end with 503".
You only need a few to start:
| Pattern | Matches | Think of it as |
|---|---|---|
^Nov 28 | Lines that start with "Nov 28" | ^ = start of line |
503$ | Lines that end in 503 | $ = end of line |
ERROR.*payment | ERROR, then anything, then payment | .* = "anything in between" |
[0-9]+ | One or more digits (with -E) | + = "one or more" |
Finding files: find
find searches for files themselves, by name, type, size or age.
For questions like "where is the nginx config?" or "which logs are eating the disk?"
find where what-to-match.
find /etc -name "*.conf"find /var/log -type ffind . -name "notes*"| Test | Meaning |
|---|---|
-name "*.log" | Name matches (cares about case) |
-iname | Name matches, ignoring case |
-type f / -type d | Only files / only folders (directories) |
-mtime -1 | Changed in the last day |
-size +100M | Bigger than 100 MB |
You can join tests together. For example, find big, old logs:
find /var/log -name "*.log" -size +100M -mtime +7Remember it:
greplooks inside files.findlooks for files.
Joining tools with pipes |
The pipe | sends what one command prints into the next command.
Each Linux tool does one small job. Pipes let you chain small tools into one powerful command.
Think of an assembly line: each command does its step and hands the result on.
grep ERROR /var/log/syslog | wc -lcat /var/log/syslog | grep app | tail -n 2awk '{print $1}' /var/log/nginx/access.log | sort | uniq- Line 1: find the ERROR lines, then count them (
wc -lcounts lines). - Line 2: show the app's lines, then keep only the last 2.
- Line 3: take the first column (the client IP), sort it, then remove repeats. The result is every unique IP that visited nginx. You will need this often during traffic spikes.
Real-world scenario: "checkout is failing"
Users say payments fail. You check the log for errors, count them, then read the newest ones:
grep -c ERROR /var/log/sysloggrep ERROR /var/log/syslog | tail -n 5grep -i payment /var/log/syslogYou see payment service returned 503. Now you know the problem is in the payment service, not the app itself. Thirty seconds of grep saved an hour of guessing.
Common mistakes
caton huge files floods the terminal. Useless,headortail.- Forgetting
-i, and missing "Error" because you searched for "ERROR". - Not quoting patterns that have spaces or special characters. Always quote:
grep "connection refused" app.log.
Practice
- Show the last 4 lines of
/var/log/syslog. - Count how many lines in
/var/log/syslogcontain ERROR. - Show every line that is not INFO, with line numbers.
- Find every
.conffile under/etc. - List the unique IP addresses in
/var/log/nginx/access.log.
Cheat sheet
| Command | What it does |
|---|---|
less file | Page through a big file |
head -n 20 file | First 20 lines |
tail -n 20 file | Last 20 lines |
tail -f file | Watch a log live |
grep -in "x" file | Search, ignoring case, with line numbers |
grep -r "x" dir/ | Search a whole folder |
find /path -name "*.log" | Find files by name |
cmd1 | cmd2 | Send one command's output into another |
Key takeaways
- Use
catfor small files andlessfor big ones. tailshows the newest lines of a log, andtail -fkeeps watching it.grepfinds lines inside files.findfinds the files themselves.- The pipe
|passes one command's output to the next.