Skip to content
$ lynuxpro
← Linux

LESSON 4 OF 26 · BEGINNER · 17 MIN

Viewing and searching files

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: cat for small files, less for big ones, head/tail for the start or end, grep to search inside files, and find to 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/hosts
SHELLbashutf-8 · 1L
127.0.0.1 localhost10.0.1.10 web110.0.1.11 web210.0.2.10 db1
OUTPUTterminalutf-8 · 4L

Use 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
OUTPUTterminalutf-8 · 1L
KeyWhat it does
Space / bPage down / page up
/errorSearch forward for "error"
n / NNext / previous match
G / gJump to the end / start
qQuit

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/syslog
SHELLbashutf-8 · 2L
Nov 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 503
OUTPUTterminalutf-8 · 3L
  • head -n 20 file shows the first 20 lines (10 if you leave out -n).
  • tail -n 20 file shows the last 20 lines, which are the newest log entries.
  • tail -f file follows 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/syslog
SHELLbashutf-8 · 1L

Remember 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/syslog
SHELLbashutf-8 · 1L
Nov 28 09:06:02 lab app[2211]: ERROR database connection timeoutNov 28 09:15:10 lab app[2211]: ERROR payment service returned 503
OUTPUTterminalutf-8 · 2L

The options you will use every day:

OptionMeaningExample
-iIgnore upper/lower casegrep -i error finds ERROR, Error and error
-nShow line numbersgrep -n timeout app.log
-cCount matching linesgrep -c ERROR app.log
-vOpposite: lines that do not matchgrep -v DEBUG app.log
-rSearch every file in a foldergrep -r "password" /etc/app/
-A 3 / -B 3Also show 3 lines after / before each matchTo see the lines around an error
-EAllow "this OR that" patternsgrep -E "ERROR|WARN" app.log
grep -n -i warn /var/log/sysloggrep -c ERROR /var/log/sysloggrep -v INFO /var/log/syslog
SHELLbashutf-8 · 3L

Remember it: -i ignore case, -n numbers, -c count, -v "very opposite", -r recursive (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:

PatternMatchesThink of it as
^Nov 28Lines that start with "Nov 28"^ = start of line
503$Lines that end in 503$ = end of line
ERROR.*paymentERROR, 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*"
SHELLbashutf-8 · 3L
TestMeaning
-name "*.log"Name matches (cares about case)
-inameName matches, ignoring case
-type f / -type dOnly files / only folders (directories)
-mtime -1Changed in the last day
-size +100MBigger than 100 MB

You can join tests together. For example, find big, old logs:

find /var/log -name "*.log" -size +100M -mtime +7
OUTPUTterminalutf-8 · 1L

Remember it: grep looks inside files. find looks 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
SHELLbashutf-8 · 3L
  • Line 1: find the ERROR lines, then count them (wc -l counts 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/syslog
SHELLbashutf-8 · 3L

You 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

  • cat on huge files floods the terminal. Use less, head or tail.
  • 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

  1. Show the last 4 lines of /var/log/syslog.
  2. Count how many lines in /var/log/syslog contain ERROR.
  3. Show every line that is not INFO, with line numbers.
  4. Find every .conf file under /etc.
  5. List the unique IP addresses in /var/log/nginx/access.log.

Cheat sheet

CommandWhat it does
less filePage through a big file
head -n 20 fileFirst 20 lines
tail -n 20 fileLast 20 lines
tail -f fileWatch a log live
grep -in "x" fileSearch, ignoring case, with line numbers
grep -r "x" dir/Search a whole folder
find /path -name "*.log"Find files by name
cmd1 | cmd2Send one command's output into another

Key takeaways

  • Use cat for small files and less for big ones.
  • tail shows the newest lines of a log, and tail -f keeps watching it.
  • grep finds lines inside files. find finds the files themselves.
  • The pipe | passes one command's output to the next.