5. ʹÓÃתÒåÐòÁÐÀ´¹ÜÀí½Ï³¤µÄÊäÈë
Äú»òÐí¿´µ½¹ýʹÓ÷´Ð±¸Ü (\) À´½«½Ï³¤µÄÐÐÑÓÐøµ½ÏÂÒ»ÐеĴúÂëʾÀý£¬²¢ÇÒÄúÖªµÀ´ó¶àÊý 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 ÖУ¬µ±Äú°´ÏòÉϼýÍ·¼üʱ£¬Õû¸ö¶àÐÐÊäÈ뽫Öػ浽µ¥¸ö³¤ÊäÈëÐÐÉÏ¡£
6. ÔÚÁбíÖжÔÃüÁî·Ö×é
´ó¶àÊý Shell ¶¼¾ßÓÐÔÚÁбíÖжÔÃüÁî·Ö×éµÄ·½·¨£¬ÒÔ±ãÄúÄܽ«ËüÃǵĺϼÆÊä³öÏòÏ´«µÝµ½Ä³¸ö¹ÜµÀ£¬»òÕß½«ÆäÈκβ¿·Ö»òÈ«²¿Á÷Öض¨Ïòµ½ÏàͬµÄµØ·½¡£ÄúÒ»°ã¿ÉÒÔͨ¹ýÔÚij¸ö Subshell ÖÐÔËÐÐÒ»¸öÃüÁîÁбí»òͨ¹ýÔÚµ±Ç° Shell ÖÐÔËÐÐÒ»¸öÃüÁîÁбíÀ´ÊµÏÖ´ËÄ¿µÄ¡£
6.1 ÔÚ 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 ¸ü¿ÉÈ¡¡£
6.2 ÔÚµ±Ç° Shell ÖÐÔËÐÐÃüÁîÁбí
½«ÃüÁîÁбíÓôóÀ¨ºÅ ({}) À¨ÆðÀ´£¬ÒÔÔÚµ±Ç° Shell ÖÐÔËÐС£È·±£ÔÚÀ¨ºÅÓëʵ¼ÊÃüÁîÖ®¼ä°üÀ¨¿Õ¸ñ£¬·ñÔò Shell ¿ÉÄÜÎÞ·¨ÕýÈ·½âÊÍÀ¨ºÅ¡£´ËÍ⣬»¹ÒªÈ·±£ÁбíÖеÄ×îºóÒ»¸öÃüÁîÒԷֺŽá⣬ÈçÒÔÏÂʾÀýËùʾ£º
Çåµ¥ 12. ºÃÏ°¹ß 6 µÄÁíÒ»¸öʾÀý£ºÔÚµ±Ç° Shell ÖÐÔËÐÐÃüÁîÁбí
~ $ { cp ${VAR}a . && chown -R guest.guest a && \
> tar cvf newarchive.tar a; } | mailx admin -S "New archive"
7. ÔÚ 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 µÄ¸¨Öú¹¤¾ß£»ËüÊÇÒ»¸öδµÃµ½³ä·ÖÀûÓõŤ¾ßÖ®Ò»£¬µ±ÄúÑø³ÉʹÓÃËüµÄÏ°¹ßʱ£¬½«»áÏ£Íû½øÐÐËùÓÐÊÔÑ飬°üÀ¨ÒÔÏÂÓ÷¨¡£
7.1 ´«µÝ¿Õ¸ñ·Ö¸ôµÄÁбí
ÔÚ×î¼òµ¥µÄµ÷ÓÃÐÎʽÖУ¬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 $
7.2 ½÷É÷ʹÓà xargs
´Ó¼¼ÊõÉϽ²£¬Ê¹Óà xargs ºÜÉÙÓöµ½Âé·³¡£È±Ê¡Çé¿öÏ£¬Îļþ½áÊø×Ö·û´®ÊÇÏ»®Ïß (_)£»Èç¹û½«¸Ã×Ö·û×÷Ϊµ¥¸öÊäÈë²ÎÊýÀ´·¢ËÍ£¬ÔòËüÖ®ºóµÄËùÓÐÄÚÈݽ«±»ºöÂÔ¡£ÎªÁË·ÀÖ¹ÕâÖÖÇé¿ö·¢Éú£¬¿ÉÒÔʹÓà -e ±êÖ¾£¬ËüÔÚ²»´ø²ÎÊýµÄÇé¿öÏÂÍêÈ«½ûÓýáÊø×Ö·û´®¡£
8. Á˽âºÎʱ 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£©À´¶ÔÐнøÐÐÆ¥ÅäºÍ¼ÆÊýÉÔ¿ìÒ»µã¡£
9. Æ¥ÅäÊä³öÖеÄ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 $
ÍƼöÐÅÏ¢
- linuxÃüÁîѧϰ±Ê¼Ç£¨11£©£ºnlÃüÁî
- linuxÃüÁîѧϰ±Ê¼Ç£¨5£©£ºrmÃüÁî
- linuxÃüÁîѧϰ±Ê¼Ç£¨4£©£ºmkdirÃüÁî
- linuxÃüÁîѧϰ±Ê¼Ç£¨1£©£ºlsÃüÁî
- ½«CentosµÄyumÔ´¸ü»»Îª¹úÄڵİ¢ÀïÔÆÔ´
- ʹÓÃNginxÌí¼Óheader·ÀÖ¹ÍøÒ³±»frame
- linuxϼÓËÙscp´«Êä´óÎļþµÄËÙ¶È
- linuxϵͳÉ϶ÔnginxÈÕÖ¾·Ö¸î´¦Àí
- lnmp/nginxϵͳÕæÕýÓÐЧµÄͼƬ·ÀµÁÁ´ÍêÕûÉèÖÃÏê½â
- Í»ÆÆÊ®Íò²¢·¢µÄNginxµÄÅäÖü°ÓÅ»¯
ÈÈÃÅÐÅÏ¢
- nohup: redirecting stderr to stdou....
- ʹÓÃlog_formatΪNginx·þÎñÆ÷ÉèÖøüÏêϸµÄÈÕÖ¾¸ñʽ
- jquery easyUI--dataGrid-Json
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- ÀûÓÃKeepalived+mysql¹¹½¨¸ß¿ÉÓÃMySQLË«Ö÷×Ô¶....
- Nginx+keepalivedʵÏÖ¸ºÔؾùºâºÍË«»úÈȱ¸¸ß¿ÉÓÃ
- jqueryʵÏÖÒ³Ãæ¼ÓÔؽø¶ÈÌõ
- Rolling cURL: PHP²¢·¢×î¼Ñʵ¼ù
- codeigniter ·ÓÉÖÕ¼«ÓÅ»¯(url rewrite)
- linuxÏÂÉèÖÃsshÎÞÃÜÂëµÇ¼
×î½ü¸üÐÂ
- ²éÕÒ²¢É¾³ý.svnĿ¼Îļþ
- redis ÆßÖÖÊý¾ÝÀàÐ͵ÄʹÓó¡¾°
- linux ÏÂÎļþ¸´ÖƵ½windowsÏÂÂÒÂëµÄ½â¾ö°ì·¨
- nginx³öÏÖ502 upstream sent too big he....
- linuxÏÂsudoÅäÖÃÏê½â
- linuxÃüÁîѧϰ±Ê¼Ç£¨15£©£ºtailÃüÁî
- linuxÃüÁîѧϰ±Ê¼Ç£¨14£©£ºheadÃüÁî
- linuxÃüÁîѧϰ±Ê¼Ç£¨13£©£ºlessÃüÁî
- linuxÃüÁîѧϰ±Ê¼Ç£¨12£©£ºmoreÃüÁî
- ¼ÓÃÜËã·¨±È½Ï3DES AES RSA ECC MD5 SHA1µÈ
ÆÀÂÛ