Skip to content

Linux Exercise 5 - Finding Text in Files

Practice using grep, egrep, and related tools to search for patterns in files and directories. Learn to interpret results and document findings in a protocol.


  • Locate the line in /etc/passwd that contains your login details.
  • Expected outcome: You should find a single line with your username, UID, GID, home directory, and shell.

  • Search the entire /home directory tree for files that contain the word hello.
  • Sort the results alphabetically by filename.
  • Expected outcome: A sorted list of file paths where the word appears.

  • Use locate to find all filenames containing the word emacs.
  • Exclude any results that contain the word lib.
  • Expected outcome: A filtered list of paths related to emacs but not libraries.

  • In your home directory, search all .txt files for lines containing the word error, ignoring case.
  • Expected outcome: Lines from text files that contain error in any case variation.

  • Count how many lines in /var/log (recursively) contain the word failed.
  • Expected outcome: A total number of matching lines across all log files.

  • In a chosen text file, display all lines that do not contain the word root.
  • Expected outcome: All lines except those mentioning root.

  • Create a file with at least 10 lines, some of which match this regex:
    (^[0-9]{1,5}[a-zA-Z ]+$)|none
  • Use egrep to test which lines match.
  • Expected outcome: Confirmation of which lines match and which do not.

  • Search recursively in /etc for lines starting with # (comments).
  • Display line numbers in the output.
  • Expected outcome: Lines from configuration files that are comments, with their line numbers.

  • In a large text file (e.g., /var/log/syslog), find the top 10 most frequent words.
  • Expected outcome: A list of words with their frequency counts, sorted from most to least frequent.

Each student must create a protocol (log) containing:

  • Command executed
  • Output received (full or partial if very long)
  • Explanation (1–2 sentences) of what the command did
  • Observation/interpretation of what they learned

Example:

Command: file sample.zip
Output: sample.zip: Zip archive data, at least v2.0 to extract
Explanation: Identified sample.zip as a compressed archive file.
Observation: Shows that zip files are binary and need archive tools to inspect contents.
Terminal window
Command: sudo tail -f /var/log/syslog
Output: (live log output appears)
Explanation: Monitors new log entries in real-time.
Observation: Useful for watching system activity as it happens.