Linux shell下30个有趣的命令
2017-10-08 00:17
239 查看
这些是我收集了多年的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
23. 从两个排序文件中合并行
comm file1 file2
打印这三列:
行
file1独有。
行
file2独有。
在行
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文件
与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
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
Tips
“本译文仅供个人研习、欣赏语言之用,谢绝任何转载及用于任何商业用途。本译文所涉法律后果均由本人承担。本人同意简书平台在接获有关著作权人的通知后,删除文章。”
相关文章推荐
- Linux shell下30个有趣的命令和提示(推荐)
- 有趣的linux shell 命令, 跑马车
- 安装乌班图系统,并且演示有趣的linux命令,你还怕对linux无兴趣吗 推荐
- Linux Shell 常用命令与目录分区的学习总结
- linux shell 多个命令一起执行的几种方法
- Linux 有趣命令
- Linux Shell编程(20)——基本命令
- Linux shell 常用命令
- Linux上面几个有趣的命令
- Linux下的有趣命令
- 一个有趣的linux命令
- Linux shell之sort命令
- [一天几个linux命令] linux shell数据重定向
- linux shell命令设置内存大小运行jar文件
- 【原创】Centos6中yum方法安装sl(linux有趣命令之一sl跑火车)
- Linux Shell脚本编程--tee命令
- linux shell man命令详细介绍-快捷获得帮助信息(2)
- 精选37条强大的常用linux shell命令组合
- Pytohn实现Linux shell中的wc命令
- 使用Bash编写Linux Shell脚本-7.复合命令