Ѹ

http://openlab.dino.co.jp/2008/02/20/133431188.html

ȥ̤ǤxargsޥҲ𤹤뵭񤤤Ƥߤޤxargsϻ䤬繥ޥΰĤǡɸϤϤ줿ե̾ʤɤȤߤʤơ̤ޥΰȤƵưȤΤǤ


㤨findǸĤե硢xargsʤǤⲼΤ褦˽񤯤ȤǤޤ

$ rm `find . -name \*~`

ǤϥҥåȤե¿ˡΤ褦ʥ顼Фߤ뤳Ȥޤ

-bash: /bin/rm: Argument list too long

ʤϰռʤȻפޤɡ¤ޥ¹ԤݤΰХȿˤϾ¤ޤΥ顼åϡθ³ĶȤ̣ǤĹfind¹Ԥ˾Τ褦ʥ顼Фߤڤʤˤʤޤ͡

ǡƱŪǤвΤ褦ˤƤãǤޤ

$ find . -name \*~ -exec rm {} \;~

100000ե뤬ҥåȤrmޥ100000¹ԤƤޤᡢ¹ԤޤǤ˰Ƴ֤롢ʤƤȤޤOSץưλ륳ȤŪ⤤Τǡºݤ˥եäƤ֤ûΤ˥ץ100000ưλΤ˻֤롢ȤȤޤ

쵤rmˤϥե¿뤱ɡ1ĤŤrmƤ֤롢ɤΤǤⶦ̤ǺߤǤ͡Ȥ󡢤ʤȤxargsפʤǤ

$ find . -name \*~ | xargs rm

xargsȤä˾2ĤȤɤ㤦Ȥȡޥ¹Իΰ¤򵤤ˤĤġrm˥ꥮθĿΥե̾Ϥơɬ׺¤β¹ԤƤޤ100000ե쵤ˤrmǤʤǤ⡢㤨25000եŤĤʬ4rmƤޤ¿ĺ®Ȥ櫓ǡxargsǤ͡

ʾ֥쥷ԡפȤƤޤȤȲΤ褦ˤʤޤ

쥷1ȥǥ쥯ȥʲˤ롢Ǹ1ʸ~פǤƤΥեls -laפ롣

$ find . -name \*~ | xargs ls -la
-rw-rw-r--  1 hanawa hanawa 29 Feb 19 18:23 ./c/test.c~
-rw-rw-r--  1 hanawa hanawa  9 Feb 19 18:21 ./hogehoge.txt~

ʤߤˡΥ쥷ԤϽʬŪǤxargsѤrmΤ褦ʼ֤ΤĤʤޥ򤤤ʤ¹ԤΤݤΤǡxargs rmפľˡxargs ls -laפʤɤоݥեǧƤХߥޤ󡣴̤Υե뤬ls -laפƤΤǧǡΥҥȥԽơls -laפrmפ˽񤭴Ƽ¹Ԥ櫓Ǥ

ޤե̾򰷤Ȥ
Τ褦xargsʤΤǤޤʸ򰷤ȤȺ뤳Ȥޤ

$ find . -name \*~ | xargs rm

㤨СHello World.php~פȤե뤬˾嵭ޥ¹ԤȲΤ褦ʥ顼ФƤޤޤ

rm: cannot remove `./Hello: No such file or directory
rm: cannot remove `World.php~: No such file or directory

xargsޥɸǶʸʲԡڡ֤ʤɡˤڤʸȸʤޤΤ褦˶ʸޤե뤬ȡ./HelloפȡWorld.php~פΤ褦˥ե̾2ʬ䤷rmޥϤƤޤᡢ̤˽ޤ󡣤褹ˡޤ

$ find . -name \*~ -print0 | xargs -0 rm

ȡfindνϤ̥ʸ000̤ɽ򤹤ȡ\0ˤǶڤƽϤޤޤxargs̥ʸǶڤƤΤȤƽԤޤη̡ʸޤե̾˽Ǥޤ

äѤxargsäǤ͡

쥷2ȥǥ쥯ȥʲˤ롢Ǹ1ʸ~פǤƤΥեls -laפ롣Hello World.php~פΤ褦ʡʸޤե뤬¸ߤ롣

$ find . -name \*~ -print0 | xargs -0 ls -la
-rw-rw-r--  1 hanawa hanawa 29 Feb 19 18:23 ./c/test.c~
-rw-rw-r--  1 hanawa hanawa  9 Feb 19 18:21 ./hogehoge.txt~
-rw-rw-r--  1 hanawa hanawa 20 Feb 19 18:24 ./php/Hello World.php~
  • xargsmv¹ԤȤ
    ơfindǸĤեmv/var/tmp˰ưʤƾͤޤ¹ԤޥrmäxargsʤǤmvξϺǸΰ˰ưΥǥ쥯ȥ̾ɬפꡢxargsǤϻȤʤȻפǤ͡ʤȤϤפäƤޤ

    ¤ϡʤȤΤfind-execץΤ褦ʵˡxargsˤѰդƤޤ

    $ find . -name \*~ -print0 | xargs -0 -I{} mv {} /var/tmp

ǽ뤳ȤϽ褿櫓Ǥޤ򤷤ޤ󡣤ȤΤ⡢1եŤļ¹ԤƤޤΤǡxargsȤ夬ޤ󡣤Ǥfind-execǻꤷ䤹ɤȻפޤ

㤢ɤФǤ礦¤ϡGNU coreutilscpmvˤxargsȤ߹碌ƻȤΥץѰդƤޤcoreutilsinfoˤ񤤤Ƥޤ-tפޤϡ--target-directoryץץdestinationΥǥ쥯ȥǤޤ

$ find . -name \*~ -print0 | xargs -0 mv -t /var/tmp


ޤǤ⤢ޤ󤬡Ǥϥǥ쥯ȥ깽¤ݻեΤ߰ưޤƱ̾Υե뤬ȴ̤ˤưޤΤդɬפǤˤޤtarоݥե򥢡֤ƤŪΥǥ쥯ȥŸ˾˶ᤤ⤷ޤʤξxargsȤ櫓Ǥˡ


ȥå   Խ ʬ ʎގ̎ ź ʣ ̾ѹ   ǽ   إ   ǽRSS
Last-modified: 2008-07-11 () 14:43:37 (4894d)