zoukankan      html  css  js  c++  java
  • Linux 下常用的Shell 命令

    英文原文链接: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风格的语法(允许像 这样的字符)

    grep -P "	"
    

    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



  • 相关阅读:
    写了个批量下载抖音无水印视频的小软件。
    ffmpeg转换参数码
    WPF
    使用EF的Code First模式创建模型
    桌面置顶显示服务器信息
    Assert.assertEquals
    XML报文解析思路
    定时任务,cron七域
    检查网络是否通畅
    Ngnix运行vue项目
  • 原文地址:https://www.cnblogs.com/yizhiamumu/p/8999510.html
Copyright © 2011-2022 走看看