ÔõÑùͨ¹ýLinux¹¤¾ß¶Ô´ó¹æÄ£ÈÕÖ¾¾ÙÐÐÆÊÎö£¿
ÔõÑùͨ¹ýlinux¹¤¾ß¶Ô´ó¹æÄ£ÈÕÖ¾¾ÙÐÐÆÊÎö£¿
Åä¾°£º
ÔÚÏÖ´ú»¥ÁªÍøÓ¦ÓÃÖУ¬±¬·¢´ó×ÚµÄÈÕÖ¾Êý¾ÝÊǺܳ£¼ûµÄ¡£ÕâЩÈÕÖ¾Êý¾Ý°üÀ¨ÁËÓ¦ÓóÌÐòµÄÔËÐÐ״̬¡¢¹ýʧÐÅÏ¢¡¢»á¼û¼Í¼µÈÖ÷ÒªÐÅÏ¢¡£¹ØÓÚϵͳÖÎÀíÔ±»ò¿ª·¢Ö°Ô±À´Ëµ£¬ÔõÑù¸ßЧµØ¶ÔÕâЩ´ó¹æÄ£µÄÈÕÖ¾¾ÙÐÐÆÊÎöºÍÌáÈ¡ÓÐמÙ×ãÇáÖصÄÒâÒå¡£±¾ÎĽ«ÏÈÈÝһЩ³£ÓõÄLinux¹¤¾ß£¬²¢ÌṩʾÀý´úÂ룬×ÊÖú¶ÁÕ߸üºÃµØÃ÷È·ºÍÕÆÎÕÔõÑùÔÚLinuxƽ̨É϶Դó¹æÄ£ÈÕÖ¾¾ÙÐÐÆÊÎö¡£
Ò»¡¢ grep
grepÊÇLinuxÖзǾ³£ÓõÄÏÂÁîÐй¤¾ß£¬ÓÃÓÚÔÚÎı¾ÖвéÕÒÆ¥ÅäµÄ×Ö·û´®¡£ËüµÄ»ù±¾Ó÷¨ÈçÏ£º
grep "Òªº¦×Ö" ÎļþÃû
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬Òªº¦×ÖÊÇÄãÒª²éÕÒµÄÄÚÈÝ£¬ÎļþÃûÊÇÄãÒªËÑË÷µÄÎļþÃû¡£
ʾÀý´úÂ룺
¼ÙÉèÎÒÃÇÓÐÒ»¸ö°üÀ¨ÁË´ó×Ú»á¼û¼Í¼µÄÈÕÖ¾Îļþaccess.log£¬ÎÒÃÇ¿ÉÒÔͨ¹ýÏÂÃæµÄÏÂÁîÀ´²éÕÒÆäÖаüÀ¨Ìض¨Òªº¦×ֵļͼ£º
grep "404" access.log
µÇ¼ºó¸´ÖÆ
Õâ»á·µ»ØËùÓаüÀ¨Òªº¦×Ö”404″µÄ¼Í¼¡£
¶þ¡¢ awk
awkÊÇÒ»ÖÖÇ¿Ê¢µÄÎı¾´¦Öóͷ£¹¤¾ß£¬¿ÉÒÔ¶ÔÎı¾¾ÙÐÐÖݪֲÙ×÷¡£ËüµÄ»ù±¾Ó÷¨ÈçÏ£º
awk '{pattern + action}' ÎļþÃû
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬patternÊÇÄãҪƥÅäµÄģʽ£¬actionÊǶÔÆ¥Åäµ½µÄÎı¾Ö´ÐеIJÙ×÷¡£
ʾÀý´úÂ룺
¼ÙÉèÎÒÃÇÐèÒªÌáÈ¡³öaccess.logÖеÄIPµØµãºÍ»á¼ûʱ¼ä£¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔϵÄawkÏÂÁ
awk '{print $1, $4}' access.log
µÇ¼ºó¸´ÖÆ
Õâ»á·µ»ØÒ»¸öÒÔ¿Õ¸ñÍÑÀëµÄIPµØµãºÍ»á¼ûʱ¼äµÄÁÐ±í¡£
Èý¡¢ sed
sedÊÇÒ»¸öÁ÷±à¼Æ÷£¬ÓÃÓÚ¶ÔÎı¾¾ÙÐÐÖݪֲÙ×÷¡£ËüµÄ»ù±¾Ó÷¨ÈçÏ£º
sed 's/Æ¥Åä×Ö·û´®/Ìæ»»×Ö·û´®/g' ÎļþÃû
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬Æ¥Åä×Ö·û´®ÊÇÄãÒªÌæ»»µÄÎı¾£¬Ìæ»»×Ö·û´®ÊÇÄãÒªÌæ»»³ÉµÄÎı¾¡£
ʾÀý´úÂ룺
¼ÙÉèÎÒÃÇÐèÒª½«access.logÖеÄËùÓÐIPµØµãÌ滻Ϊ”xxx.xxx.xxx.xxx”£¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔϵÄsedÏÂÁ
sed 's/[0-9]+.[0-9]+.[0-9]+.[0-9]+/xxx.xxx.xxx.xxx/g' access.log
µÇ¼ºó¸´ÖÆ
Õâ»á½«ËùÓÐIPµØµãÌ滻Ϊ”xxx.xxx.xxx.xxx”¡£
ËÄ¡¢ sort
sortÊÇÒ»¸öÓÃÓÚÅÅÐòÎı¾µÄ¹¤¾ß£¬ËüµÄ»ù±¾Ó÷¨ÈçÏ£º
sort ÎļþÃû
µÇ¼ºó¸´ÖÆ
ʾÀý´úÂ룺
¼ÙÉèÎÒÃÇÓÐÒ»¸ö°üÀ¨ÁË´ó×ÚÊý×ÖµÄÎļþnumbers.txt£¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔϵÄsortÏÂÁî¶ÔÆä¾ÙÐÐÅÅÐò£º
sort numbers.txt
µÇ¼ºó¸´ÖÆ
Õâ»á·µ»ØÒ»¸öƾ֤Êý×ÖÉýÐòÅÅÁеÄÁÐ±í¡£
Îå¡¢ headºÍtail
headºÍtailÊÇÓÃÓÚÌáÈ¡ÎļþµÄÇ°nÐкͺónÐеŤ¾ß£¬ËüÃǵĻù±¾Ó÷¨ÈçÏ£º
head -n ÎļþÃû tail -n ÎļþÃû
µÇ¼ºó¸´ÖÆ
ʾÀý´úÂ룺
¼ÙÉèÎÒÃÇÐèÒªÉó²éaccess.logµÄÇ°10Ðкͺó10ÐУ¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔϵÄheadºÍtailÏÂÁ
head -n 10 access.log tail -n 10 access.log
µÇ¼ºó¸´ÖÆ
Õâ»á·µ»Øaccess.logµÄÇ°10Ðкͺó10ÐС£
×ÛÉÏËùÊö£¬Í¨¹ýÉÏÊöÏÈÈݵÄgrep¡¢awk¡¢sed¡¢sort¡¢headºÍtailµÈLinux¹¤¾ß£¬ÎÒÃÇ¿ÉÒÔÀû±ãµØ¶Ô´ó¹æÄ£ÈÕÖ¾¾ÙÐÐÆÊÎöºÍÌáÈ¡¡£ËäÈ»£¬Linuxƽ̨»¹ÓµÓиü¶àÀàËƹ¤¾ßºÍÏÂÁ¶ÁÕß¿ÉÒÔͨ¹ý½øÒ»²½µÄÑо¿ºÍʵ¼ù£¬¾ò¿Í¸ü¶àÇ¿Ê¢µÄ¹¦Ð§£¬Ìá¸ßÈÕÖ¾ÆÊÎöµÄЧÂʺÍ׼ȷÐÔ¡£Ï£Íû±¾ÎĶԶÁÕßÓÐËù×ÊÖú£¡
ÒÔÉϾÍÊÇÔõÑùͨ¹ýLinux¹¤¾ß¶Ô´ó¹æÄ£ÈÕÖ¾¾ÙÐÐÆÊÎö£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