Shell有趣的命令

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

watch "ls -larth"

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

sudo fuser -k 8000/tcp

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

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

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

rename 's/\.bak$/.txt/' *.bak

5. 得到完整的文件路徑

readlink -f file.txt

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服務器時。css

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-utilsnginx

19. 檢查資源的使用狀況

/usr/bin/time -v ls

20. 文件的隨機行

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

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

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

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

固然,也能夠使用screentmux來完成此目的。sql

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

timeout 10s ./script.sh

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

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

comm file1 file2

打印這三列:ubuntu

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

使用選項-1-2-3,能夠刪除這些列。bash

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

split -l LINES -d file.txt output_prefix

25. 刷新交換分區

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

sudo swapoff -a sudo swapon -a

26. 修復ext4文件系統的superblock問題

sudo fsck.ext4 -f -y /dev/sda1 sudo fsck.ext4 -v /dev/sda1 sudo mke2fs -n /dev/sda1 sudo e2fsck -n <first block number of previous list> /dev/sda1

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

fallocate -l 1G test.img

28. 從命令行操做PDF文件

joinshuffleselect等命令相比,pdftk是個更好用的命令:dom

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

還能夠使用cpdf操做內容:ssh

cpdf -draft in.pdf -o out.pdf # Remove images cpdf -blacktext in.pdf -o out.pdf # Convert all text to black color

29. 根據生成的輸出監視進度

# 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 # -i2, refresh every 2 seconds

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

apt-file update apt-file search dir/file.h
相關文章
相關標籤/搜索