Shell命令和技巧
監控命令(每2秒執行一次)
watch "ls -larth"
使用一個埠殺死程式
sudo fuser -k 8000/tcp
限制以下命令的記憶體使用
ulimit -Sv 1000# 1000 KBs = 1 MB
ulimit -Sv unlimited # Remove limit使用正則表示式重新命名所選檔案
rename 's/.bak$/.txt/' *.bak
獲得完整的檔案路徑
readlink -f file.txt
列出tar.gz檔案的內容,並只提取一個檔案
tar tf file.tgz
tar xf file.tgz filename按照檔案大小列出檔案
ls -lS
跟蹤路由
mtr google.com
查詢檔案的提示
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列印無限迴圈的文字
yes
yes hello當前登入使用者
w
輸出結果前置行號
ls | nl
Grep使用Perl風格的語法(允許像\t這樣的字元)
grep -P "\t"
Cat命令反向輸出(從末端開始)
tac file
檢查每個目錄中的檔案的的許可權
檢測許可權錯誤是很有用的,例如在配置web伺服器時。namei -l /path/to/file.txt
每次修改檔案時都會執行命令
while inotifywait -e close_write document.tex
do
make
done複製到剪貼簿
cat file.txt | xclip -selection clipboard
Latex的拼寫和語法檢查
detex file.tex | diction -bs
你可能需要安裝以下內容:sudo apt-get install diction texlive-extra-utils
。
檢查資源的使用情況
/usr/bin/time -v ls
檔案的隨機行
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在離開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來完成此目的。
在有限的時間內執行命令
timeout 10s ./script.sh
# Restart every 30 minutes
while true; do timeout 30m ./script.sh; done從兩個排序檔案中合併行
comm file1 file2
列印這三列:
1.行file1獨有。
2.行file2獨有。
3.在行file1和行file2中都有。
使用選項-1、-2、-3,可以刪除這些列。
在檔案中分割長檔案,使用相同數量的行
split -l LINES -d file.txt output_prefix
重新整理交換分割槽
如果一個程式消耗了太多的記憶體,交換分割槽就會被剩餘的記憶體填滿,當你回到正常的時候,一切都是緩慢的。只需重新啟動交換分割槽來修復它:
sudo swapoff -a
sudo swapon -a
修復ext4檔案系統的superblock問題
sudo fsck.ext4 -f -y /dev/sda1
sudo fsck.ext4 -v /dev/sda1
sudo mke2fs -n /dev/sda1
sudo e2fsck -n建立給定大小的空檔案
fallocate -l 1G test.img
從命令列操作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
根據生成的輸出監視進度
# 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在Ubuntu中找到一個檔案的包
apt-file update
apt-file search dir/file.h
轉自:https://www.jb51.net/article/125194.htm
非常有用啊!