Shell命令和技巧

  1. 監控命令(每2秒運行一次)web

    watch "ls -larth"正則表達式

  2. 使用一個端口殺死程序服務器

    sudo fuser -k 8000/tcpdom

  3. 限制如下命令的內存使用tcp

    ulimit -Sv 1000# 1000 KBs = 1 MB
    ulimit -Sv unlimited # Remove limitgoogle

  4. 使用正則表達式重命名所選文件.net

    rename 's/.bak$/.txt/' *.bak命令行

  5. 得到完整的文件路徑code

    readlink -f file.txthtm

  6. 列出tar.gz文件的內容,並只提取一個文件

    tar tf file.tgz
    tar xf file.tgz filename

  7. 按照文件大小列出文件

    ls -lS

  8. 跟蹤路由

    mtr google.com

  9. 查找文件的提示

    find . -size 20c # By file size (20 bytes)
    find . -name "*.gz" -delete # Delete files
    find . -exec echo {} ; # One file by line
    ./file1
    ./file2
    ./file3
    find . -exec echo {} + # All in the same line
    ./file1 ./file2 ./file3

  10. 打印無限循環的文本

    yes
    yes hello

  11. 當前登陸用戶

    w

  12. 輸出結果前置行號

    ls | nl

  13. Grep使用Perl風格的語法(容許像\t這樣的字符)

    grep -P "\t"

  14. Cat命令反向輸出(從末端開始)

    tac file

  15. 檢查每一個目錄中的文件的的權限
    檢測權限錯誤是頗有用的,例如在配置web服務器時。

    namei -l /path/to/file.txt

  16. 每次修改文件時都會執行命令

    while inotifywait -e close_write document.tex
    do
    make
    done

  17. 複製到剪貼板

    cat file.txt | xclip -selection clipboard

  18. Latex的拼寫和語法檢查

    detex file.tex | diction -bs

你可能須要安裝如下內容:sudo apt-get install diction texlive-extra-utils

  1. 檢查資源的使用狀況

    /usr/bin/time -v ls

  2. 文件的隨機行

    cat file.txt | sort -R
    cat file.txt | sort -R | head # Pick a random sambple
    # Even better (suggested by xearl in Hacker news):
    shuf file.txt

  3. 在離開SSH會話後保持程序運行

若是程序不須要任何交互:

nohup ./script.sh &

若是你須要手動輸入一些內容,而後離開:

./script.sh
<Type any input you want>
<Ctrl-Z> # send process to sleep
jobs -l  # find out the job id
disown -h jobid  # disown job
bg# continue running in the background

固然,也可使用screen或tmux來完成此目的。

  1. 在有限的時間內運行命令

    timeout 10s ./script.sh
    # Restart every 30 minutes
    while true; do timeout 30m ./script.sh; done

  2. 從兩個排序文件中合併行

    comm file1 file2

打印這三列:

1.行file1獨有。
2.行file2獨有。
3.在行file1和行file2中都有。

使用選項-一、-二、-3,能夠刪除這些列。

  1. 在文件中分割長文件,使用相同數量的行

    split -l LINES -d file.txt output_prefix

  2. 刷新交換分區

若是一個程序消耗了太多的內存,交換分區就會被剩餘的內存填滿,當你回到正常的時候,一切都是緩慢的。只需從新啓動交換分區來修復它:

sudo swapoff -a
sudo swapon -a
  1. 修復ext4文件系統的superblock問題

    sudo fsck.ext4 -f -y /dev/sda1
    sudo fsck.ext4 -v /dev/sda1
    sudo mke2fs -n /dev/sda1
    sudo e2fsck -n /dev/sda1

  2. 建立給定大小的空文件

    fallocate -l 1G test.img

  3. 從命令行操做PDF文件

與join,shuffle,select等命令相比,pdftk是個更好用的命令:

pdftk *.pdf cat output all.pdf    # Join PDFs together
pdftk A=in.pdf cat A5 output out.pdf # Extract page from PDF

還可使用cpdf操做內容:

cpdf -draft in.pdf -o out.pdf   # Remove images
cpdf -blacktext in.pdf -o out.pdf # Convert all text to black color
  1. 根據生成的輸出監視進度

    # Write random data, encode it in base64 and monitor how fast it
    # is being sent to /dev/null
    cat /dev/urandom | base64 | pv -lbri2 > /dev/null
    # pv options:
    # -l, lines
    # -b, total counter
    # -r, show rate

  2. 在Ubuntu中找到一個文件的包

    apt-file update
    apt-file search dir/file.h


轉自:https://www.jb51.net/article/125194.htm 很是有用啊!

相關文章
相關標籤/搜索