<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          提升開(kāi)發(fā)效率N倍的20+命令行神器!

          共 20757字,需瀏覽 42分鐘

           ·

          2021-03-14 14:04

          背景

          本文主要來(lái)源于在之前公司的小組內(nèi)部的一個(gè)小分享,整理成一篇文章po出來(lái)。題目叫 “Shell 助力開(kāi)發(fā)效率提升”,更切題的應(yīng)該是叫“命令行”提升開(kāi)發(fā)效率,這里并沒(méi)有講到 Shell 編程,而是主要介紹 Linux 或者 Mac 下常用的一些基本工具命令來(lái)幫助處理一些日常事務(wù)。

          通過(guò)本文的介紹,你應(yīng)該對(duì)相關(guān)命令有一個(gè)初步的了解,知道比如用什么命令可以完成怎樣的操作, 至于具體的參數(shù),不需要刻意地背誦,等到需要用到的時(shí)候,再去 cmd --help 或者 man cmd,用得多了,常用的命令也就自然記住了。

          本文首先介紹了 Linux/Mac 下一些常用的命令行工具,然后用具體的示例闡述了常用的命令用法,最后通過(guò)一兩個(gè)案例來(lái)說(shuō)明這些工具的強(qiáng)大之處:

          • 比如給定一個(gè) nginx 日志文件,能夠找出 HTTP 404 請(qǐng)求最多的 top 10 是什么? 比如能找到請(qǐng)求耗時(shí)最多的 top 10 是什么?
          • 再比如能夠簡(jiǎn)單的得到每小時(shí)的"PV"是多少? 再比如拿到一篇文章, 能否簡(jiǎn)單統(tǒng)計(jì)一下這篇文章單次詞頻最高的10個(gè)詞語(yǔ)是什么?
          • 需要批量改某個(gè)文件夾下的文件名,批量將文件夾下的圖片壓縮成固定大小的,等等。

          Mac 環(huán)境

          • zsh
          • on-my-zsh
          • plugin
            • git
            • autojump
            • osx(man-preview/quick-look/pfd(print Finder director)/cdf(cd Finder))
          • 常用快捷鍵(bindkey)
          • 演示: 高亮/git/智能補(bǔ)全/跳轉(zhuǎn)(j, d)...

          這里給大家展示一個(gè)小 Demo,之前在視頻號(hào)(程序猿石頭,歡迎關(guān)注)中分享的一個(gè)小視頻,演示了如何在目錄之間快速跳轉(zhuǎn)。

          關(guān)于 Mac 程序猿提高效率的相關(guān)技巧,更多的可以參考以下三篇文章:

          Shell 基礎(chǔ)命令

          • 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 文本處理

          這里就是通過(guò)案例講了一下12個(gè)命令的大致用法和參數(shù),可以通過(guò)點(diǎn)擊右邊的目錄(我博客有目錄,公眾號(hào)上木有)直達(dá)你想要了解的命令。

          find, grep, xargs, cut, paste, comm
          join, sort, uniq, tr, sed, awk

          find

          • 常用參數(shù)

            • 文件名 -name, 文件類型-type, 查找最大深度-maxdepth
            • 時(shí)間過(guò)濾(create/access/modify) -[cam]time
            • 執(zhí)行動(dòng)作 -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

          • 常用參數(shù)

            • -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)
          • 相關(guān)命令: grep -z / zgrep / zcat xx | grep

          xargs

          • 常用參數(shù)

            • -n(每行列數(shù)),
            • -I(變量替換)
            • -d(分隔符), Mac 不支持,注意與GNU版本的區(qū)別
          • 示例

            find . -type f -name "*.jpg" | xargs -n1 -I {} du -sh {}

          cut

          • 常用參數(shù)

            • -b(字節(jié))
            • -c(字符)
            • -f(第幾列),-d(分隔符),f 范圍: n, n-, -m, n-m
          • 示例

            echo "helloworldhellp" | cut -c1-10
            cut -d, -f2-8 csu.db.export.csv

          paste

          • 常用參數(shù)

            • -d 分隔符
            • -s 列轉(zhuǎn)行
          • 示例

            ?  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 分隔符,默認(rèn)為空格或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

          • 常用參數(shù)

            • 用法 comm [-123i] file1 file2
            • 字典序列, 3列: 只在file1/file2/both
            • - 去掉某列,i 忽略大小寫(xiě)
          • 示例

            ?  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

          相關(guān)命令 diff(類似git diff)

          sort

          • 常用參數(shù)

            • -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 file2
            one     1
            one1    4
            three   3
            two     2
            ?  Documents$ sort -b -k2 -r file2
            one1    4
            three   3
            two     2
            one     1

          uniq

          • 常用參數(shù)

            • -c 重復(fù)次數(shù)
            • -d 重復(fù)的
            • -u 沒(méi)重復(fù)的
            • -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比較相鄰的是否重復(fù),一般與sort聯(lián)用

          tr

          • 常用參數(shù)

            • -c 補(bǔ)集
            • -d 刪除
            • -s 壓縮相鄰重復(fù)的
          • 示例

            ?  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

          • 常用參數(shù)

            • -d 刪除
            • -s 替換, g 全局
            • -e 多個(gè)命令疊加
            • -i 修改原文件(Mac下加參數(shù) "",備份)
          • 示例

            ?  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
            # ()標(biāo)記(轉(zhuǎn)義), \1 引用
            ?  Documents$ sed 's/\([0-9]\)/\1.html/g' file2
            one     1.html
            two     2.html
            three   3.html
            one1.html    4.html
            # 與上面一樣 & 標(biāo)記匹配的字符
            ?  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

          • 基本參數(shù)和語(yǔ)法

            • NR 行號(hào), NF 列數(shù)量
            • $1 第1列, $2, $3...
            • -F fs fs分隔符,字符串或正則
            • 語(yǔ)法: awk 'BEGIN{ commands } pattern{ commands } END{ commands }', 流程如下:
            1. 執(zhí)行begin
            2. 對(duì)輸入每一行執(zhí)行 pattern{ commands }, pattern 可以是 正則/reg exp/, 關(guān)系運(yùn)算等
            3. 處理完畢, 執(zhí)行 end
          • 示例

            ?  Documents$ cat file5
            11  11 aa cc
            22  22 bb
            33  33 d
            11  11
            11  11
            #行號(hào), 列數(shù)量, 第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"
            #添加表達(dá)式
            ?  Documents$ awk '$1>=22 {print NR":", $3}' file5
            2: bb
            3: d
            #累加1到36,奇數(shù),偶數(shù)
            ?  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

          其他高級(jí)語(yǔ)法:for, while 等, 各種函數(shù)等,本身awk是一個(gè)強(qiáng)大的語(yǔ)言,可以掌握一些基本的用法。

          實(shí)際應(yīng)用

          日志統(tǒng)計(jì)分析

          例如拿到一個(gè)nginx日志文件,可以做很多事情,比如看哪些請(qǐng)求是耗時(shí)最久的進(jìn)而進(jìn)行優(yōu)化,比如看每小時(shí)的"PV"數(shù) 等等。

          ?  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的一個(gè)案例, 例如希望找到top 10 請(qǐng)求的path:

          head -n 10000 std.nginx.log | awk '{print $8 ", " $10}' | grep ',404' | sort | uniq -c | sort -nr -k1 | head -n 10
          #or
          head -n 10000 std.nginx.log | awk '$10==404 {print $8}' |sort | uniq -c | sort -nr -k1 | head -n 10

          當(dāng)然,你可能一次不會(huì)直接處理成功,一般會(huì)先少拿一部分?jǐn)?shù)據(jù)進(jìn)行處理看邏輯是否正常, 或者你可以緩存一些中間結(jié)果.

          cat std.nginx.log | awk '{print $8 "," $10}' | grep ',404' >404.log
          sort 404.log | uniq -c | sort -nr -k1 | head -n 10

          再比如每小時(shí)請(qǐng)求數(shù)量,請(qǐng)求耗時(shí)等等

          ?  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

          其他實(shí)際案例 ip block

          案例: db數(shù)據(jù)訂正

          背景: 因?yàn)槟撤?wù)bug,導(dǎo)致插入到db的圖片路徑不對(duì),需要將形如(安全需要已經(jīng)將敏感數(shù)據(jù)替換) 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,因?yàn)閙ysql等db貌似不支持直接正則的替換,所以不能夠很方便的進(jìn)行寫(xiě)sql進(jìn)行替換(就算支持,直接改也有風(fēng)險(xiǎn)的,還是先備份再修改留個(gè)“后悔藥”)。

          當(dāng)然將數(shù)據(jù)導(dǎo)出,然后寫(xiě) python 等腳本處理也是一種解決方案,但如果用上面的命令行處理,只需要幾十秒即可完成。

          步驟:

          1. 準(zhǔn)備數(shù)據(jù)

            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/%';
          2. 替換原文件 一般在用sed替換的時(shí)候,先測(cè)試一下是否正常替換。

            #測(cè)試是否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" 替換時(shí)保留原始備份文件
            sed -i "" 's|https://www.tanglei.name/upload/photos/[0-9]\{1,\}/|http://www.tanglei.me|g' customers.csv
          3. 拼接sql, 然后執(zhí)行

            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
            #然后執(zhí)行sql 即可

          其他

          • play framework session

          • 老方式: 需要啟play環(huán)境,慢。新方式直接命令行解決。

            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"

          • 統(tǒng)計(jì)文章單詞頻率: 下面案例統(tǒng)計(jì)了川普就職演講原文中詞頻最高的10個(gè)詞。

            ?  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
          • 隨機(jī)數(shù):比如常常新注冊(cè)一個(gè)網(wǎng)站,隨機(jī)生成一個(gè)密碼之類的。

            ?  Documents$ cat /dev/urandom | LC_CTYPE=C tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 5
            cpBnvC0niwTybSSJhUUiZwIz6ykJxBvu
            VDP56NlHnugAt2yDySAB9HU2Nd0LlYCW
            0WEDzpjPop32T5STvR6K6SfZMyT6KvAI
            a9xBwBat7tJVaad279fOPdA9fEuDEqUd
            hTLrOiTH5FNP2nU3uflsjPUXJmfleI5c
            ?  Documents$ cat /dev/urandom | head -c32 | base64
            WoCqUye9mSXI/WhHODHDjzLaSb09xrOtbrJagG7Kfqc=
          • 圖片處理壓縮,可批量改圖片大小等等 sips

            ?  linux-shell-more-effiency$ sips -g all which-whereis.png
            /Users/tanglei/Documents/linux-shell-more-effiency/which-whereis.png
              pixelWidth: 280
              pixelHeight: 81
              typeIdentifier: public.png
              format: png
              formatOptions: default
              dpiWidth: 72.000
              dpiHeight: 72.000
              samplesPerPixel: 4
              bitsPerSample: 8
              hasAlpha: yes
              space: RGB
              profile: DELL U2412M
            ?  linux-shell-more-effiency$ sips -Z 250 which-whereis.png
            /Users/tanglei/Documents/linux-shell-more-effiency/which-whereis.png
              /Users/tanglei/Documents/linux-shell-more-effiency/which-whereis.png
            ?  linux-shell-more-effiency$ sips -g all which-whereis.png
            /Users/tanglei/Documents/linux-shell-more-effiency/which-whereis.png
              pixelWidth: 250
              pixelHeight: 72
              typeIdentifier: public.png
              format: png
              formatOptions: default
              dpiWidth: 72.000
              dpiHeight: 72.000
              samplesPerPixel: 4
              bitsPerSample: 8
              hasAlpha: yes
              space: RGB
              profile: DELL U2412M
            ?  linux-shell-more-effiency$ sips -z 100 30 which-whereis.png
            /Users/tanglei/Documents/linux-shell-more-effiency/which-whereis.png
              /Users/tanglei/Documents/linux-shell-more-effiency/which-whereis.png
            ?  linux-shell-more-effiency$ sips -g pixelWidth -g pixelHeight which-whereis.png
            /Users/tanglei/Documents/linux-shell-more-effiency/which-whereis.png
              pixelWidth: 30
              pixelHeight: 100
          • 命令行處理 JSON 的神器:隨著 JSON 通用性,常常需要處理 JSON 數(shù)據(jù),這里推薦這個(gè)命令行 JSON 處理神器 jq is a lightweight and flexible command-line JSON processor[1]

          • 推薦以下參考材料:

          • [1] JSON processor: https://stedolan.github.io/jq/

            [2] Linux工具快速教程: http://linuxtools-rst.readthedocs.io/zh_CN/latest/index.html

            [3] Linux命令大全: http://man.linuxde.net/

            [4] Advanced Bash-Scripting Guide: http://tldp.org/LDP/abs/html/

            [5] UNIX環(huán)境高級(jí)編程: https://book.douban.com/subject/25900403


          喜歡的朋友歡迎關(guān)注,訂閱更多精彩內(nèi)容
          瀏覽 34
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产视频精品i | 国产真实露脸乱子伦对白高清视频 | 色99在线视频 | 污污污污网站 | 日韩视频在线观看二区 |