提升开发效率N倍的20+命令行神器,赶紧收藏了
èæ¯
æ¬æ主è¦æ¥æºäºå¨ä¹åå¬å¸çå°ç»åé¨çä¸ä¸ªå°å享ï¼æ´çæä¸ç¯æç« poåºæ¥ãé¢ç®å« “Shell å©åå¼åæçæå”ï¼æ´åé¢çåºè¯¥æ¯å«“å½ä»¤è¡”æåå¼åæçï¼è¿é并没æè®²å° Shell ç¼ç¨ï¼èæ¯ä¸»è¦ä»ç» Linux æè Mac ä¸å¸¸ç¨çä¸äºåºæ¬å·¥å·å½ä»¤æ¥å¸®å©å¤çä¸äºæ¥å¸¸äºå¡ã
éè¿æ¬æçä»ç»ï¼ä½ åºè¯¥å¯¹ç¸å³å½ä»¤æä¸ä¸ªåæ¥çäºè§£ï¼ç¥éæ¯å¦ç¨ä»ä¹å½ä»¤å¯ä»¥å®æææ ·çæä½ï¼ è³äºå·ä½çåæ°ï¼ä¸éè¦å»æå°è诵ï¼çå°éè¦ç¨å°çæ¶åï¼åå» cmd --help æè man cmdï¼ç¨å¾å¤äºï¼å¸¸ç¨çå½ä»¤ä¹å°±èªç¶è®°ä½äºã
æ¬æé¦åä»ç»äº Linux/Mac ä¸ä¸äºå¸¸ç¨çå½ä»¤è¡å·¥å·ï¼ç¶åç¨å·ä½ç示ä¾éè¿°äºå¸¸ç¨çå½ä»¤ç¨æ³ï¼æåéè¿ä¸ä¸¤ä¸ªæ¡ä¾æ¥è¯´æè¿äºå·¥å·ç强大ä¹å¤ï¼
- æ¯å¦ç»å®ä¸ä¸ª nginx æ¥å¿æ件ï¼è½å¤æ¾åº HTTP 404 请æ±æå¤ç top 10 æ¯ä»ä¹? æ¯å¦è½æ¾å°è¯·æ±èæ¶æå¤ç top 10 æ¯ä»ä¹?
- åæ¯å¦è½å¤ç®åçå¾å°æ¯å°æ¶ç"PV"æ¯å¤å°? åæ¯å¦æ¿å°ä¸ç¯æç« ï¼ è½å¦ç®åç»è®¡ä¸ä¸è¿ç¯æç« å次è¯é¢æé«ç10个è¯è¯æ¯ä»ä¹?
- éè¦æ¹éæ¹æ个æ件夹ä¸çæ件åï¼æ¹éå°æ件夹ä¸çå¾çå缩æåºå®å¤§å°çï¼ççã
Mac ç¯å¢
- zsh
- on-my-zsh
- plugin
- git
- autojump
- osx(man-preview/quick-look/pfd(print Finder director)/cdf(cd Finder))
- 常ç¨å¿«æ·é®(bindkey)
- æ¼ç¤º: é«äº®/git/æºè½è¡¥å¨/跳转(j, d)...
Shell åºç¡å½ä»¤
which/whereisï¼ å¸¸ç¨ whatisï¼ manï¼ --help
➜ .oh-my-zsh git:(master)$ whereis ls /bin/ls➜ .oh-my-zsh git:(master)$ which ls ls: aliased to ls -G
åºæ¬æ件ç®å½æä½
rmï¼ mkdirï¼ mvï¼ cpï¼ cdï¼ lsï¼ lnï¼ fileï¼ statï¼ wc(-l/w/c)ï¼ headï¼ moreï¼ tailï¼ cat...
å©å¨ 管é: |
Shell ææ¬å¤ç
è¿éå°±æ¯éè¿æ¡ä¾è®²äºä¸ä¸12个å½ä»¤ç大è´ç¨æ³ååæ°ï¼å¯ä»¥éè¿ç¹å»å³è¾¹çç®å½(æå客æç®å½ï¼å¬ä¼å·ä¸æ¨æ)ç´è¾¾ä½ æ³è¦äºè§£çå½ä»¤ã
find, grep, xargs, cut, paste, comm join, sort, uniq, tr, sed, awk
find
- 常ç¨åæ°
- æ件å -nameï¼ æ件类å-typeï¼ æ¥æ¾æ大深度-maxdepth
- æ¶é´è¿æ»¤(create/access/modify) -[cam]time
- æ§è¡å¨ä½ -exec
示ä¾
find ./ -name "*.json" find . -maxdepth 7 -name "*.json" -type f find . -name "*.log.gz" -ctime +7 -size +1M -delete (atime/ctime/mtime) find . -name "*.scala" -atime -7 -exec du -h {} \;
grep
- 常ç¨åæ°
- -v(invert-match)ï¼
- -c(count)ï¼
- -n(line-number)ï¼
- -i(ignore-case)ï¼
- -lï¼ -Lï¼ -R(-rï¼ --recursive)ï¼ -e
示ä¾
grep 'partner' ./*.scala -l grep -e 'World' -e 'first' -i -R ./ (-e: or)
ç¸å³å½ä»¤: grep -z / zgrep / zcat xx | grep
xargs
- 常ç¨åæ°
- -n(æ¯è¡åæ°)ï¼
- -I(åéæ¿æ¢)
- -d(åé符)ï¼ Mac ä¸æ¯æï¼æ³¨æä¸GNUçæ¬çåºå«
示ä¾
echo "helloworldhellp" | cut -c1-10 cut -dï¼ -f2-8 csu.db.export.csv
cut
- 常ç¨åæ°
- -b(åè)
- -c(å符)
- -f(第å å)ï¼-d(åé符)ï¼f èå´: n, n-, -m, n-m
示ä¾
echo "helloworldhellp" | cut -c1-10cut -dï¼ -f2-8 csu.db.export.csv
paste
- 常ç¨åæ°
- -d åé符
- -s å转è¡
示ä¾
➜ Documents$ cat file1 1 11 2 22 3 33 4 44 ➜ Documents$ cat file2 one 1 two 2 three 3 one1 4 ➜ Documents$ paste -d, file1 file2 1 11, one 1 2 22, two 2 3 33, three 3 4 44, one1 4 ➜ Documents$ paste -s -d: file1 file2 a 11:b bb:3 33:4 44 one 1:two 2:three 3:one1 4
join
类似sqlä¸ç ...inner join ...on ...ï¼ -t åé符ï¼é»è®¤ä¸ºç©ºæ ¼ætab
➜ Documents$ cat j1 1 11 2 22 3 33 4 44 5 55 ➜ Documents$ cat j2 one 1 0 one 2 1 two 4 2 three 5 3 one1 5 4 ➜ Documents$ join -1 1 -2 3 j1 j2 1 11 one 2 2 22 two 4 3 33 three 5 4 44 one1 5
comm
- 常ç¨åæ°
- ç¨æ³ comm [-123i] file1 file2
- åå¸åºåï¼ 3å: åªå¨file1/file2/both
- - å»ææåï¼i 忽ç¥å¤§å°å
示ä¾
➜ Documents$ seq 1 5 >file11 ➜ Documents$ seq 2 6 >file22 ➜ Documents$ cat file11 1 2 3 4 5 ➜ Documents$ cat file22 2 3 4 5 6 ➜ Documents$ comm file11 file22 1 2 3 4 5 6 ➜ Documents$ comm -1 file11 file22 2 3 4 5 6 ➜ Documents$ comm -2 file11 file22 1 2 3 4 5 ➜ Documents$ comm -23 file11 file22 1
ç¸å³å½ä»¤ diff(类似git diff)
sort
- 常ç¨åæ°
- -dï¼ --dictionary-order
- -nï¼ --numeric-sort
- -rï¼ --reverse
- -bï¼ --ignore-leading-blanks
- -kï¼ --key
示ä¾
➜ Documents$ cat file2 one 1 two 2 three 3 one1 4 ➜ Documents$ sort file2one 1 one1 4 three 3 two 2 ➜ Documents$ sort -b -k2 -r file2one1 4 three 3 two 2 one 1
uniq
- 常ç¨åæ°
- -c éå¤æ¬¡æ°
- -d éå¤ç
- -u 没éå¤ç
- -f 忽ç¥åå å
示ä¾
➜ Documents$ cat file4 11 22 33 11 11 ➜ Documents$ sort file4 | uniq -c 3 11 1 22 1 33 ➜ Documents$ sort file4 | uniq -d 11 ➜ Documents$ sort file4 | uniq -u 22 33 ➜ Documents$ cat file3 one 1 two 1 three 3 one1 4 ➜ Documents$ uniq -c -f 1 file3 2 one 1 1 three 3 1 one1 4
注æï¼uniqæ¯è¾ç¸é»çæ¯å¦éå¤ï¼ä¸è¬ä¸sortèç¨
tr
- 常ç¨åæ°
- -c è¡¥é
- -d å é¤
- -s å缩ç¸é»éå¤ç
示ä¾
➜ Documents$ echo '1111234444533hello' | tr '[1-3]' '[a-c]' aaaabc44445cchello➜ Documents$ echo '1111234444533hello' | tr -d '[1-3]' 44445hello➜ Documents$ echo '1111234444533hello' | tr -dc '[1-3]' 11112333➜ Documents$ echo '1111234444533hello' | tr -s '[0-9]' 123453hello➜ Documents$ echo 'helloworld' | tr '[:lower:]' '[:upper:]' HELLOWORLD
sed
- 常ç¨åæ°
- -d å é¤
- -s æ¿æ¢ï¼ g å¨å±
- -e å¤ä¸ªå½ä»¤å å
- -i ä¿®æ¹åæ件(Macä¸å åæ° ""ï¼å¤ä»½)
示ä¾
➜ Documents$ cat file2 one 1 two 2 three 3 one1 4 ➜ Documents$ sed "2,3d" file2 one 1 one1 4 ➜ Documents$ sed '/one/d' file2 two 2 three 3 ➜ Documents$ sed 's/one/111/g' file2 111 1 two 2 three 3 1111 4 #å°oneæ¿æ¢æ111 并å°å«ætwoçè¡å é¤ ➜ Documents$ sed -e 's/one/111/g' -e '/two/d' file2 111 1 three 3 1111 4 # ()æ è®°(转ä¹), \1 å¼ç¨ ➜ Documents$ sed 's/\([0-9]\)/\1.html/g' file2 one 1.html two 2.html three 3.html one1.html 4.html # ä¸ä¸é¢ä¸æ · & æ è®°å¹éçå符➜ Documents$ sed 's/[0-9]/&.html/g' file2 one 1.html two 2.html three 3.html one1.html 4.html ➜ Documents$ cat mobile.csv"13090246026" "18020278026" "18520261021" "13110221022" ➜ Documents$ sed 's/\([0-9]\{3\}\)[0-9]\{4\}/\1xxxx/g' mobile.csv "130xxxx6026" "180xxxx8026" "185xxxx1021" "131xxxx1022"
awk
- åºæ¬åæ°åè¯æ³
- NR è¡å·ï¼ NF åæ°é
- $1 第1åï¼ $2, $3...
- -F fs fsåé符ï¼å符串ææ£å
è¯æ³: awk 'BEGIN{ commands } pattern{ commands } END{ commands }'ï¼ æµç¨å¦ä¸:
- æ§è¡begin
- 对è¾å¥æ¯ä¸è¡æ§è¡ pattern{ commands }ï¼ pattern å¯ä»¥æ¯ æ£å/reg exp/ï¼ å³ç³»è¿ç®ç
- å¤çå®æ¯ï¼ æ§è¡ end
示ä¾
➜ Documents$ cat file5 11 11 aa cc 22 22 bb 33 33 d 11 11 11 11 #è¡å·ï¼ åæ°éï¼ ç¬¬3å ➜ Documents$ awk '{print NR"("NF"):"ï¼ $3}' file5 1(4): aa 2(3): bb 3(3): d 4(2): 5(2): #å符串åå²ï¼ æå°1ï¼2å ➜ Documents$ awk -F"xxxx" '{print $1ï¼ $2}' mobile.csv "130 6026" "180 8026" "185 1021" "131 1022" #æ·»å 表达å¼➜ Documents$ awk '$1>=22 {print NR":"ï¼ $3}' file5 2: bb3: d#ç´¯å 1å°36ï¼å¥æ°ï¼å¶æ° ➜ Documents$ seq 36 | awk 'BEGIN{sum=0; print "question:"} {print $1" +"; sum+=$1} END{print "="; print sum}' | xargs | sed 's/+ =/=/' question: 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 + 18 + 19 + 20 + 21 + 22 + 23 + 24 + 25 + 26 + 27 + 28 + 29 + 30 + 31 + 32 + 33 + 34 + 35 + 36 = 666 ➜ Documents$ seq 36 | awk 'BEGIN{sum=0; print "question:"} $1 % 2 ==1 {print $1" +"; sum+=$1} END{print "="; print sum}' | xargs | sed 's/+ =/=/' question: 1 + 3 + 5 + 7 + 9 + 11 + 13 + 15 + 17 + 19 + 21 + 23 + 25 + 27 + 29 + 31 + 33 + 35 = 324 ➜ Documents$ seq 36 | awk 'BEGIN{sum=0; print "question:"} $1 % 2 !=1 {print $1" +"; sum+=$1} END{print "="; print sum}' | xargs | sed 's/+ =/=/' question: 2 + 4 + 6 + 8 + 10 + 12 + 14 + 16 + 18 + 20 + 22 + 24 + 26 + 28 + 30 + 32 + 34 + 36 = 342
å¶ä»é«çº§è¯æ³ï¼for, while çï¼ åç§å½æ°çï¼æ¬èº«awkæ¯ä¸ä¸ªå¼ºå¤§çè¯è¨ï¼å¯ä»¥ææ¡ä¸äºåºæ¬çç¨æ³ã
å®éåºç¨
æ¥å¿ç»è®¡åæ
ä¾å¦æ¿å°ä¸ä¸ªnginxæ¥å¿æ件ï¼å¯ä»¥åå¾å¤äºæï¼æ¯å¦çåªäºè¯·æ±æ¯èæ¶æä¹çè¿èè¿è¡ä¼åï¼æ¯å¦çæ¯å°æ¶ç"PV"æ° ççã
➜ Documents$ head -n5 std.nginx.log 106.38.187.225 - - [20/Feb/2017:03:31:01 +0800] www.tanglei.name "GET /baike/208344.html HTTP/1.0" 301 486 "-" "Mozilla/5.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322) 360JK yunjiankong 975382" "106.38.187.225, 106.38.187.225" - 0.000 106.38.187.225 - - [20/Feb/2017:03:31:02 +0800] www.tanglei.name "GET /baike/208344.html HTTP/1.0" 301 486 "-" "Mozilla/5.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322) 360JK yunjiankong 975382" "106.38.187.225, 106.38.187.225" - 0.000 10.130.64.143 - - [20/Feb/2017:03:31:02 +0800] stdbaike.bdp.cc "POST /baike/wp-cron.php?doing_wp_cron=1487532662.2058920860290527343750 HTTP/1.1" 200 182 "-" "WordPress/4.5.6; http://www.tanglei.name/baike" "10.130.64.143" 0.205 0.205 10.130.64.143 - - [20/Feb/2017:03:31:02 +0800] www.tanglei.name "GET /external/api/login-status HTTP/1.0" 200 478 "-" "-" "10.130.64.143" 0.003 0.004 10.130.64.143 - - [20/Feb/2017:03:31:02 +0800] www.tanglei.name "GET /content_util/authorcontents?count=5&offset=0&israndom=1&author=9 HTTP/1.0" 200 11972 "-" "-" "10.130.64.143" 0.013 0.013
ä¸é¢æ¯nginxçä¸ä¸ªæ¡ä¾ï¼ ä¾å¦å¸ææ¾å°top 10 请æ±çpath:
head -n 10000 std.nginx.log | awk '{print $8 ", " $10}' | grep ',404' | sort | uniq -c | sort -nr -k1 | head -n 10 #orhead -n 10000 std.nginx.log | awk '$10==404 {print $8}' |sort | uniq -c | sort -nr -k1 | head -n 10
å½ç¶ï¼ä½ å¯è½ä¸æ¬¡ä¸ä¼ç´æ¥å¤çæåï¼ä¸è¬ä¼åå°æ¿ä¸é¨åæ°æ®è¿è¡å¤ççé»è¾æ¯å¦æ£å¸¸ï¼ æèä½ å¯ä»¥ç¼åä¸äºä¸é´ç»æ.
cat std.nginx.log | awk '{print $8 "," $10}' | grep ',404' >404.log sort 404.log | uniq -c | sort -nr -k1 | head -n 10
åæ¯å¦æ¯å°æ¶è¯·æ±æ°éï¼è¯·æ±èæ¶çç
➜ Documents$ head -n 100000 std.nginx.log | awk -F: '{print $1 $2}' | cut -f3 -d/ | uniq -c 8237 201703 15051 201704 16083 201705 18561 201706 22723 201707 19345 201708
å¶ä»å®éæ¡ä¾ ip block
æ¡ä¾: dbæ°æ®è®¢æ£
èæ¯: å 为ææå¡bugï¼å¯¼è´æå¥å°dbçå¾çè·¯å¾ä¸å¯¹ï¼éè¦å°å½¢å¦(å®å¨éè¦å·²ç»å°æææ°æ®æ¿æ¢) https://www.tanglei.name/upload/photos/129630//internal-public/shangtongdai/2017-02-19-abcdefg-eb85-4c24-883e-hijklmn.jpg æ¿æ¢æ http://www.tanglei.me/internal-public/shangtongdai/2017-02-19-abcdefg-eb85-4c24-883e-hijklmn.jpgï¼å 为mysqlçdbè²ä¼¼ä¸æ¯æç´æ¥æ£åçæ¿æ¢ï¼æ以ä¸è½å¤å¾æ¹ä¾¿çè¿è¡åsqlè¿è¡æ¿æ¢(å°±ç®æ¯æï¼ç´æ¥æ¹ä¹æé£é©çï¼è¿æ¯åå¤ä»½åä¿®æ¹ç个“åæ诔)ã
å½ç¶å°æ°æ®å¯¼åºï¼ç¶åå python çèæ¬å¤çä¹æ¯ä¸ç§è§£å³æ¹æ¡ï¼ä½å¦æç¨ä¸é¢çå½ä»¤è¡å¤çï¼åªéè¦å åç§å³å¯å®æã
æ¥éª¤:
- åå¤æ°æ®
select id, photo_url_1, photo_url_2, photo_url_3 from somedb.sometable where photo_url_1 like 'https://www.tanglei.name/upload/photos/%//internal-public/%' or photo_url_2 like 'https://www.tanglei.name/upload/photos/%//internal-public/%' or photo_url_3 like 'https://www.tanglei.name/upload/photos/%//internal-public/%';
- æ¿æ¢åæ件 ä¸è¬å¨ç¨sedæ¿æ¢çæ¶åï¼åæµè¯ä¸ä¸æ¯å¦æ£å¸¸æ¿æ¢ã
#æµè¯æ¯å¦OK head -n 5 customers.csv | sed 's|https://www.tanglei.name/upload/photos/[0-9]\{1,\}/|http://www.tanglei.me|g' # ç´æ¥æ¿æ¢åæä»¶ï¼ å¯ä»¥sed -i ".bak" æ¿æ¢æ¶ä¿çåå§å¤ä»½æ件 sed -i "" 's|https://www.tanglei.name/upload/photos/[0-9]\{1,\}/|http://www.tanglei.me|g' customers.csv
- æ¼æ¥sqlï¼ ç¶åæ§è¡
awk -Fï¼ '{print "update sometable set photo_url_1 = " $2, ", photo_url_2 = " $3, ", photo_url_3 = " $4, " where id = " $1 ";" }' customers.csv > customer.sql #ç¶åæ§è¡sql å³å¯
å¶ä»
- play framework session
èæ¹å¼: éè¦å¯playç¯å¢ï¼æ¢ãæ°æ¹å¼ç´æ¥å½ä»¤è¡è§£å³ã
sbt "project site" consoleQuick import play.api.libs._val sec = "secret...secret" var uid = "10086" Crypto.sign(s"uid=$uid"ï¼ sec.getBytes("UTF-8")) + s"-uid=$uid" ➜ Documents$ ~/stdcookie.sh 97522 918xxxxdf64abcfcxxxxc465xx7554dxxxx21e-uid=97522 ➜ Documents$ cat ~/stdcookie.sh#!/bin/bash ## cannot remove this line uid=$1 hash=`echo -n "uid=$uid" | openssl dgst -sha1 -hmac "secret...secret"` echo "$hash-uid=$uid"
ç»è®¡æç« åè¯é¢ç: ä¸é¢æ¡ä¾ç»è®¡äºå·æ®å°±èæ¼è®²åæä¸è¯é¢æé«ç10个è¯ã
➜ Documents$ head -n3 chuanpu.txt Chief Justice Robertsï¼ President Carterï¼ President Clintonï¼ President Bushï¼ President Obamaï¼ fellow Americans and people of the worldï¼ thank you. Weï¼ the citizens of Americaï¼ are now joined in a great national effort to rebuild our country and restore its promise for all of our people. Together we will determine the course of America and the world for manyï¼ many years to come. ➜ Documents$ cat chuanpu.txt | tr -dc 'a-zA-Z ' | xargs -n 1 | sort | uniq -c | sort -nr -k1 | head -n 20 65 the 63 and 48 of 46 our 42 will 37 to 21 We 20 is 18 we 17 America 15 a 14 all 13 in 13 for 13 be 13 are 10 your 10 not 10 And 10 American
- éæºæ°ï¼æ¯å¦å¸¸å¸¸æ°æ³¨åä¸ä¸ªç½ç«ï¼éæºçæä¸ä¸ªå¯ç ä¹ç±»çã