Reading and finding: cat, less, head, tail -f, grep and find
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
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.
cat -n app/shared/.env 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
fmoves down a page, andbmoves up. /wordsearches forward,njumps to the next match andNto the previous one.Gjumps to the end,gto the start.qquits.
head and tail show the first and last lines. The default is 10; -n sets the number.
head -n 3 access.log
tail -n 2 access.log198.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.
tail -n 2 -f access.log203.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.
grep ' 500 ' access.log | head -3198.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:
-nshows line numbers, so you can find the spot in the file.-cprints only how many lines matched.-iignores upper and lower case.-vinverts the match and prints lines that do not match.-Eturns on extended patterns, sogrep -E ' (404|500) 'matches either code.-rsearches every file under a folder, and-lprints only the names of files that match.-B 2 -A 2prints two lines before and after each match, which helps you see context.
grep -c ' 500 ' access.log
grep -E ' (404|500) ' access.log | wc -l
grep -n 'lessons/99' access.log | head -37
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:
grep -rn 'DB_URL' appapp/shared/.env:1:DB_URL=postgres://exampleQuote 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.
find app -name '*.js'
find app -type d
find app -name '*.log' -size +1M
find app -name '*.log' -mtime +7app/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.logThe first argument is where to start. Then come tests:
-name 'pattern'matches the file name, and-inameignores case. Quote the pattern sofindsees the*, not the shell.-type fmeans files and-type dmeans directories.-size +1Mmeans larger than one megabyte.-mtime +7means modified more than 7 days ago, and-mmin -60means modified in the last 60 minutes.
You can act on each match with -exec. The {} stands for the file and \; ends the command:
find app -name '*.log' -mtime +7 -exec ls -l {} \;-rw-r--r-- 1 root root 0 Nov 22 20:44 app/shared/logs/old.logReplace 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#
- Create a file with ten lines using
seq 1 10 > numbers.txt. Show the first three lines, the last two, and line numbers withcat -n. - Write a small log file of your own (copy the format above for five or six lines). Use
grep -cto count requests with status 200, andgrep -vto show the ones that are not 200. - Open the log in
less, search for404with/404, jump to the next match withn, then quit withq. - In one terminal run
tail -fon a file. In a second terminal append a line withecho 'new line' >> file. Watch it appear in the first. - In a folder with some
.logfiles, usefindto 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.
Sign in to keep track of the lessons you finish.