Grep È«ÃæËÑË÷ÕýÔò±í´ïʽ²¢°ÑÐдòÓ¡³öÀ´£¬ÊÇÒ»ÖÖÇ¿´óÊÇÎı¾ËÑË÷¹¤¾ß£¬ËüÄÜʹÓÃÕýÔò±í´ïʽËÑË÷Îı¾£¬²¢°ÑÆ¥ÅäµÄÐдòÓ¡³öÀ´¡£
Grep ÃüÁîÔÚ¶ÔÒ»¸ö»ò¶à¸öÎļþµÄÄÚÈݽøÐлùÓÚģʽµÄËÑË÷µÄÇé¿öÏÂÊǷdz£ÓÐÓõġ£Ä£Ê½¿ÉÒÔÊÇ
µ¥¸ö×Ö·û£¬¶à¸ö×Ö·û£¬µ¥¸öµ¥´Ê£¬»òÕßÊÇÒ»¸ö¾ä×Ó¡£
µ±ÃüÁîÆ¥Åäµ½Ö´ÐÐÃüÁîʱָ¶¨µÄģʽʱ£¬grep»á½«°üº¬Ä£Ê½Ò»ÐÐÊä³ö£¬µ«ÊDz¢²¢²»¶ÔÔÎļþÄÚÈݽøÐÐÐ޸ġ£
ÔÚÕâÀïÌÖÂÛ12¸ögrep ÃüÁîµÄ°¸Àý£º
1.ÔÚÎļþÖвéÕÒģʽ
ÔÚetc/passwdÎļþÖвéÕÒµ¥´Ê¡°root¡±
rep: /mnt/hgfs/link: ûÓÐÄǸöÎļþ»òĿ¼
[root@xyd ~]# grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
2.ÔÚ¶à¸öÎļþÖвéÕÒģʽ
[root@xyd ~]# grep root /etc/passwd /etc/gshadow
/etc/passwd:root:x:0:0:root:/root:/bin/bash
/etc/passwd:operator:x:11:0:operator:/root:/sbin/nologin
/etc/gshadow:root:::
/etc/gshadow:sfcb:!::root
3.ʹÓÃ-l²ÎÊýÁгö°üº¬Ö¸¶¨Ä£Ê½µÄÎļþµÄÎļþÃû
[root@xyd ~]# grep -l root /etc/passwd /etc/gshadow /etc/mtab
/etc/passwd
/etc/gshadow
/etc/mtab
4.ʹÓÃ-n²ÎÊý£¬ÔÚÎļþÖвéÕÒÖ¸¶¨Ä£Ê½²¢ÏÔʾƥÅäÐкÅ
[root@xyd ~]# grep -n root /etc/passwd /etc/shadow
/etc/passwd:1:root:x:0:0:root:/root:/bin/bash
/etc/passwd:11:operator:x:11:0:operator:/root:/sbin/nologin
/etc/shadow:1:root:$6$R2fpvQrgWHngCHNb$E0i7BtlE1tHvj6YkdaXy5SoKkArTfto7fOHg0AEMC66auei3XNx9AZR0Zjd1fCAGX2AClk606V.Z2ydWpI.xS0:17289:0:99999:7:::5.
5.ʹÓÃ-v²ÎÊýÊä³ö²»°üº¬Ö¸¶¨Ä£Ê½µÄÐÐ
[root@xyd ~]# grep -v linuxtechi /etc/passwd
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
t:x:0:0:root:/root:/bin/bash
6.ʹÓÃ$·ûºÅÊä³öËùÓÐÒÑÖ¸¶¨Ä£Ê½½áβµÄÐÐ
Êä³ö/etc/passwd ÎļþÖÐËùÓÐÒÔbash ½áβµÄÐÐ
[root@xyd ~]# grep bash$ /etc/passwd
root:x:0:0:root:/root:/bin/bash
amandabackup:x:33:6:Amanda user:/var/lib/amanda:/bin/bash
mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash
postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash
user:x:500:501:user:/home/user:/bin/bash
edu:x:501:502::/home/edu:/bin/bash
7.bash ½Å±¾½«ÃÀÔª£¨$£©·ûºÅÊÓ×÷ÌØÊâ×Ö·û£¬ÓÃÓÚÖ¸¶¨Ò»ÐлòÕßÒ»¸öµ¥´ÊµÄ½áβ¡£
ʹÓÃ-r ²ÎÊýµÝ¹éµØ²éÕÒÌض¨Ä£Ê½
root@Linux-world:~# grep -r linuxtechi /etc/
/etc/subuid:linuxtechi:100000:65536
/etc/group:adm:x:4:syslog,linuxtechi
/etc/group:cdrom:x:24:linuxtechi
/etc/group:sudo:x:27:linuxtechi
/etc/group:dip:x:30:linuxtechi
8.ʹÓÃgrep ²éÕÒÎļþÖÐËùÓпÕÐÐ
[root@xyd ~]# grep ^$ /etc/shadow
[root@xyd ~]#
9.ÓÉÓÚ/etc/shadowÎļþÖÐûÓпÕÐУ¬ËùÒÔûÓÐÈκÎÊä³ö¡£
ʹÓÃ-i²ÎÊý²éÕÒģʽ
Grep ÃüÁîµÄ-I ²ÎÊýÔÚ²éÕÒʱºöÂÔ×Ö·ûµÄ´óСд
[root@xyd ~]# grep -i ROOt /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@xyd ~]#
10.ʹÓÃ-e ²ÎÊý²éÕÒ¶à¸öģʽ
operator:x:11:0:operator:/root:/sbin/nologin
[root@xyd ~]# grep -e "root" -e "linuxtechi" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
11.Êä³öÆ¥ÅäÖ¸¶¨Ä£Ê½ÐеÄÇ°»òÕߺóÃæNÐÐ
ʹÓÃ-B²ÎÊýÊä³öÆ¥ÅäÐÐÊÇÇ°4ÐÐ
[root@xyd ~]# grep -B 4 "games" /etc/passwd
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
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
12.ʹÓÃ-A ²ÎÊýÊä³öÆ¥ÅäÐеĺó4ÐÐ
root@xyd ~]# grep -A 4 "games" /etc/passwd
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
dbus:x:81:81:System message bus:/:/sbin/nologin
13.ʹÓÃ-C Êä³ö²ÎÊýµÄÇ°ºó¸÷4ÐÐ
[root@xyd ~]# grep -C 4 "games" /etc/passwd
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
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
dbus:x:81:81:System message bus:/:/sbin/nologin