²ÉÓà 10 ¸öÄܹ»Ìá¸ßÄúµÄ UNIX? ÃüÁîÐÐЧÂʵĺÃÏ°¹ß----²¢Ôڴ˹ý³ÌÖаÚÍѲ»Á¼µÄʹÓÃģʽ¡£±¾ÎÄÑÐò½¥½øµØÖ¸µ¼Äúѧϰ¼¸ÏîÓÃÓÚÃüÁîÐвÙ×÷µÄ¼¼Êõ£¬ÕâЩ¼¼Êõ·Ç³£ºÃ£¬µ«ÊÇͨ³£±»ºöÂÔ¡£Á˽ⳣ¼û´íÎóºÍ¿Ë·þËüÃǵķ½·¨£¬ÒÔ±ãÄúÄܹ»È·ÇÐÁ˽âΪºÎÖµµÃ²ÉÓÃÕâЩ UNIX Ï°¹ß¡£
ÒýÑÔ
µ±Äú¾³£Ê¹ÓÃij¸öϵͳʱ£¬ÍùÍù»áÏÝÈëijÖ̶ֹ¨µÄʹÓÃģʽ¡£ÓÐʱ£¬ÄúûÓÐÑø³ÉÒÔ¾¡¿ÉÄÜ×îºÃµÄ·½Ê½×öʵÄÏ°¹ß¡£ÓÐʱ£¬ÄúµÄ²»Á¼Ï°¹ßÉõÖÁ»áµ¼Ö³öÏÖ»ìÂÒ¡£¾ÀÕý´ËÀàȱµãµÄ×î¼Ñ·½·¨Ö®Ò»£¬¾ÍÊÇÓÐÒâʶµØ²ÉÓõÖÖÆÕâЩ»µÏ°¹ßµÄºÃÏ°¹ß¡£±¾ÎÄÌá³öÁË 10 ¸öÖµµÃ²ÉÓÃµÄ UNIX ÃüÁîÐÐÏ°¹ß----°ïÖúÄú¿Ë·þÐí¶à³£¼ûʹÓùÖñ±£¬²¢Ôڸùý³ÌÖÐÌá¸ßÃüÁîÐй¤×÷ЧÂʵĺÃÏ°¹ß¡£ÏÂÃæÁгöÁËÕâ 10 ¸öºÃÏ°¹ß£¬Ö®ºó¶Ô½øÐÐÁ˸üÏêϸµÄÃèÊö¡£
²ÉÓà 10 ¸öºÃÏ°¹ß
ÔÚµ¥¸öÃüÁîÖд´½¨Ä¿Â¼Ê÷
Çåµ¥ 1 ÑÝʾÁË×î³£¼ûµÄ UNIX »µÏ°¹ßÖ®Ò»£ºÒ»´Î¶¨ÒåÒ»¸öĿ¼Ê÷¡£
Çåµ¥ 1. »µÏ°¹ß 1 µÄʾÀý£ºµ¥¶À¶¨Òåÿ¸öĿ¼Ê÷
~ $ mkdir tmp
~ $ cd tmp
~/tmp $ mkdir a
~/tmp $ cd a
~/tmp/a $ mkdir b
~/tmp/a $ cd b
~/tmp/a/b/ $ mkdir c
~/tmp/a/b/ $ cd c
~/tmp/a/b/c $
ʹÓà mkdir µÄ -p Ñ¡Ïî²¢ÔÚµ¥¸öÃüÁîÖд´½¨ËùÓи¸Ä¿Â¼¼°Æä×ÓĿ¼ҪÈÝÒ׵öࡣµ«ÊǼ´Ê¹¶ÔÓÚÖªµÀ´ËÑ¡ÏîµÄ¹ÜÀíÔ±£¬ËûÃÇÔÚÃüÁîÐÐÉÏ´´½¨×ÓĿ¼ʱҲÈÔÈ»Êø¸¿ÓÚÖð²½´´½¨Ã¿¼¶×ÓĿ¼¡£»¨Ê±¼äÓÐÒâʶµØÑø³ÉÕâ¸öºÃÏ°¹ßÊÇÖµµÃµÄ£º
Çåµ¥ 2. ºÃÏ°¹ß 1 µÄʾÀý£ºÊ¹ÓÃÒ»¸öÃüÁîÀ´¶¨ÒåĿ¼Ê÷
~ $ mkdir -p tmp/a/b/c
Äú¿ÉÒÔʹÓôËÑ¡ÏîÀ´´´½¨Õû¸ö¸´ÔÓµÄĿ¼Ê÷£¨Ôڽű¾ÖÐʹÓÃÊǷdz£ÀíÏëµÄ£©£¬¶ø²»Ö»ÊÇ´´½¨¼òµ¥µÄ²ã´Î½á¹¹¡£ÀýÈ磺
Çåµ¥ 3. ºÃÏ°¹ß 1 µÄÁíÒ»¸öʾÀý£ºÊ¹ÓÃÒ»¸öÃüÁîÀ´¶¨Ò帴ÔÓµÄĿ¼Ê÷
~ $ mkdir -p project/{lib/ext,bin,src,doc/{html,info,pdf},demo/stat/a}
¹ýÈ¥£¬µ¥¶À¶¨ÒåĿ¼µÄΨһ½è¿ÚÊÇÄúµÄ mkdir ʵÏÖ²»Ö§³Ö´ËÑ¡Ïµ«ÊÇÔÚ´ó¶àÊýϵͳÉϲ»ÔÙÊÇÕâÑùÁË¡£IBM¡¢AIX?¡¢mkdir¡¢GNU mkdir ºÍÆäËû×ñÊص¥Ò» UNIX ¹æ·¶ (Single UNIX Specification) µÄϵͳÏÖÔÚ¶¼¾ßÓдËÑ¡Ïî¡£¶ÔÓÚÈÔȻȱ·¦¸Ã¹¦ÄܵÄÉÙÊýϵͳ£¬Äú¿ÉÒÔʹÓà mkdirhier ½Å±¾£¨Çë²Î¼û²Î¿¼×ÊÁÏ£©£¬´Ë½Å±¾ÊÇÖ´ÐÐÏàͬ¹¦ÄÜµÄ mkdir µÄ°ü×°£º
~ $ mkdirhier project/{lib/ext,bin,src,doc/{html,info,pdf},demo/stat/a}
¸ü¸Ä·¾¶£»²»ÒªÒƶ¯´æµµ
ÁíÒ»¸ö²»Á¼µÄʹÓÃģʽÊǽ« .tar ´æµµÎļþÒƶ¯µ½Ä³¸öĿ¼£¬ÒòΪ¸ÃĿ¼ǡºÃÊÇÄúÏ£ÍûÔÚÆäÖÐÌáÈ¡ .tar ÎļþµÄĿ¼¡£ÆäʵÄú¸ù±¾²»ÐèÒªÕâÑù×ö¡£Äú¿ÉÒÔËæÐÄËùÓûµØ½«ÈκΠ.tar ´æµµÎļþ½âѹËõµ½ÈκÎĿ¼----Õâ¾ÍÊÇ -C Ñ¡ÏîµÄÓÃ;¡£ÔÚ½âѹËõij¸ö´æµµÎļþʱ£¬Ê¹Óà -C Ñ¡ÏîÀ´Ö¸¶¨ÒªÔÚÆäÖнâѹËõ¸ÃÎļþµÄĿ¼£º
Çåµ¥ 4. ºÃÏ°¹ß 2 µÄʾÀý£ºÊ¹ÓÃÑ¡Ïî -C À´½âѹËõ .tar ´æµµÎļþ
~ $ tar xvf -C tmp/a/b/c newarc.tar.gz
Ïà¶ÔÓÚ½«´æµµÎļþÒƶ¯µ½ÄúÏ£ÍûÔÚÆäÖнâѹËõËüµÄλÖã¬Çл»µ½¸ÃĿ¼£¬È»ºó²Å½âѹËõËü£¬Ñø³ÉʹÓà -C µÄÏ°¹ßÔò¸ü¼Ó¿ÉÈ¡----µ±´æµµÎļþλÓÚÆäËûij¸öλÖÃʱÓÈÆäÈç´Ë¡£
½«ÃüÁîÓë¿ØÖƲÙ×÷·û×éºÏʹÓÃ
Äú¿ÉÄÜÒѾ֪µÀ£¬ÔÚ´ó¶àÊý Shell ÖУ¬Äú¿ÉÒÔÔÚµ¥¸öÃüÁîÐÐÉÏͨ¹ýÔÚÃüÁîÖ®¼ä·ÅÖÃÒ»¸ö·ÖºÅ (;) À´×éºÏÃüÁî¡£¸Ã·ÖºÅÊÇ Shell ¿ØÖƲÙ×÷·û£¬ËäÈ»Ëü¶ÔÓÚÔÚµ¥¸öÃüÁîÐÐÉϽ«ÀëÉ¢µÄÃüÁî´®ÁªÆðÀ´ºÜÓÐÓ㬵«Ëü²¢²»ÊÊÓÃÓÚËùÓÐÇé¿ö¡£ÀýÈ磬¼ÙÉèÄúʹÓ÷ֺÅÀ´×éºÏÁ½¸öÃüÁÆäÖеڶþ¸öÃüÁîµÄÕýÈ·Ö´ÐÐÍêÈ«ÒÀÀµÓÚµÚÒ»¸öÃüÁîµÄ³É¹¦Íê³É¡£Èç¹ûµÚÒ»¸öÃüÁîδ°´ÄúÔ¤ÆÚµÄÄÇÑùÍ˳ö£¬µÚ¶þ¸öÃüÁîÈÔÈ»»áÔËÐÐ----½á¹û»áµ¼ÖÂʧ°Ü¡£Ïà·´£¬Ó¦¸ÃʹÓøüÊʵ±µÄ¿ØÖƲÙ×÷·û£¨±¾ÎĽ«ÃèÊöÆäÖеIJ¿·Ö²Ù×÷·û£©¡£Ö»ÒªÄúµÄ Shell Ö§³ÖËüÃÇ£¬¾ÍÖµµÃÑø³ÉʹÓÃËüÃǵÄÏ°¹ß¡£½öµ±ÁíÒ»¸öÃüÁî·µ»ØÁãÍ˳ö״̬ʱ²ÅÔËÐÐij¸öÃüÁî
ʹÓà && ¿ØÖƲÙ×÷·ûÀ´×éºÏÁ½¸öÃüÁÒÔ±ã½öµ± µÚÒ»¸öÃüÁî·µ»ØÁãÍ˳ö״̬ʱ²ÅÔËÐеڶþ¸öÃüÁî¡£»»¾ä»°Ëµ£¬Èç¹ûµÚÒ»¸öÃüÁîÔËÐгɹ¦£¬ÔòµÚ¶þ¸öÃüÁÔËÐС£Èç¹ûµÚÒ»¸öÃüÁîʧ°Ü£¬ÔòµÚ¶þ¸öÃüÁî¸ù±¾¾Í²»ÔËÐС£ÀýÈ磺
Çåµ¥ 5. ºÃÏ°¹ß 3 µÄʾÀý£º½«ÃüÁîÓë¿ØÖƲÙ×÷·û×éºÏʹÓÃ
~ $ cd tmp/a/b/c && tar xvf ~/archive.tar
ÔÚ´ËÀýÖУ¬´æµµµÄÄÚÈݽ«ÌáÈ¡µ½ ~/tmp/a/b/c Ŀ¼ÖУ¬³ý·Ç¸ÃĿ¼²»´æÔÚ¡£Èç¹û¸ÃĿ¼²»´æÔÚ£¬Ôò tar ÃüÁî²»»áÔËÐУ¬Òò´Ë²»»áÌáÈ¡ÈκÎÄÚÈÝ¡£
½öµ±ÁíÒ»¸öÃüÁî·µ»Ø·ÇÁãÍ˳ö״̬ʱ²ÅÔËÐÐij¸öÃüÁîÀàËƵأ¬|| ¿ØÖƲÙ×÷·û·Ö¸ôÁ½¸öÃüÁ²¢ÇÒ½öµ±µÚÒ»¸öÃüÁî·µ»Ø·ÇÁãÍ˳ö״̬ʱ²ÅÔËÐеڶþ¸öÃüÁî¡£»»¾ä»°Ëµ£¬Èç¹ûµÚÒ»¸öÃüÁî³É¹¦£¬ÔòµÚ¶þ¸öÃüÁî²»»áÔËÐС£Èç¹ûµÚÒ»¸öÃüÁîʧ°Ü£¬ÔòµÚ¶þ¸öÃüÁî²Å»á ÔËÐС£ÔÚ²âÊÔij¸ö¸ø¶¨Ä¿Â¼ÊÇ·ñ´æÔÚʱ£¬Í¨³£Ê¹Óô˲Ù×÷·û£¬Èç¹û¸ÃĿ¼²»´æÔÚ£¬Ôò´´½¨Ëü£º
Çåµ¥ 6. ºÃÏ°¹ß 3 µÄÁíÒ»¸öʾÀý£º½«ÃüÁîÓë¿ØÖƲÙ×÷·û×éºÏʹÓÃ
~ $ cd tmp/a/b/c || mkdir -p tmp/a/b/c
Äú»¹¿ÉÒÔ×éºÏʹÓñ¾²¿·ÖÖÐÃèÊöµÄ¿ØÖƲÙ×÷·û¡£Ã¿¸ö²Ù×÷·û¶¼Ó°Ïì×îºóµÄÃüÁîÔËÐУº
Çåµ¥ 7. ºÃÏ°¹ß 3 µÄ×éºÏʾÀý£º½«ÃüÁîÓë¿ØÖƲÙ×÷·û×éºÏʹÓÃ
~ $ cd tmp/a/b/c || mkdir -p tmp/a/b/c && tar xvf -C tmp/a/b/c ~/archive.tar
½÷É÷ÒýÓñäÁ¿
ʼÖÕÒª½÷É÷ʹÓà Shell À©Õ¹ºÍ±äÁ¿Ãû³Æ¡£Ò»°ã×îºÃ½«±äÁ¿µ÷ÓðüÀ¨ÔÚË«ÒýºÅÖУ¬³ý·ÇÄúÓв»ÕâÑù×öµÄ×ã¹»ÀíÓÉ¡£ÀàËƵأ¬Èç¹ûÄúÖ±½ÓÔÚ×ÖĸÊý×ÖÎı¾ºóÃæʹÓñäÁ¿Ãû³Æ£¬Ôò»¹ÒªÈ·±£½«¸Ã±äÁ¿Ãû³Æ°üÀ¨ÔÚ·½À¨ºÅ ([]) ÖУ¬ÒÔʹÆäÓëÖÜΧµÄÎı¾Çø·Ö¿ªÀ´¡£·ñÔò£¬Shell ½«°ÑβËæÎı¾½âÊÍΪ±äÁ¿Ãû³ÆµÄÒ»²¿·Ö----²¢ÇҺܿÉÄÜ·µ»ØÒ»¸ö¿ÕÖµ¡£Çåµ¥ 8 ÌṩÁ˱äÁ¿µÄ¸÷ÖÖÒýÓúͷÇÒýÓü°ÆäÓ°ÏìµÄʾÀý¡£
Çåµ¥ 8. ºÃÏ°¹ß 4 µÄʾÀý£ºÒýÓ㨺ͷÇÒýÓ㩱äÁ¿
~ $ ls tmp/
a b
~ $ VAR="tmp/*"
~ $ echo $VAR
tmp/a tmp/b
~ $ echo "$VAR"
tmp/*
~ $ echo $VARa
~ $ echo "$VARa"
~ $ echo "${VAR}a"
tmp/*a
~ $ echo ${VAR}a
tmp/a
~ $
ʹÓÃתÒåÐòÁÐÀ´¹ÜÀí½Ï³¤µÄÊäÈë
Äú»òÐí¿´µ½¹ýʹÓ÷´Ð±¸Ü (\) À´½«½Ï³¤µÄÐÐÑÓÐøµ½ÏÂÒ»ÐеĴúÂëʾÀý£¬²¢ÇÒÄúÖªµÀ´ó¶àÊý Shell ¶¼½«Äúͨ¹ý·´Ð±¸ÜÁª½ÓµÄºóÐøÐÐÉϼüÈëµÄÄÚÈÝÊÓΪµ¥¸ö³¤ÐС£È»¶ø£¬Äú¿ÉÄÜûÓÐÔÚÃüÁîÐÐÖÐÏñͨ³£ÄÇÑùÀûÓô˹¦ÄÜ¡£Èç¹ûÄúµÄÖÕ¶ËÎÞ·¨ÕýÈ·´¦Àí¶àÐлØÈÆ£¬»òÕßÄúµÄÃüÁîÐбÈͨ³£Ð¡£¨ÀýÈçÔÚÌáʾ·ûÏÂÓг¤Â·¾µÄʱºò£©£¬·´Ð±¸Ü¾ÍÌرðÓÐÓ᣷´Ð±¸Ü¶ÔÓÚÁ˽â¼üÈëµÄ³¤ÊäÈëÐеĺ¬ÒåÒ²·Ç³£ÓÐÓã¬ÈçÒÔÏÂʾÀýËùʾ£º
Çåµ¥ 9. ºÃÏ°¹ß 5 µÄʾÀý£º½«·´Ð±¸ÜÓÃÓÚ³¤ÊäÈë
~ $ cd tmp/a/b/c || \
> mkdir -p tmp/a/b/c && \
> tar xvf -C tmp/a/b/c ~/archive.tar
»òÕߣ¬Ò²¿ÉÒÔʹÓÃÒÔÏÂÅäÖãº
Çåµ¥ 10. ºÃÏ°¹ß 5 µÄÌæ´úʾÀý£º½«·´Ð±¸ÜÓÃÓÚ³¤ÊäÈë
~ $ cd tmp/a/b/c \
> || \
> mkdir -p tmp/a/b/c \
> && \
> tar xvf -C tmp/a/b/c ~/archive.tar
È»¶ø£¬µ±Äú½«ÊäÈëÐл®·Öµ½¶àÐÐÉÏʱ£¬Shell ʼÖÕ½«ÆäÊÓΪµ¥¸öÁ¬ÐøµÄÐУ¬ÒòΪËü×ÜÊÇɾ³ýËùÓз´Ð±¸ÜºÍ¶îÍâµÄ¿Õ¸ñ¡£×¢Ò⣺ÔÚ´ó¶àÊý Shell ÖУ¬µ±Äú°´ÏòÉϼýÍ·¼üʱ£¬Õû¸ö¶àÐÐÊäÈ뽫Öػ浽µ¥¸ö³¤ÊäÈëÐÐÉÏ¡£
ÔÚÁбíÖжÔÃüÁî·Ö×é
´ó¶àÊý Shell ¶¼¾ßÓÐÔÚÁбíÖжÔÃüÁî·Ö×éµÄ·½·¨£¬ÒÔ±ãÄúÄܽ«ËüÃǵĺϼÆÊä³öÏòÏ´«µÝµ½Ä³¸ö¹ÜµÀ£¬»òÕß½«ÆäÈκβ¿·Ö»òÈ«²¿Á÷Öض¨Ïòµ½ÏàͬµÄµØ·½¡£ÄúÒ»°ã¿ÉÒÔͨ¹ýÔÚij¸ö Subshell ÖÐÔËÐÐÒ»¸öÃüÁîÁбí»òͨ¹ýÔÚµ±Ç° Shell ÖÐÔËÐÐÒ»¸öÃüÁîÁбíÀ´ÊµÏÖ´ËÄ¿µÄ¡£
ÔÚ Subshell ÖÐÔËÐÐÃüÁîÁбí
ʹÓÃÀ¨ºÅ½«ÃüÁîÁбí°üÀ¨ÔÚµ¥¸ö×éÖС£ÕâÑù×ö½«ÔÚÒ»¸öÐ嵀 Subshell ÖÐÔËÐÐÃüÁ²¢ÔÊÐíÄúÖض¨Ïò»òÊÕ¼¯Õû×éÃüÁîµÄÊä³ö£¬ÈçÒÔÏÂʾÀýËùʾ£º
Çåµ¥ 11. ºÃÏ°¹ß 6 µÄʾÀý£ºÔÚ Subshell ÖÐÔËÐÐÃüÁîÁбí
~ $ ( cd tmp/a/b/c/ || mkdir -p tmp/a/b/c && \
> VAR=$PWD; cd ~; tar xvf -C $VAR archive.tar ) \
> | mailx admin -S "Archive contents"
ÔÚ´ËʾÀýÖУ¬¸Ã´æµµµÄÄÚÈݽ«ÌáÈ¡µ½ tmp/a/b/c/ Ŀ¼ÖУ¬Í¬Ê±½«·Ö×éÃüÁîµÄÊä³ö£¨°üÀ¨ËùÌáÈ¡ÎļþµÄÁÐ±í£©Í¨¹ýÓʼþ·¢Ë͵½µØÖ· admin¡£
µ±ÄúÔÚÃüÁîÁбíÖÐÖØж¨Òå»·¾³±äÁ¿£¬²¢ÇÒÄú²»Ï£Íû½«ÄÇЩ¶¨ÒåÓ¦ÓÃÓÚµ±Ç° Shell ʱ£¬Ê¹Óà Subshell ¸ü¿ÉÈ¡¡£
ÔÚµ±Ç° Shell ÖÐÔËÐÐÃüÁîÁбí
½«ÃüÁîÁбíÓôóÀ¨ºÅ ({}) À¨ÆðÀ´£¬ÒÔÔÚµ±Ç° Shell ÖÐÔËÐС£È·±£ÔÚÀ¨ºÅÓëʵ¼ÊÃüÁîÖ®¼ä°üÀ¨¿Õ¸ñ£¬·ñÔò Shell ¿ÉÄÜÎÞ·¨ÕýÈ·½âÊÍÀ¨ºÅ¡£´ËÍ⣬»¹ÒªÈ·±£ÁбíÖеÄ×îºóÒ»¸öÃüÁîÒԷֺŽá⣬ÈçÒÔÏÂʾÀýËùʾ£º
Çåµ¥ 12. ºÃÏ°¹ß 6 µÄÁíÒ»¸öʾÀý£ºÔÚµ±Ç° Shell ÖÐÔËÐÐÃüÁîÁбí
~ $ { cp ${VAR}a . && chown -R guest.guest a && \
> tar cvf newarchive.tar a; } | mailx admin -S "New archive"
ÔÚ find Ö®ÍâʹÓà xargs
ʹÓà xargs ¹¤¾ß×÷ΪɸѡÆ÷£¬ÒÔ³ä·ÖÀûÓÃ´Ó find ÃüÁîÌôÑ¡µÄÊä³ö¡£find ÔËÐÐͨ³£ÌṩÓëijЩÌõ¼þÆ¥ÅäµÄÎļþÁÐ±í¡£´ËÁÐ±í±»´«µÝµ½ xargs ÉÏ£¬ºóÕßÈ»ºóʹÓøÃÎļþÁбí×÷Ϊ²ÎÊýÀ´ÔËÐÐÆäËûijЩÓÐÓõÄÃüÁÈçÒÔÏÂʾÀýËùʾ£º
Çåµ¥ 13. xargs ¹¤¾ßµÄ¾µäÓ÷¨Ê¾Àý
~ $ find some-file-criteria some-file-path | \
> xargs some-great-command-that-needs-filename-arguments
È»¶ø£¬²»Òª½« xargs ½ö¿´×÷ÊÇ find µÄ¸¨Öú¹¤¾ß£»ËüÊÇÒ»¸öδµÃµ½³ä·ÖÀûÓõŤ¾ßÖ®Ò»£¬µ±ÄúÑø³ÉʹÓÃËüµÄÏ°¹ßʱ£¬½«»áÏ£Íû½øÐÐËùÓÐÊÔÑ飬°üÀ¨ÒÔÏÂÓ÷¨¡£
´«µÝ¿Õ¸ñ·Ö¸ôµÄÁбí
ÔÚ×î¼òµ¥µÄµ÷ÓÃÐÎʽÖУ¬xargs ¾ÍÏñÒ»¸öɸѡÆ÷£¬Ëü½ÓÊÜÒ»¸öÁÐ±í£¨Ã¿¸ö³ÉÔ±·Ö±ðÔÚµ¥¶ÀµÄÐÐÉÏ£©×÷ΪÊäÈë¡£¸Ã¹¤¾ß½«ÄÇЩ³ÉÔ±·ÅÖÃÔÚµ¥¸ö¿Õ¸ñ·Ö¸ôµÄÐÐÉÏ£º
Çåµ¥ 14. xargs ¹¤¾ß²úÉúµÄÊä³öʾÀý
~ $ xargs
a
b
c
Control-D
a b c
~ $
Äú¿ÉÒÔ·¢ËÍͨ¹ý xargs À´Êä³öÎļþÃûµÄÈκι¤¾ßµÄÊä³ö£¬ÒÔ±ãΪÆäËûijЩ½ÓÊÜÎļþÃû×÷Ϊ²ÎÊýµÄ¹¤¾ß»ñµÃ²ÎÊýÁÐ±í£¬ÈçÒÔÏÂʾÀýËùʾ£º
Çåµ¥ 15. xargs ¹¤¾ßµÄʹÓÃʾÀý
~/tmp $ ls -1 | xargs
December_Report.pdf README a archive.tar mkdirhier.sh
~/tmp $ ls -1 | xargs file
December_Report.pdf: PDF document, version 1.3
README: ASCII text
a: directory
archive.tar: POSIX tar archive
mkdirhier.sh: Bourne shell script text executable
~/tmp $
xargs ÃüÁî²»Ö»ÓÃÓÚ´«µÝÎļþÃû¡£Äú»¹¿ÉÒÔÔÚÐèÒª½«Îı¾É¸Ñ¡µ½µ¥¸öÐÐÖеÄÈκÎʱºòʹÓÃËü£º
Çåµ¥ 16. ºÃÏ°¹ß 7 µÄʾÀý£ºÊ¹Óà xargs ¹¤¾ßÀ´½«Îı¾É¸Ñ¡µ½µ¥¸öÐÐÖÐ
~/tmp $ ls -l | xargs
-rw-r--r-- 7 joe joe 12043 Jan 27 20:36 December_Report.pdf -rw-r--r-- 1 \
root root 238 Dec 03 08:19 README drwxr-xr-x 38 joe joe 354082 Nov 02 \
16:07 a -rw-r--r-- 3 joe joe 5096 Dec 14 14:26 archive.tar -rwxr-xr-x 1 \
joe joe 3239 Sep 30 12:40 mkdirhier.sh
~/tmp $
½÷É÷ʹÓà xargs
´Ó¼¼ÊõÉϽ²£¬Ê¹Óà xargs ºÜÉÙÓöµ½Âé·³¡£È±Ê¡Çé¿öÏ£¬Îļþ½áÊø×Ö·û´®ÊÇÏ»®Ïß (_)£»Èç¹û½«¸Ã×Ö·û×÷Ϊµ¥¸öÊäÈë²ÎÊýÀ´·¢ËÍ£¬ÔòËüÖ®ºóµÄËùÓÐÄÚÈݽ«±»ºöÂÔ¡£ÎªÁË·ÀÖ¹ÕâÖÖÇé¿ö·¢Éú£¬¿ÉÒÔʹÓà -e ±êÖ¾£¬ËüÔÚ²»´ø²ÎÊýµÄÇé¿öÏÂÍêÈ«½ûÓýáÊø×Ö·û´®¡£
Á˽âºÎʱ grep Ó¦¸ÃÖ´ÐмÆÊý----ºÎʱӦ¸ÃÈƹý
±ÜÃâͨ¹ý¹ÜµÀ½« grep ·¢Ë͵½ wc -l À´¶ÔÊä³öÐÐÊý¼ÆÊý¡£grep µÄ -c Ñ¡ÏîÌṩÁ˶ÔÓëÌض¨Ä£Ê½Æ¥ÅäµÄÐеļÆÊý£¬²¢ÇÒÒ»°ãÒª±Èͨ¹ý¹ÜµÀ·¢Ë͵½ wc ¸ü¿ì£¬ÈçÒÔÏÂʾÀýËùʾ£º
Çåµ¥ 17. ºÃÏ°¹ß 8 µÄʾÀý£ºÊ¹ÓúͲ»Ê¹Óà grep µÄÐмÆÊý
~ $ time grep and tmp/a/longfile.txt | wc -l
2811
real 0m0.097s
user 0m0.006s
sys 0m0.032s
~ $ time grep -c and tmp/a/longfile.txt
2811
real 0m0.013s
user 0m0.006s
sys 0m0.005s
~ $
³ýÁËËÙ¶ÈÒòËØÍ⣬-c Ñ¡ÏÊÇÖ´ÐмÆÊýµÄºÃ·½·¨¡£¶ÔÓÚ¶à¸öÎļþ£¬´ø -c Ñ¡ÏîµÄ grep ·µ»Øÿ¸öÎļþµÄµ¥¶À¼ÆÊý£¬Ã¿ÐÐÒ»¸ö¼ÆÊý£¬¶øÕë¶Ô wc µÄ¹ÜµÀÔòÌṩËùÓÐÎļþµÄ×éºÏ×ܼÆÊý¡£
È»¶ø£¬²»¹ÜÊÇ·ñ¿¼ÂÇËٶȣ¬´ËʾÀý¶¼±íÃ÷ÁËÁíÒ»¸öÒª±ÜÃâµØ³£¼û´íÎó¡£ÕâЩ¼ÆÊý·½·¨½öÌṩ°üº¬Æ¥ÅäģʽµÄÐÐÊý----Èç¹ûÄǾÍÊÇÄúÒª²éÕҵĽá¹û£¬ÕâûʲôÎÊÌâ¡£µ«ÊÇÔÚÐÐÖоßÓÐij¸öÌض¨Ä£Ê½µÄ¶à¸öʵÀýµÄÇé¿öÏ£¬ÕâЩ·½·¨ÎÞ·¨ÎªÄúÌṩʵ¼ÊÆ¥ÅäʵÀýÊýÁ¿ µÄÕæʵ¼ÆÊý¡£¹é¸ù½áµ×£¬ÈôÒª¶ÔʵÀý¼ÆÊý£¬Äú»¹ÊÇҪʹÓà wc À´¼ÆÊý¡£Ê×ÏÈ£¬Ê¹Óà -o Ñ¡ÏÈç¹ûÄúµÄ°æ±¾Ö§³ÖËüµÄ»°£©À´ÔËÐÐ grep ÃüÁî¡£´ËÑ¡Ïî½ö Êä³öÆ¥ÅäµÄģʽ£¬Ã¿ÐÐÒ»¸öģʽ£¬¶ø²»Êä³öÐб¾Éí¡£µ«ÊÇÄú²»Äܽ«ËüÓë -c Ñ¡Ïî½áºÏʹÓã¬Òò´ËҪʹÓà wc -l À´¶ÔÐмÆÊý£¬ÈçÒÔÏÂʾÀýËùʾ£º
Çåµ¥ 18. ºÃÏ°¹ß 8 µÄʾÀý£ºÊ¹Óà grep ¶ÔģʽʵÀý¼ÆÊý
~ $ grep -o and tmp/a/longfile.txt | wc -l
3402
~ $
ÔÚ´ËÀýÖУ¬µ÷Óà wc Òª±ÈµÚ¶þ´Îµ÷Óà grep ²¢²åÈëÒ»¸öÐéÄâģʽ£¨ÀýÈç grep -c£©À´¶ÔÐнøÐÐÆ¥ÅäºÍ¼ÆÊýÉÔ¿ìÒ»µã¡£
Æ¥ÅäÊä³öÖеÄijЩ×ֶΣ¬¶ø²»Ö»ÊǶÔÐнøÐÐÆ¥Åä
µ±ÄúֻϣÍûÆ¥ÅäÊä³öÐÐÖÐÌض¨×ֶΠÖеÄģʽʱ£¬ÖîÈç awk µÈ¹¤¾ßÒªÓÅÓÚ grep¡£ÏÂÃæ¾¹ý¼ò»¯µÄʾÀýÑÝʾÁËÈçºÎ½öÁгö 12 ÔÂÐ޸ĹýµÄÎļþ¡£
Çåµ¥ 19. »µÏ°¹ß 9 µÄʾÀý£ºÊ¹Óà grep À´²éÕÒÌض¨×Ö¶ÎÖеÄģʽ
~/tmp $ ls -l /tmp/a/b/c | grep Dec
-rw-r--r-- 7 joe joe 12043 Jan 27 20:36 December_Report.pdf
-rw-r--r-- 1 root root 238 Dec 03 08:19 README
-rw-r--r-- 3 joe joe 5096 Dec 14 14:26 archive.tar
~/tmp $
ÔÚ´ËʾÀýÖУ¬grep ¶ÔÐнøÐÐɸѡ£¬²¢Êä³öÆäÐÞ¸ÄÈÕÆÚºÍÃû³ÆÖдø Dec µÄËùÓÐÎļþ¡£Òò´Ë£¬ÖîÈç December_Report.pdf µÈÎļþÊÇÆ¥ÅäµÄ£¬¼´Ê¹Ëü×Ô´ÓÒ»Ô·ÝÒÔÀ´»¹Î´Ð޸Ĺý¡£Õâ¿ÉÄܲ»ÊÇÄúÏ£ÍûµÄ½á¹û¡£ÎªÁËÆ¥ÅäÌض¨×Ö¶ÎÖеÄģʽ£¬×îºÃʹÓà awk£¬ÆäÖеÄÒ»¸ö¹ØϵÔËËã·û¶ÔÈ·ÇеÄ×ֶνøÐÐÆ¥Å䣬ÈçÒÔÏÂʾÀýËùʾ£º
Çåµ¥ 20. ºÃÏ°¹ß 9 µÄʾÀý£ºÊ¹Óà awk À´²éÕÒÌض¨×Ö¶ÎÖеÄģʽ
~/tmp $ ls -l | awk '$6 == "Dec"'
-rw-r--r-- 3 joe joe 5096 Dec 14 14:26 archive.tar
-rw-r--r-- 1 root root 238 Dec 03 08:19 README
~/tmp $
ÓйØÈçºÎʹÓà awk µÄ¸ü¶àÏêϸÐÅÏ¢£¬Çë²Î¼û²Î¿¼×ÊÁÏ¡£
Í£Ö¹¶Ô cat ʹÓùܵÀ
grep µÄÒ»¸ö³£¼ûµÄ»ù±¾Ó÷¨´íÎóÊÇͨ¹ý¹ÜµÀ½« cat µÄÊä³ö·¢Ë͵½ grep ÒÔËÑË÷µ¥¸öÎļþµÄÄÚÈÝ¡£Õâ¾ø¶ÔÊDz»±ØÒªµÄ£¬´¿´âÊÇÀË·Ñʱ¼ä£¬ÒòΪÖîÈç grep ÕâÑùµÄ¹¤¾ß½ÓÊÜÎļþÃû×÷Ϊ²ÎÊý¡£Äú¸ù±¾²»ÐèÒªÔÚÕâÖÖÇé¿öÏÂʹÓà cat£¬ÈçÒÔÏÂʾÀýËùʾ£º
Çåµ¥ 21. ºÃÏ°¹ßºÍ»µÏ°¹ß 10 µÄʾÀý£ºÊ¹ÓôøºÍ²»´ø cat µÄ grep
~ $ time cat tmp/a/longfile.txt | grep and
2811
real 0m0.015s
user 0m0.003s
sys 0m0.013s
~ $ time grep and tmp/a/longfile.txt
2811
real 0m0.010s
user 0m0.006s
sys 0m0.004s
~ $
´Ë´íÎó´æÔÚÓÚÐí¶à¹¤¾ßÖС£ÓÉÓÚ´ó¶àÊý¹¤¾ß¶¼½ÓÊÜʹÓÃÁ¬×Ö·û (-) µÄ±ê×¼ÊäÈë×÷Ϊһ¸ö²ÎÊý£¬Òò´Ë¼´Ê¹Ê¹Óà cat À´·ÖÉ¢ stdin ÖеĶà¸öÎļþ£¬²ÎÊýҲͨ³£ÊÇÎÞЧµÄ¡£½öµ±ÄúʹÓôø¶à¸öɸѡѡÏîÖ®Ò»µÄ cat ʱ£¬²ÅÕæÕýÓбØÒªÔڹܵÀÇ°Ê×ÏÈÖ´ÐÐÁ¬½Ó¡£
½áÊøÓÑø³ÉºÃÏ°¹ß
×îºÃ¼ì²éÒ»ÏÂÄúµÄÃüÁîÐÐÏ°¹ßÖеÄÈκβ»Á¼µÄʹÓÃģʽ¡£²»Á¼µÄʹÓÃģʽ»á½µµÍÄúµÄËٶȣ¬²¢ÇÒͨ³£»áµ¼ÖÂÒâÍâ´íÎó¡£±¾ÎĽéÉÜÁË 10 ¸öÐÂÏ°¹ß£¬ËüÃÇ¿ÉÒÔ°ïÖúÄú°ÚÍÑÐí¶à×î³£¼ûµÄʹÓôíÎó¡£Ñø³ÉÕâЩºÃÏ°¹ßÊǼÓÇ¿ÄúµÄ UNIX ÃüÁîÐм¼ÄܵĻý¼«²½Öè¡£
0.0.0.* ÓÚ 2007-08-15 05:12:27·¢±í:
Nice site. Thank you!
0.0.0.* ÓÚ 2007-08-12 22:49:03·¢±í:
Ö§³ÖÂ¥Ö÷
166.111.139.* ÓÚ 2007-08-12 22:47:58·¢±í:
ºÃÎÄ
222.151.211.* ÓÚ 2007-08-12 22:47:53·¢±í:
rosxjgid lpycehvx jewazry takgpimr pztnxkys cybheopr gtas
leitter ÓÚ 2007-07-17 10:34:43·¢±í:
·Ç³£ºÃÏ°¹ß