Linux Exercise 5 - Finding Text in Files
Exercise: Searching and Filtering Text in Linux
Section titled âExercise: Searching and Filtering Text in LinuxâObjective
Section titled âObjectiveâ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.
Task 1: Search for Your User Information
Section titled â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â
Section titled â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
Section titled â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.
Task 4: Case-Insensitive Search
Section titled âTask 4: Case-Insensitive Searchâ- 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
Section titled â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
Section titled â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
Section titled â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
Section titled â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
Section titled â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)
Section titled âđ 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.zipOutput: sample.zip: Zip archive data, at least v2.0 to extractExplanation: Identified sample.zip as a compressed archive file.Observation: Shows that zip files are binary and need archive tools to inspect contents.Command: sudo tail -f /var/log/syslogOutput: (live log output appears)Explanation: Monitors new log entries in real-time.Observation: Useful for watching system activity as it happens.