tech katta

Reading and finding: cat, less, head, tail -f, grep and find

Lesson 04 of 23 · 4 min readFree

In this lesson

Read files of any size without opening an editor, follow a log as it grows, search inside files with grep and locate files with find.

On this page
  1. Why this matters
  2. Reading a file: cat, less, head and tail
  3. Searching text: grep
  4. Finding files: find
  5. Try it

On a server you spend more time reading than writing: reading a config to see what it says, reading a log to see what went wrong, searching a codebase for where a setting comes from. These six commands cover nearly all of it.

Why this matters#

A production log can be hundreds of megabytes. Opening it in an editor would be slow or impossible, and you only need a few lines of it anyway. The reading tools here let you look at the beginning, the end, a live stream or just the lines that matter, and they work the same on every Linux machine you will ever log in to.

The examples use a small nginx-style access log, access.log, with 120 requests from the example addresses 203.0.113.x and 198.51.100.x. Real log files have the same shape, only bigger.

Reading a file: cat, less, head and tail#

cat prints a whole file. It is perfect for short files such as a config.

Shell
cat -n app/shared/.env
Text
     1	DB_URL=postgres://example

-n numbers the lines. Another useful flag is -A, which marks the end of each line with $ and shows tabs and other invisible characters. If a config seems right but fails, cat -A often reveals a stray space or a Windows line ending.

For a long file, cat would flood your screen. Use less, a pager that lets you scroll. Inside it:

  • Space or f moves down a page, and b moves up.
  • /word searches forward, n jumps to the next match and N to the previous one.
  • G jumps to the end, g to the start.
  • q quits.

head and tail show the first and last lines. The default is 10; -n sets the number.

Shell
head -n 3 access.log
tail -n 2 access.log
Text
198.51.100.4 - - [02/Dec/2026:10:00:08 +0000] "POST /login HTTP/1.1" 200 7552 "-" "curl/8.5.0"
198.51.100.9 - - [02/Dec/2026:10:00:12 +0000] "GET /api/v1/lessons/99 HTTP/1.1" 500 3175 "-" "curl/8.5.0"
198.51.100.9 - - [02/Dec/2026:10:00:15 +0000] "GET /static/app.js HTTP/1.1" 304 1692 "-" "curl/8.5.0"
203.0.113.25 - - [02/Dec/2026:10:08:57 +0000] "GET /api/v1/courses HTTP/1.1" 200 343 "-" "curl/8.5.0"
203.0.113.77 - - [02/Dec/2026:10:09:03 +0000] "GET /api/v1/lessons/12 HTTP/1.1" 200 4155 "-" "curl/8.5.0"

Each line is one request. Reading left to right: client address, two unused fields, time, the request line (method, path, protocol), the status code, the response size in bytes, the referrer and the user agent.

Following a log live: tail -f#

tail -f prints the last lines and then keeps the command open, printing new lines as they are written. It is the standard way to watch a service while you test it from another window. Press Ctrl+C to stop.

Shell
tail -n 2 -f access.log
Text
203.0.113.25 - - [02/Dec/2026:10:08:57 +0000] "GET /api/v1/courses HTTP/1.1" 200 343 "-" "curl/8.5.0"
203.0.113.77 - - [02/Dec/2026:10:09:03 +0000] "GET /api/v1/lessons/12 HTTP/1.1" 200 4155 "-" "curl/8.5.0"
198.51.100.4 - - [02/Dec/2026:10:09:10 +0000] "GET /health HTTP/1.1" 200 15 "-" "curl/8.5.0"

The first two lines were already in the file. The third appeared after the command started, when a new request was appended. A related option, tail -F, also keeps following when the log is rotated and replaced by a fresh file, which suits long-running watches.

Searching text: grep#

grep prints the lines of a file that match a pattern. It is the tool you will use most in this lesson.

