Viewing and Searching Files
Read files with cat, less, head and tail, follow live logs, count lines with wc, search text with grep and locate files with find.
Welcome back!
Step 1 of 12Learn
A 2 GB log file at 3 a.m.
The website is down. Somewhere in /var/log/nginx/error.log, a 2 GB file, is the reason. Opening it in a graphical editor would freeze the machine. On the command line you can read the last 50 lines in a fraction of a second, or pull out only the lines that mention "timeout".
Tools for reading files:
cat fileprints the whole file to the screen. Fine for short files like/etc/hostname; useless for long ones, which scroll past too fast. (The name comes from con*cat*enate:cat a.txt b.txtprints both, one after the other.)less fileopens a scrollable viewer. Use the arrows, Space andbto page,/wordto search forward,nfor the next match,Gto jump to the end, andqto quit. It loads only what it needs, so it opens huge files instantly.head fileshows the first 10 lines;head -n 30 fileshows 30.tail fileshows the last 10 lines;tail -n 50 fileshows 50.tail -f filefollows the file: it keeps printing new lines as they are written. Run it on a log while you reload a web page and you watch errors appear live. Press Ctrl+C to stop following.
wc file counts lines, words and bytes. wc -l access.log answers "how many requests were logged?" because each request is one line.
Step 2 of 12Practice · Match the pairs
Match each goal to the best command.
Tap an answer, then tap the place where it belongs (or drag it there).
- Watch new lines appear in a log in real time
- Scroll and search through a very large file
- See the first 20 lines of a CSV to check its columns
- Count how many lines a file has
- Print a short config file in full
Step 3 of 12Compare
Four ways to read a file
| Aspect | cat | less | head | tail |
|---|---|---|---|---|
| Shows | The whole file at once | One screen at a time | The first 10 lines | The last 10 lines |
| Good for | Short files like /etc/hostname | Long files, even several GB | Checking the format of a file | The newest log entries |
| Useful option | cat a b joins files | /word searches, q quits | -n 30 for 30 lines | -f follows new lines live |
Step 4 of 12Learn
Searching inside files with grep
grep prints every line that contains a pattern. It is probably the single most used tool for troubleshooting.
$ grep "timeout" /var/log/nginx/error.logOptions you will use constantly:
-iignores case, so "Error", "ERROR" and "error" all match.-nshows the line number of each match.-vinverts the match: show lines that do not contain the pattern.grep -v "^#" config.confhides comment lines.-ccounts matching lines instead of printing them.-rsearches recursively through every file in a directory:grep -r "db_password" /etc/myapp/.-llists only the names of files that contain a match.-wmatches whole words, sogrep -w catskips "category".
Patterns are regular expressions, a small language for describing text. A few symbols cover most needs: ^ anchors to the start of a line, $ to the end, . matches any one character and .* any run of characters. So grep "^ERROR" finds lines that begin with ERROR, and grep "failed.*root" finds "failed" followed later on the same line by "root". Wrap patterns in quotes so the shell doesn't try to expand special characters itself.
Step 5 of 12Practice · Spot the mistake
A teammate wants to list the line numbers of all lines in auth.log that contain "Failed password", ignoring case. Tap the part of this command that is wrong.
Step 6 of 12Learn
Finding files with find
grep searches inside files. To search for the files themselves by name, size, type or age, use find. Its grammar is: where to start, then tests.
$ find /etc -name "*.conf"That walks through /etc and every directory below it, printing paths whose names end in .conf. Common tests:
-name "pattern"matches the file name (case-sensitive);-inameignores case. Quote the pattern so the shell doesn't expand it first.-type ffor regular files,-type dfor directories.-size +100Mfor files larger than 100 MiB;-size -10kfor smaller than 10 KiB.-mtime -7for files modified less than 7 days ago;-mtime +30for more than 30 days ago.-user lenafor files owned by a user.
Tests combine with an implied AND: find /var/log -type f -name "*.log" -mtime +30 lists log files untouched for a month. You can act on results with -delete or -exec, for example find . -name "*.tmp" -exec rm {} \; where {} stands for each file found. Always run the search without the action first and read the list.
Disk space questions go together with find: du -sh * shows how much space each item in the current directory uses, and df -h shows free space on each mounted disk.
Step 7 of 12Compare
grep vs find
| Aspect | grep | find |
|---|---|---|
| Searches | Inside files, line by line | The file system, for files themselves |
| Matches on | Text patterns (regular expressions) | Name, type, size, age, owner |
| Example | grep -rn "timeout" /etc/myapp | find /var/log -name "*.log" -mtime +30 |
| Answers | Which lines mention this? | Which files fit these rules? |
Step 8 of 12Try the calculator
File size converter
ls -l shows sizes in bytes. Tools like ls -h, du -h and find -size use binary units where 1 KiB = 1,024 bytes. Disk makers use decimal units where 1 GB = 1,000,000,000 bytes. Enter a size in bytes to compare.
- Kibibytes (KiB)
- 2,097,152
- Mebibytes (MiB)
- 2,048
- Gibibytes (GiB)
- 2
- Gigabytes (GB, decimal)
- 2.147
Step 9 of 12Practice · Work it out
Using the converter or the formula, how many MiB is a log file that ls -l reports as 52,428,800 bytes?
Step 10 of 12Note
Step 11 of 12Practice · Sort into groups
Sort each task by the tool you would reach for.
Tap an item, then tap its group (or drag it there).
Step 12 of 12Practice · Choose one
Which command lists all regular files under /home/lena that end in .log and were modified more than 30 days ago?
Lesson complete!