Linux shell下30个有趣的命令和提示(推荐)

2019-09-23 09:03:04丽君

这些是我收集了多年的Linux shell的30个有趣的命令和提示。

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

当然,也可以使用screen或tmux来完成此目的。

22. 在有限的时间内运行命令

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