1. 程式人生 > >Linux 下常用的Shell 命令

Linux 下常用的Shell 命令

input int AR sel https 如果 del grep option

英文原文鏈接:https://www.lopezferrando.com/30-interesting-shell-commands/

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

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

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會話後保持程序運行

如果程序不需要任何交互:

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來完成此目的。

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

timeout 10s ./script.sh

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

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

comm file1 file2

打印這三列:

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

使用選項-1-2-3,可以刪除這些列。

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是個更好用的命令:

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

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



Linux 下常用的Shell 命令