shell脚本补遗

作者: 前浪浪奔浪流 | 来源:发表于2022-03-16 15:38 被阅读0次

    Linux自动批量增加公钥
    https://blog.csdn.net/heian_99/article/details/105197561
    主机存活监控
    https://blog.csdn.net/heian_99/article/details/105165477
    Redhat机器巡检脚本
    https://blog.csdn.net/heian_99/article/details/114080095
    Shell帮你掌管上千台服务(多线程)
    https://blog.csdn.net/heian_99/article/details/117753840

    1、查找目录下所有以 .zip 结尾的文件移动到指定目录。

    find . -name "*.zip" -exec mv {} ./backup/;
    

    2、查找当前目录 30 天以前大于 100M 的 log 文件并删除。

    find . -name "*.log" –mtime +30 –typef –size +100M | xargs rm –rf {};
    

    3、批量解压当前目录下以 .zip 结尾的所有文件到指定目录。

    for i  in  `find . –name "*.zip" –type f`
    do
      unzip –d $i /data/www/
    done
    注解:for i in (command);do … done 为 for 循环的一个常用格式,其中i为变量,可以自己指定。
    

    4、写一个脚本查找最后创建时间是 3 天前,后缀是 *.log 的文件并删除。

    find . -mtime +3  -name "*.log" | xargs rm -rf {};
    

    5、写一个脚本将某目录下大于 100k 的文件移动至 /tmp 下

    find . -size +100k -exec mv {} /tmp;
    

    6、如何判断某个目录是否存在,不存在则新建,存在则打印信息。

    if [ ! –d /data/backup/ ];then
       mkdir –p /data/backup/
    else
       echo  "目录已存在"
    fi
    -d 代表目录
    

    7、替换文件中的目录

    sed 's:/user/local:/tmp:g' test.txt
    或者
    sed -i 's//usr/local//tmp/g' test.txt
    

    8、sed 常用命令

    如何去掉行首的.字符: sed -i 's/^.//g' test.txt
    
    在行首添加一个a字符: sed 's/^/a/g'    test.txt
    
    在行尾添加一个a字符: sed 's/$/a/'     tets.txt
    
    在特定行后添加一个z字符:sed '/rumen/az' test.txt
    
    在行前加入一个c字符: sed '/rumenz/ic' test.txt
    

    9、sed 另外一个用法找到当前行,然后在修改该行后面的参数

    sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config
    sed 冒号方式
    sed -i 's:/tmp:/tmp/abc/:g' test.txt意思是将/tmp改成/tmp/abc/。
    

    10、统计 Nginx 访问日志 访问量排在前20的ip地址

    cat access.log |awk '{print $1}'|sort|uniq -c |sort -nr |head -20
    注解:sort 排序、uniq(检查及删除文本文件中重复出现的行列 )
    

    11、修改文本中以ab 结尾的替换成 cd:

    sed -e 's/ab$/cd/g' b.txt
    

    12、网络抓包:tcpdump

    #抓取 56.7 通过80端口请求的数据包。
    tcpdump -nn host 192.168.56.7 and port 80
    #排除0.22 80端口
    tcpdump -nn host 192.168.56.7 or ! host 192.168.0.22 and port 80
    

    13、统计 bash_history 最常用的 20 条命令

    history | awk '{print $2}' | sort | uniq -c | sort -k1,1nr | head -10
    

    14、配置防火墙脚本,只允许远程主机访问本机的 80 端口

    iptables -F
    iptables -X
    iptables -A INPUT -p tcp --dport 80 -j accept
    iptables -A INPUT -p tcp -j REJECT
    或者
    iptables -A INPUT -m state --state NEW-m tcp -p tcp --dport 80 -j ACCEPT
    

    相关文章

      网友评论

        本文标题:shell脚本补遗

        本文链接:https://www.haomeiwen.com/subject/tsupdrtx.html