Shell命令和技巧

  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

  1. 检查资源的使用情况

    /usr/bin/time -v ls

  2. 文件的随机行

    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

  3. 在离开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来完成此目的。

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

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

  2. 从两个排序文件中合并行

    comm file1 file2

打印这三列:

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

使用选项-1、-2、-3,可以删除这些列。

  1. 在文件中分割长文件,使用相同数量的行

    split -l LINES -d file.txt output_prefix

  2. 刷新交换分区

如果一个程序消耗了太多的内存,交换分区就会被剩余的内存填满,当你回到正常的时候,一切都是缓慢的。只需重新启动交换分区来修复它:

sudo swapoff -a
sudo swapon -a
  1. 修复ext4文件系统的superblock问题

    sudo fsck.ext4 -f -y /dev/sda1
    sudo fsck.ext4 -v /dev/sda1
    sudo mke2fs -n /dev/sda1
    sudo e2fsck -n

  2. 创建给定大小的空文件

    fallocate -l 1G test.img

  3. 从命令行操作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
  1. 根据生成的输出监视进度

    # 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

  2. 在Ubuntu中找到一个文件的包

    apt-file update
    apt-file search dir/file.h


转自:https://www.jb51.net/article/125194.htm
非常有用啊!

猜你喜欢

转载自www.cnblogs.com/music378/p/10137684.html
今日推荐