Shell
grep ' 500 ' access.log | head -3
Text
198.51.100.9 - - [02/Dec/2026:10:00:12 +0000] "GET /api/v1/lessons/99 HTTP/1.1" 500 3175 "-" "curl/8.5.0"
203.0.113.25 - - [02/Dec/2026:10:02:13 +0000] "GET /api/v1/lessons/99 HTTP/1.1" 500 404 "-" "curl/8.5.0"
203.0.113.25 - - [02/Dec/2026:10:02:27 +0000] "GET /api/v1/lessons/99 HTTP/1.1" 500 2373 "-" "curl/8.5.0"

The spaces around 500 matter. Without them, grep would also match a response size such as 5004. Every failing request here is for the same path, /api/v1/lessons/99, which is already a lead.

The options you will use daily:

  • -n shows line numbers, so you can find the spot in the file.
  • -c prints only how many lines matched.
  • -i ignores upper and lower case.
  • -v inverts the match and prints lines that do not match.
  • -E turns on extended patterns, so grep -E ' (404|500) ' matches either code.
  • -r searches every file under a folder, and -l prints only the names of files that match.
  • -B 2 -A 2 prints two lines before and after each match, which helps you see context.
Shell
grep -c ' 500 ' access.log
grep -E ' (404|500) ' access.log | wc -l
grep -n 'lessons/99' access.log | head -3
Text
7
18
2:198.51.100.9 - - [02/Dec/2026:10:00:12 +0000] "GET /api/v1/lessons/99 HTTP/1.1" 500 3175 "-" "curl/8.5.0"
10:203.0.113.25 - - [02/Dec/2026:10:00:33 +0000] "GET /api/v1/lessons/99 HTTP/1.1" 404 8338 "-" "curl/8.5.0"
32:203.0.113.25 - - [02/Dec/2026:10:02:13 +0000] "GET /api/v1/lessons/99 HTTP/1.1" 500 404 "-" "curl/8.5.0"

(wc -l counts lines; lesson 5 explains the | pipe.)

grep also works on code and config. To find where a setting is used across a deployed app:

Shell
grep -rn 'DB_URL' app
Text
app/shared/.env:1:DB_URL=postgres://example

Quote your pattern. Without quotes, characters like * or | are handled by the shell before grep sees them. Note that in basic patterns . matches any character; use grep -F to search for a literal string such as 1.2.3.

Finding files: find#

grep looks inside files. find looks for files by name, type, size or age. It walks a folder tree and applies tests to each entry.

Shell
find app -name '*.js'
find app -type d
find app -name '*.log' -size +1M
find app -name '*.log' -mtime +7
Text
app/releases/20261202/src/db.js
app/releases/20261202/server.js
app
app/shared
app/shared/logs
app/releases
app/releases/20261202
app/releases/20261202/src
app/shared/logs/big.log
app/shared/logs/old.log

The first argument is where to start. Then come tests:

  • -name 'pattern' matches the file name, and -iname ignores case. Quote the pattern so find sees the *, not the shell.
  • -type f means files and -type d means directories.
  • -size +1M means larger than one megabyte.
  • -mtime +7 means modified more than 7 days ago, and -mmin -60 means modified in the last 60 minutes.

You can act on each match with -exec. The {} stands for the file and \; ends the command:

Shell
find app -name '*.log' -mtime +7 -exec ls -l {} \;
Text
-rw-r--r-- 1 root root 0 Nov 22 20:44 app/shared/logs/old.log

Replace ls -l with rm and you have the classic "delete logs older than a week" cleanup. Run it first with -exec ls -l, or just find ... -print, to check the list. If find cannot read a folder it prints "Permission denied" and carries on; add 2>/dev/null to hide those messages (lesson 5 explains why).

Try it#

  1. Create a file with ten lines using seq 1 10 > numbers.txt. Show the first three lines, the last two, and line numbers with cat -n.
  2. Write a small log file of your own (copy the format above for five or six lines). Use grep -c to count requests with status 200, and grep -v to show the ones that are not 200.
  3. Open the log in less, search for 404 with /404, jump to the next match with n, then quit with q.
  4. In one terminal run tail -f on a file. In a second terminal append a line with echo 'new line' >> file. Watch it appear in the first.
  5. In a folder with some .log files, use find to list the ones larger than 1 MB, then the ones older than 7 days. Combine the second one with -exec ls -l {} \; and read the result before doing anything else.