FLOPPIX Home | FAQ | Download | Labs | ToC | Back |  Next 

Grep


Notes:

The grep command searches the contents of a file for lines that contain a certain pattern.

Examples:


Exercises:

  1. Use the grep command to display the lines from the file /etc/passwd for users with the flin login shell.
     

  2. Use the grep command to display the lines from the file /etc/hosts.deny that are NOT comment lines (the comment lines are the lines that start with #).
     

  3. Use the grep command to display all lines from any file in /etc/init.d that start with echo.
     

  4. Which files in /etc/init.d contain the string "dmesg"? ____________________
     

  5. Which files in /etc/init.d contain the word tr? _________________________
    Note: use the man command to lookup to option for grep that searches for the word "tr" instead of the string "tr".


Questions & Answers:

  1. What command will display all lines in /etc/init.d/userinitials that start with echo?
     
    Answer:  grep ^echo /etc/init.d/userinitials
     

  2. What command will display all lines that contain echo in any file in the directory /etc/init.d?
     
    Answer:  grep echo /etc/init.d/*
     

  3. What command will display the lines in the file /etc/login.defs that do not start with #
     
    Answer:  grep -v '^#' /etc/login.defs
     

  4. What command will display a list of users whose login shell is /bin/bash. (Recall that the login shell is the last entry in the line for each user in the file /etc/passwd).
     
    Answer:  grep /bin/bash$ /etc/passwd

Copyright © L. M. MacEwan
FLOPPIX Home | FAQ | Download | Labs | ToC | Back |  Next