Skip to content

Linux Exercise 5 - Finding Text in Files

Exercise: Searching and Filtering Text in Linux

Objective

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


Tasks

Task 1: Search for Your User Information

  • 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.

Task 2: Find Files Containing “hello”

  • 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.

Task 3: Filter locate Results

  • 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.

Task 5: Count Matches

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

Task 6: Invert Match

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

Task 7: Regular Expression Challenge

  • 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.

Task 8: Recursive Search with Line Numbers

  • 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.

Task 9: Top 10 Frequent Words

  • 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.

📑 Protocol (What to Hand In)

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.