美文网首页
shell 技巧

shell 技巧

作者: e652d1fb12eb | 来源:发表于2020-12-06 11:10 被阅读0次
1. 查找当前目录下所有以.tar结尾的文件然后移动到指定目录:
find . -name "*.tar" -exec mv {} ./backup/ ;

find -name用于查找某个文件名字,-exec、xargs可以用来承接前面的结果,然后将要执行的动作。延伸,-mtime查找修改时间,-type批定对象类型,如f代表文件,d代表目录,-size指定大小,如经常用到的:查找当前目录30天以前大于100M的log文件并删除:

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

find其它实例:

find . -ctime +365 -exec rm -rf {} \;
find . -ctime +7 | xargs rm -rf
find ./ -size 0 -exec rm {};
find /data/database/tjepd6db/arch/ -type f -mtime +30 -exec rm -f {} \;
find ./*.aud -mtime +3 | wc -l
find . -mtime +30 -name "*.trc/trm" | wc -l 查看一个月之前的trc/trm文件数量
find . -mtime +30 -name "*.trc/trm" | xargs rm -rf  删除一个月之前的trc/trm文件
2.批量解压当前目录下以.zip结尾的所有文件到指定目录:
for i in `find . -name "*.zip" -typef`
do
unzip -d $i /data/www/img/
done
3. sed常用命令收集:
  • 去掉行首的.字符:
sed -i 's/^.//g' test.txt
  • 在行首添加一个a字符
sed 's/^/a/g' test.txt
  • 在行尾添加一个a字符
sed 's/$/a/' test.txt

在行前加入一个c字符:

sed '/wuguangke/ic' test.txt
4. 判断目录是否存在,不存在则新建,存在则打印:
if [ ! -d /data/backup/ ] ; then
mkdir -p /data/backup/
else
echo "The Directory already exists, please exit"
fi
5. 监控linux磁盘根分区,如果根分区空间大于等于90%,发送mail给SA
df -h | sed -n '//$/p' | awk '{print $5}' | awk -F "%" '{print $1}'
  • if条件判断该大小是否大于90,如果大于90则发送邮件报警
while sleep 5m
do
for i in `df -h | sed -n '//$/p' | awk '{print $5}' | sed 's/%//g'`
do
echo $i
if [ $i -ge 90 ]; then
echo "More than 90% Linux of disk space, Please Linux SA check Linux Disk!" | mail -s "Warn Linux / Parts is $i%"
xxx@xxx.xxx
fi
done
done
6. 统计Nginx访问日志,访问量排在前20的ip地址
cat access.log | awk '{print $1}' | sort | uniq -c | sort -nr | head -20
7. sed另外一个用法找到当前行,然后修改该行后面的参数
sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config
8.打印出一个文件里面的最大和最小值
1. cat a.txt | sort -nr | awk '{} END {print} NR==1'
2. cat a.txt | sort -nr | awk 'END {print} NR==1'

这个才是真正打印最大最小值:

sed 's/ / /g' a.txt | sort -nr|sed -n ' 1p;$p'
9. 修改文本中以jk结尾的替换成yz:
sed -e 's/jk$/yz/g' b.txt
10. 网络抓包
tcpdump -nn host 102.168.56.7 and port 80  #抓取56.7通过80请求的数据包
tcpdump -nn host 192.168.56.7 or ! host 192.168.0.22 and port 80 #排除0.22 80端口
11. 显示最常用的20条命令:
cat .bash_history | grep -v ^# | awk '{print $1}' | sort | uniq -c | sort -nr | head 20
12.写一个脚本查找最后创建时间是3天前,后缀是.log的文件并删除
find . -mtime +3 -name "*.log" | xargs rm -fr {} ;
13. 写一个脚本将某目录下大于100K的文件移动到/tmp下
find . -size +100k -exec mv {} /tmp;
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
15. 替换文件中的目录
sed 's:/user/local:/tmp:g' test.txt  #/user/local替换为/tmp
sed -i 's//usr/local//tmp/g' test.txt

相关文章

  • mac shell终端编辑命令行快捷键

    Ctrl-Line技巧提示 Command-Line技巧提示 Open . 技巧提示 参考 mac shell终端...

  • Shell 的基本使用

    这里使用的是Bash 概要语法 小技巧 当前执行shell文件的位置 参考 shell教程

  • Linux Shell 动态生成 数组系列 Seq 使用技巧

    Linux Shell 动态生成 数组系列 Seq 使用技巧 如果对linux shell 数组不是很熟悉的话,请...

  • Shell技巧

    管道命令一个失败时整个失败 有时候我们可能会执行类似这样的命令: cat test.sh |grep if | c...

  • shell 技巧

    1. 查找当前目录下所有以.tar结尾的文件然后移动到指定目录: find -name用于查找某个文件名字,-ex...

  • Hello fish shell

    什么是 fish shell ? 为什么用它? 可以参考 Fish shell 入门教程 和 量化计算中的技巧(一...

  • adb shell am 命令之debug技巧

    adb shell am 命令之debug技巧 Tags: adb_shell 我们一般的调试方式都是先在某个地方...

  • Shell脚本学习指南

    《Shell脚本学习指南》Shell脚本编程(scripting)的技巧永远不会过时:它们可以让UNIX充分发挥其...

  • Shell 脚本中的小技巧

    Shell 脚本中的小技巧 关于变量 一,在shell 脚本中定义默认值 [图片上传失败...(image-54c...

  • shell对文件的操作

    shell对文件的操作 简介 sed命令 sed小技巧 一、简介 在shell脚本编写中,时常会用到对文件的相关操...

网友评论

      本文标题:shell 技巧

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