Find a course

Module 2: Working with FilesLesson 5 of 1312 min

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.

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 file prints 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.txt prints both, one after the other.)
  • less file opens a scrollable viewer. Use the arrows, Space and b to page, /word to search forward, n for the next match, G to jump to the end, and q to quit. It loads only what it needs, so it opens huge files instantly.
  • head file shows the first 10 lines; head -n 30 file shows 30.
  • tail file shows the last 10 lines; tail -n 50 file shows 50.
  • tail -f file follows 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).

  1. Watch new lines appear in a log in real time
  2. Scroll and search through a very large file
  3. See the first 20 lines of a CSV to check its columns
  4. Count how many lines a file has
  5. Print a short config file in full

Step 3 of 12Compare

Four ways to read a file

Aspectcatlessheadtail
ShowsThe whole file at onceOne screen at a timeThe first 10 linesThe last 10 lines
Good forShort files like /etc/hostnameLong files, even several GBChecking the format of a fileThe newest log entries
Useful optioncat 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.log

Options you will use constantly:

  • -i ignores case, so "Error", "ERROR" and "error" all match.
  • -n shows the line number of each match.
  • -v inverts the match: show lines that do not contain the pattern. grep -v "^#" config.conf hides comment lines.
  • -c counts matching lines instead of printing them.
  • -r searches recursively through every file in a directory: grep -r "db_password" /etc/myapp/.
  • -l lists only the names of files that contain a match.
  • -w matches whole words, so grep -w cat skips "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); -iname ignores case. Quote the pattern so the shell doesn't expand it first.
  • -type f for regular files, -type d for directories.
  • -size +100M for files larger than 100 MiB; -size -10k for smaller than 10 KiB.
  • -mtime -7 for files modified less than 7 days ago; -mtime +30 for more than 30 days ago.
  • -user lena for 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

Aspectgrepfind
SearchesInside files, line by lineThe file system, for files themselves
Matches onText patterns (regular expressions)Name, type, size, age, owner
Examplegrep -rn "timeout" /etc/myappfind /var/log -name "*.log" -mtime +30
AnswersWhich 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?