1-          the grep command:

 

DESCRIPTION

       Grep  searches  the  named  input  FILEs (or standard input if no files are named, or the file name - is given) for lines containing a match to the

       given PATTERN.  By default, grep prints the matching lines.

 

 

Hrm .. sounds interesting you say .. how do I use it?

 

First lets download a file that we can all work with:

wget http://rfplwgw/examples/lesson3.txt

 

 

Now lets take a look at a few examples:

 

 

rp-lxdesk13:tmurphy/apps/learnlinux/examples$ cat lesson3.txt | grep radiusd

radiusd:x:95:95:radiusd user:/home/radiusd:/sbin/nologin

radiusd1:x:95:95:radiusd user:/home/radiusd:/sbin/nologin

radiusd2:x:95:95:radiusd user:/home/radiusd:/sbin/nologin

 

note that the grep command pulled out only the lines that contained the string “radiusd”

 

 

rp-lxdesk13:tmurphy/apps/learnlinux/examples$ cat lesson3.txt | grep radiusd2

radiusd2:x:95:95:radiusd user:/home/radiusd:/sbin/nologin

 

now grep found only the part of the file that contained the more specific radiusd2

 

--- ----------- -- ------------------------ - -- -- - - - - - - -  - - - - - - - - - - - -

In the next example I’ll show you the “-i” switch.  –i makes the grep search string case insensitive

 

rp-lxdesk13:tmurphy/apps/learnlinux/examples$ cat lesson3.txt | grep  exim

exim:x:93:93::/var/spool/exim:/sbin/nologin

 

rp-lxdesk13:tmurphy/apps/learnlinux/examples$ cat lesson3.txt | grep  -i exim

exim:x:93:93::/var/spool/exim:/sbin/nologin

EXIM:x:93:93::/var/spool/EXIM:/sbin/nologin

 

--- ----------- -- ------------------------ - -- -- - - - - - - -  - - - - - - - - - - - -

Now lets talk about the –v example; -v shows me everything EXCEPT the search string:

 

rp-lxdesk13:tmurphy/apps/learnlinux/examples$ cat lesson3.txt | grep -v -i exim

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/nologin

daemon:x:2:2:daemon:/sbin:/sbin/nologin

adm:x:3:4:adm:/var/adm:/sbin/nologin

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

sync:x:5:0:sync:/sbin:/bin/sync

shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

halt:x:7:0:halt:/sbin:/sbin/halt

mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

news:x:9:13:news:/etc/news:

uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

operator:x:11:0:operator:/root:/sbin/nologin

games:x:12:100:games:/usr/games:/sbin/nologin

gopher:x:13:30:gopher:/var/gopher:/sbin/nologin

ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

nobody:x:99:99:Nobody:/:/sbin/nologin

nscd:x:28:28:NSCD Daemon:/:/sbin/nologin

ldap:x:55:55:LDAP User:/var/lib/ldap:/bin/false

distcache:x:94:94:Distcache:/:/sbin/nologin

vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin

squid:x:23:23::/var/spool/squid:/sbin/nologin

pcap:x:77:77::/var/arpwatch:/sbin/nologin

uuidd:x:100:101:UUID generator helper daemon:/var/lib/libuuid:/sbin/nologin

apache:x:48:48:Apache:/var/www:/sbin/nologin

rpc:x:32:32:Portmapper RPC user:/:/sbin/nologin

sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash

amanda:x:33:6:Amanda user:/var/lib/amanda:/bin/bash

rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin

nfsnobody:x:4294967294:4294967294:Anonymous NFS User:/var/lib/nfs:/sbin/nologin

mailnull:x:47:47::/var/spool/mqueue:/sbin/nologin

smmsp:x:51:51::/var/spool/mqueue:/sbin/nologin

postfix:x:89:89::/var/spool/postfix:/sbin/nologin

ntp:x:38:38::/etc/ntp:/sbin/nologin

cyrus:x:76:12:Cyrus IMAP Server:/var/lib/imap:/bin/bash

