這些是我收集了多年的Linux shell的30個有趣的命令和提示。
1. 監控命令(每2秒運行一次)
watch "ls -larth"
2. 使用一個端口殺死程序
sudo fuser -k 8000/tcp
3. 限制以下命令的內存使用
ulimit -Sv 1000 # 1000 KBs = 1 MBulimit -Sv unlimited # Remove limit
4. 使用正則表達式重命名所選文件
rename 's//.bak$/.txt/' *.bak
5. 獲得完整的文件路徑
readlink -f file.txt
6. 列出tar.gz文件的內容,并只提取一個文件
tar tf file.tgztar 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 filesfind . -exec echo {} /; # One file by line./file1./file2./file3find . -exec echo {} /+ # All in the same line./file1 ./file2 ./file310. 打印無限循環的文本
yesyes 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.texdo makedone
17. 復制到剪貼板
cat file.txt | xclip -selection clipboard
18. Latex的拼寫和語法檢查
detex file.tex | diction -bs
你可能需要安裝以下內容:sudo apt-get install diction texlive-extra-utils。
19. 檢查資源的使用情況
/usr/bin/time -v ls
20. 文件的隨機行
cat file.txt | sort -Rcat file.txt | sort -R | head # Pick a random sambple# Even better (suggested by xearl in Hacker news):shuf file.txt
21. 在離開SSH會話后保持程序運行
如果程序不需要任何交互:
nohup ./script.sh &
如果你需要手動輸入一些內容,然后離開:
./script.sh<Type any input you want><Ctrl-Z> # send process to sleepjobs -l # find out the job iddisown -h jobid # disown jobbg # continue running in the background
當然,也可以使用screen或tmux來完成此目的。
22. 在有限的時間內運行命令
timeout 10s ./script.sh# Restart every 30 minuteswhile true; do timeout 30m ./script.sh; done
新聞熱點
疑難解答