rh135-day11.pdf

(237 KB) Pobierz
RH135-Day11
Written by Razib Shahriar Rubence
Regular expressions and grep:
A regular expression is a pattern that describes a set of strings. Regular Expressions is nothing
but a pattern to match for each input line. A pattern is a sequence of characters.
The grep command searches the given files for lines containing a match to a given pattern list.
In other words, use the grep command to search words or strings in a text files. When it finds a
match in a file, it will display those line on screen. the command grep supports extended regular
expression
Examples in Practice LAB:
- Search for 'rhce' in /etc/passswd
# grep -color 'rhce' /etc/passwd
Sample outputs:
rhce:x:1000:1000:rhce Gite,,,:/home/rhce:/bin/bash
rhcegite:x:1001:1001::/home/rhcegite:/bin/sh
giterhce:x:1002:1002::/home/giterhce:/bin/sh
- Search rhce in any case (i.e. case insensitive search)
# grep -i -w rhce /etc/passwd
- Search rhce or datapark in any case
# grep -E -i -w 'rhce|datapark' /etc/passwd
The PATTERN in last example, used as an extended regular expression.
-You can use ^ and $ to force a regex to match only at the start or end of a line, respectively.
The following example displays lines starting with the rhce only:
grep '^rhce' /etc/passwd
Sample outputs:
rhce:x:1000:1000:rhce Gite,,,:/home/rhce:/bin/bash
rhcegite:x:1001:1001::/home/rhcegite:/bin/sh
- You can display only lines starting with the word rhce only i.e. do not display rhcegite, rhceg
etc:
grep -w '^rhce' /etc/passwd
- Find lines ending with word rhce:
grep 'rhce$' filename
- Match line only containing rhce:
grep '^rhce$' filename
1/3
RH135-Day11
Written by Razib Shahriar Rubence
-You can search for blank lines with the following examples:
grep '^$' filename
-You can search for users who has Shell
grep ':/bin/bash$' /etc/passwd
-Match rhce or Rhce:
grep '[rR]hce' filename
-You can also match digits (i.e match rhce1 or rhce2 etc):
grep -w '[rR]hce[0-9]' filename
-You can match two numeric digits (i.e. match rhce11, rhce12 etc):
grep 'rhce[0-9][0-9]' filename
-Display all the lines containing either a "w" or "n" character:
grep [wn] filename
- How Do I do OR with grep?
grep 'word1|word2' filename
How Do I do AND with grep?
grep 'word1' filename | grep 'word2'
The following will match both "col" and "cool":
egrep 'co{1,2}l' filename
How Do I Show Only The Matches, Not The Lines?
grep -o regex filename
Find Command:
Apart from the basic operation of looking for files under a directory structure, you can also
perform several practical operations using find command that will make your command line
journey easy.
01. Find files using name
This is a basic usage of the find command. This example finds all files with name —
MyCProgram.c in the current directory and all it’s sub-directories.
# find -name "MyCProgram.c"
02. Other options with find commnand
find /home -user datapark
Find every file under the directory /home owned by the user datapark.
2/3
RH135-Day11
Written by Razib Shahriar Rubence
find /usr -name *park
Find every file under the directory /usr ending in "park".
find /usr -name data*
Find every file under the directory /usr starting with "data".
find /var/spool -mtime +60
Find every file under the directory /var/spool that was modified more than 60 days ago.
Pipelines and Redirection
- ">" is used to redirect Standard Output to a file which will overrite all text (if any) in that file.
grep "root" /etc/passwd > /tmp/newfile
- ">>" is used to redirect Standard Output to a file which will add new lines with this output in
that file
grep "data" /etc/passwd >> /tmp/newfile
- "2>" is used to redirect Standard Error to a file
find /etc -name passwd 2> /tmp/errorfile
find /etc -name passwd > /tmp/outputfile 2> /tmp/errorfile
- "2> /dev/null"  is used to discard the error messages by redirecting to /dev/null
find /etc -name passwd > /tmp/outputfile 2>/dev/null
- "2>&1" is used to combine OUTPUT and and ERROR
find /etc -name passwd /tmp/all 2>&1
- | (pipe) is used to send OUTPUT from one command as INPUT of another command
cat /etc/passwd | grep "root"
First command shows all local users information from /etc/passwd the second command (grep
"root") took this information and finaly dispalys only root user.
3/3
Zgłoś jeśli naruszono regulamin