ident:x:98:98::/home/ident:/sbin/nologin

mailman:x:41:41:GNU Mailing List Manager:/usr/lib/mailman:/sbin/nologin

webalizer:x:67:67:Webalizer:/var/www/usage:/sbin/nologin

quagga:x:92:92:Quagga routing suite:/var/run/quagga:/sbin/nologin

pvm:x:24:24::/usr/share/pvm3:/bin/bash

radvd:x:75:75:radvd user:/:/sbin/nologin

privoxy:x:73:73::/etc/privoxy:/sbin/nologin

mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash

dbus:x:81:81:System message bus:/:/sbin/nologin

avahi:x:70:70:Avahi daemon:/:/sbin/nologin

hsqldb:x:96:96::/var/lib/hsqldb:/sbin/nologin

radiusd:x:95:95:radiusd user:/home/radiusd:/sbin/nologin

radiusd1:x:95:95:radiusd user:/home/radiusd:/sbin/nologin

tomcat:x:91:91:Tomcat:/usr/share/tomcat5:/bin/sh

named:x:25:25:Named:/var/named:/sbin/nologin

haldaemon:x:68:68:HAL daemon:/:/sbin/nologin

pegasus:x:66:65:tog-pegasus OpenPegasus WBEM/CIM services:/var/lib/Pegasus:/sbin/nologin

radiusd2:x:95:95:radiusd user:/home/radiusd:/sbin/nologin

xfs:x:43:43:X Font Server:/etc/X11/fs:/sbin/nologin

avahi-autoipd:x:101:105:avahi-autoipd:/var/lib/avahi-autoipd:/sbin/nologin

sabayon:x:86:86:Sabayon user:/home/sabayon:/sbin/nologin

gdm:x:42:42::/var/gdm:/sbin/nologin

kannel:x:424:424:Kannel WAP and SMS gateway:/var/lib/kannel:

wombat:x:8003:7525:Wombat:/dat/wombat:/bin/bash

mdops:x:8467:7525:MarketData Operations:/home/mdops:/bin/bash

nx:x:425:425::/usr/NX/home/nx:/usr/NX/bin/nxserver

 

note that the output contains neither the capital or lowercase version of the string exim

 

-------------------------------------------------------------------------------------------

I can combine the grep command with the output of all other UNIX commands, here is an example:

 

rp-lxdesk13:tmurphy/apps/learnlinux/examples$ ifconfig -a | grep eth

eth0      Link encap:Ethernet  HWaddr 00:24:E8:5A:57:C3 

eth1      Link encap:Ethernet  HWaddr 00:24:E8:5A:57:C5 

 

shows me all the Ethernet interfaces on a UNIX machine

 

 

rp-lxdesk13:tmurphy/apps/learnlinux/examples$ host tmurphy | grep alias

tmurphy.mlp.com is an alias for ny-lxdesk54.mlp.com.

 

shows me if tmurphy is a dns alias.

 

rp-lxdesk13:tmurphy/apps/learnlinux/examples$ dig www.google.com | grep Query

;; Query time: 1 msec

 

Shows me how log it takes to do a dns query

 

 

2-          the egrep command:

 

egrep is a lot like grep only that it supportes some regular expressions

 

rp-lxdesk13:tmurphy/apps/learnlinux/examples$ cat lesson3.txt | egrep 'wombat|mdops'

wombat:x:8003:7525:Wombat:/dat/wombat:/bin/bash

mdops:x:8467:7525:MarketData Operations:/home/mdops:/bin/bash

 

note that it greped multiple strings at once

 

 

 

 

 

 

Read http://www.uccs.edu/~ahitchco/grep/

 

 

 

3-          the LS Command:

read http://www.ee.surrey.ac.uk/Teaching/Unix/unix5.html

 

 

 

assignment:

1-    show me how to only get the line with apache from lesson3.txt

2-    use the cal command and show me all the output except the days of the week

3-    using the last command, show me only when the machine was rebooted

4-    do a long listing of the /etc directory and show me only files that contain the string passwd in their string