ã·ã§ã«ãè¦ããçµç·¯ã詳ãç®ã«æ¸ãã¦ã¿ã
perl - the best friend of find(1) 404 Blog Not Foundãè¦ã¦ã¦ãã·ã§ã«ã«ã¤ãã¦æ¸ãã¦ã¿ãããªãã¾ããã
ä¸åº¦ãLinuxを学ぶための10個の効果的な方法ã§è¨åããã·ã§ã«ãè¦ããçµç·¯ã詳ããæ¸ãã¦ã¿ããã£ãã®ã§ã ãã ãã¨æ¸ãã¦ã¿ãã
ååæ¸ããå
容ããã¡ãã
é ã追ã£ã¦è§£èª¬ãã¦ããã¾ãã
ã·ã§ã«è¦ããçµç·¯ã
- 第ä¸æ®µéã¨ãã¦ã¯ãã¤ãã©ã¤ã³ãè¦ãã |
- 第äºæ®µéã¯sort ã grep ãªã©ããã¤ãã©ã¤ã³ã§å¦çããã®ã«ä¾¿å©ãªã³ãã³ããè¦ããã
- 第ä¸æ®µéã§ls | awk '{print $2}' ã¨ããã£ã¦ã¿ã¦ãã¹ãã¼ã¹ã§åºåãããï¼åç®ã表示ãããäºã«æåãã¦ã¿ãã
- 第å段éã§ls | awk '{print "ls "$2}'ã§ã³ãã³ããä½ã£ã¦ã¿ã¦ls | awk '{print "ls "$2}'|sh ã§ã³ãã³ããã°ã¼ã£ã¨å¦çãã¦ãããäºã«æåãã¦ã¯ã³ã©ã¤ãã¼ãä½ãåºãã
- 第äºæ®µéã§xargs ãè¦ããããawk ã®FSã®è¨å®æ¹æ³ã¨ããè¦ãããã`ã§å²ã¿å§ãã¦ã¯ã³ã©ã¤ãã¼ãå¾ã ã«ä¸æããªãã
- 第å 段éã§ã·ã§ã«ã¹ã¯ãªãããæ¸ãåºãã¦ãfor FILES in `ls`ã§ã«ã¼ãã¾ããå§ããããã¦ã¹ã¯ãªããããå§ãã
- ãªãããã¤ã®éã«ãã·ã§ã«ãã¾ããªã£ã¦ãï¼ âã¤ãã³ã³
第ä¸æ®µéã¨ãã¦ã¯ãã¤ãã©ã¤ã³ãè¦ãã |
æ¬å½ã®åæã«ã¯lsã¨ãcdã¨ããããã§ãããããããã¯ã¨ãããããã£é£ã°ãã¦ã
ãã¤ãã©ã¤ã³ã
ãªãã·ã§ã³ãã³ãã³ãã¨ãååã«ããããªãã¦ãã
è²ã
åºæ¥ãæ§ã«ãªãã¾ãã*1
cat file | sort | uniq ã¨ããã£ã¦ããæ代ãç§ã«ãããã¾ããã*2
第äºæ®µéã¯sort ã grep ãªã©ããã¤ãã©ã¤ã³ã§å¦çããã®ã«ä¾¿å©ãªã³ãã³ããè¦ããã
ãã§ããã¤ãã©ã¤ã³ãå¦çãã¦ããå ã«ä¾¿å©ãªã³ãã³ãããã®ãªãã·ã§ã³ãã©ãã©ãè¦ãã¦ããã¾ããã
ãã¡ããä»ãåå¼·ä¸ã§ããããã使ã£ãç©ãããã¯ã¢ãã
- sort 並ã³æ¿ã sort -u ã§uniqã¨åãæå³ãæã£ãããã
- uniq ã½ã¼ãæ¸ã¿ã®ãã¼ã¿ã®éè¤è¡ããªãããuniq -c ã§ã«ã¦ã³ãããã
- grep ãã¡ã¤ã«ã®æååæ¤ç´¢ãä¸è´ããè¡ãåºãã
- tee ãã¡ã¤ã«ã«ãã°ãæ¸ãåºããªããæ¨æºåºåã«ããã®ã¾ã¾åºãã¦ãããã
- tail ææ¸ã®æ«å°¾ãåºããtail -fã§è¿½å ãããç©ãã©ãã©ãåºãã¦ãããã
- less çµæãä¸ä¸ã«ã¹ã¯ãã¼ã«åºæ¥ãæ§ã«ãªãã
ãã¨ãã°ãtail -f logfile | grep " /index.html "ã¨ãã§index.htmlã®ãã°ã®ã¿ãè¦ããã
sort textfile | less ã§ãã½ã¼ãããçµæããã¼ã§ç§»åã§ãããã¨ã
å¿ç¨ç¯å²ã¯è²ã
åºããã¾ããã
第ä¸æ®µéã§ls | awk '{print $2}' ã¨ããã£ã¦ã¿ã¦ãã¹ãã¼ã¹ã§åºåãããï¼åç®ã表示ãããäºã«æåãã¦ã¿ãã*3
ãã¨ãã°ls -l | awk '{print $3}' ã¨ãããã¨ææè
ã ã表示ã§ããããã¦ã
ls -l | awk '{print $3}' | sort | uniq -c ãªããã§ããããã©ã®ãããã®ãã¡ã¤ã«æã£ã¦ãã確èªã§ããããã¦ã
awkã§ã¨ãããã print ã¨æååã®çµåã¨splitã ãè¦ãã¦ãã
ã¯ã³ã©ã¤ãã¼ã®å¿ç¨ç¯å²åºããã¾ããã
å¿ è¦ãªé¨åã ãåãåºããããæååçµã¿åããããã¨ã
第å段éã§ls | awk '{print "ls "$2}'ã§ã³ãã³ããä½ã£ã¦ã¿ã¦ls | awk '{print "ls "$2}'|sh ã§ã³ãã³ããã°ã¼ã£ã¨å¦çãã¦ãããäºã«æåãã¦ã¯ã³ã©ã¤ãã¼ãä½ãåºãã
shã£ã¦ãæåå渡ãã¨ãããã³ãã³ãã§å¦çãã¦ããããã§ãããã
ãã¡ã¤ã«æ¯è¼ç¨ã«find | awk '{print "diff ./"$1" ../org/"$1"}' | sh ã¨ããã£ããã®ã§ãã
ä¾ã«ãã£ã¦diffã®ãªãã·ã§ã³ã§ãã©ã«ãæ¯è¼ã§ãããããã®ã§ããã
ããããã®ç¥ããªããã©åæã§å¯¾å¿ãããã
find | grep "\.svn\/entries" | sed -e "script"
ã§ãç¹å®ã®ãã¡ã¤ã«ã ãæååç½®æã¨ãã§ããã
è¶
å¼·å¼ãªããæ¹ã§ãã®ã§ãæ¬å½ãªããã¡ã£ã¨ãªãã·ã§ã³ä»ããæ¹ãè¯ã*4ãã§ããã
ãªãã·ã§ã³ã£ã¦ããªãå¤ãã®ã§ãè¦ããããªãã£ããã§ãããã
å¾ã
ã«è¦ãã¦è¡ã£ã¦ã¾ãããå
ã«ããããå¼·å¼ãªããæ¹ã§ã·ã§ã«ãè¦ãã¦ããã¾ããã
第äºæ®µéã§xargs ãè¦ããããawk ã®FSã®è¨å®æ¹æ³ã¨ããè¦ãããã`ã§å²ã¿å§ãã¦ã¯ã³ã©ã¤ãã¼ãå¾ã ã«ä¸æããªãã
ã¨ããããã§ãps -ef | grep "/usr/bin/ruby /program" | awk '{print "kill "$2}' | shãªãã¦
ãã¦ãããå¥ã®äººããã£ã¦ãä½æ¥ã¯awk ã§killãªãã¦ä½ã£ã¦ãªããshã«ã渡ãã¦ãªãã£ããã§ãããã
ãã©ããã£ã¦ããã§ããï¼ãã£ã¦èããã¨ãã
xargs æãã¦è²°ãã¾ããã
ps -ef | grep "/usr/bin/ruby /program" | awk '{print "kill "$2}' | sh ã
ps -ef | grep "/usr/bin/ruby /program" | awk '{print $2}' | xargs kill ã«ãªãã¾ãã
ãããããã¼ãã¨æãã¦è²°ã£ãæ¥ãããã使ãå§ãã¾ããã
ãã¨ãawk 㧠awk 'BEGIN { FS=","} {print $1}'ã¨ããã¨,ã§åãã¦ãããæ§ã«ãªããã¨ãç¥ã£ãã
CURRENT_PATH=`pwd`ãªãããè¦ãã¦ããã¾ããã
ã¡ãã£ã¨è©±ãããã¾ãããxargsã«é¢ãã¦ã¯perl - the best friend of find(1) 404 Blog Not Foundå
ã§
いまどきfindとxargsを使う時は-print0と-0を忘れずにã§
ãã£ã¬ã¯ããªåã«0x20ï¼ç©ºç½ï¼ãå ¥ãMac OS Xç°å¢ã§findã«-print0ã使ããã«xargsã§åãåãã®ã¯ã¤ãããããªãå±éºãä¸åº¦AppleãiTunesã®ã¢ãããã¼ãã¹ã¯ãªãã㧠0x20ããªãã¿ã®æ³å®å¤åä½ãããããã¦ã誤æ¶å»ãããã¨ããã£ãããªãã
ãªã®ã§ãfindã«ã¯å¿ ã-print0ãªãã·ã§ã³ãä»ãã¦ãxargs -0ã§åãåããã¨ããã®ãåºãä¸ã«åºããããä¾ãã°find . -type f -name '*~' -print0 | xargs -0 rmã¨ããæãã
ã¨ç´¹ä»ããã¦ãã¾ããã
2/1からMac使い始めたãã§ããç¥ãã¾ããã§ããã
ä»ã¾ã§åé¡èµ·ãã¦ãªãã¦ããã£ãã
第å 段éã§ã·ã§ã«ã¹ã¯ãªãããæ¸ãåºãã¦ãfor FILES in `ls`ã§ã«ã¼ãã¾ããå§ããããã¦ã¹ã¯ãªããããå§ãã
ã¯ã³ã©ã¤ãã¼ãæ¸ãã¦ããã©ã·ã§ã«ã¹ã¯ãªããã¯ãã¾ãæ¸ãã¦ãªãã£ãç§ã
ã§ãããã·ã§ã«ã¹ã¯ãªããã«ããå¿
è¦æ§ãããç®æãããã¤ãã§ã¦ãã¦
for FILES in `ls`ã§ããã¡ã¤ã«æ¯ã«å¦çãåºæ¥ãäºãç¥ã
ãã¼ãºããã£ãã®ã§ã·ã§ã«ã¹ã¯ãªãããªãããæ¸ãåºãã¾ããã
if ã®æ¸ãæ¹ãforã®æ¸ãæ¹ãè¦ãã¦ãããã¾ã§ã®ç¥èãå©ç¨ãã¦ã·ã§ã«ãæ¸ãã¦ã¾ãã
ãªãããã¤ã®éã«ãã·ã§ã«ãã¾ããªã£ã¦ãï¼ âã¤ãã³ã³
大åå¨ãã®äººã«èãããæ§ã«ãªã£ã¦ãã¦ãçµæ§Linux使ããããã«ãªã£ã¦ããã®ããªãã¨ã
ã§ããã¾ã ã¾ã åå¼·ä¸ã
ä»ã®äººã使ã£ã¦ããã¯ããã¯ã¨ãè¦ã¦ã¯åå¼·ãã¦ãæ¯æ¥ã§ãã
追è¨ï¼
調åã«ã®ã£ã¦vimçãæ¸ãã¦ã¿ã¾ããã
http://d.hatena.ne.jp/zenpou/20080225/1203946622
*1:åå¿è ã¯ããããã£ã¡ããè¦ããã¨è¯ãã¨æã£ã¦ãã
*2:å®ã¯sortã ãã§åãäºåºæ¥ããããã
*3:ãã¾ããã§ããã©lsã ã¨ã ãã§ããããls -lãããªãã¨ã
*4:ã²ã¨ã¤ã®ããã°ã©ã ã§æ¸ãã®ã§ãªã½ã¼ã¹ãã¾ã使ããªãããã°ããã¾ããªã