博客
关于我
Linux 运维需要掌握的 17 个实用技巧
阅读量:194 次
发布时间:2019-02-28

本文共 2418 字,大约阅读时间需要 8 分钟。

Linux命令实用技巧(17项)

1. 查找当前目录下所有以.tar结尾的文件并移动到指定目录

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

注解:

find命令用于查找文件,-name "*.tar"指定了以.tar结尾的文件,-exec mv {} /backup/则将匹配的文件移动到指定目录。可以使用-type f限制为文件,避免误将目录包含。

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

for i in `find . -name "*.zip" -type f` do  unzip -d /data/www/img/done $idone

注解:

find命令查找所有.zip文件,使用-type f确保只匹配文件。for循环遍历每个文件,使用unzip解压到指定目录。

3. sed命令收集常用用法

  • 去掉行首的.字符:
    sed -i 's/^.//g' test.txt
  • 在行首添加a字符:
    sed 's/^/a/g' test.txt
  • 在行尾添加a字符:
    sed 's/$/a/' test.txt
  • 在特定行后插入字符:
    sed '/wuguangke/ac' test.txt
  • 在行前插入字符:
    sed '/wuguangke/ic' test.txt
  • 替换文件中的目录:
    sed -i 's:/user/local:/tmp:g' test.txt

4. 判断目录是否存在,不存在则新建,存在则打印信息

if [ ! -d /data/backup/ ]; then  mkdir -p /data/backup/else  echo "The Directory already exists, please exit."fi

5. 监控Linux磁盘根分区空间(超过90%时发送邮件)

  • 打印根分区大小:
    df -h | sed -n '/^Filesystem.*\/[^/]/p' | awk '{print $5}'
  • 检查空间是否大于90%:
    while sleep 5; do  df -h | sed -n '/^Filesystem.*\/[^/]/p' | awk '{print $5}' | sed 's/%//g' | awk '{if ($1 >= 90) then email...}'done

6. 统计Nginx访问日志,找出访问量前20的IP地址

cat access.log | awk '{print $1}' | sort | uniq -c | sort -nr | head -20

注解:

sort排序后uniq -c去重统计,sort -nr按访问量降序排列,head -20显示前20条。

7. sed命令用于修改特定行

  • 修改SELINUX状态:
    sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config
  • 替换目录路径:
    sed -i 's:/user/local:/tmp:g' test.txt

8. 打印文件中的最大值、最小值和总和

  • 最大值:
    awk 'BEGIN {max=0} {if ($1+0 > max+0) max=$1} END {print "Max=" max}'
  • 最小值:
    awk 'BEGIN {min=65536} {if ($1+0 < min+0) min=$1} END {print "Min=" min}'
  • 总和:
    cat test.txt | awk '{sum+=$1} END {print "Sum=" sum}'

9. 使用snmpd抓取Cacti v2数据

snmpwalk -v2c -c public 192.168.0.241

10. 修改文本中以“jk”结尾的字符串为“yz”

sed -e 's/jk$/yz/g' b.txt

11. 网络抓包:tcpdump

  • 捕获特定主机和端口的数据包:
    tcpdump -nn host 192.168.56.7 and port 80
  • 排除特定主机:
    tcpdump -nn host 192.168.0.22 and port 80

12. 显示最常用的20条Linux命令

cat .bash_history | grep -v ^# | awk '{print $1}' | sort | uniq -c | sort -nr | head -20

13. 删除最后创建时间小于3天且后缀为*.log的文件

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

14. 将某目录下大小超过100k的文件移动至/tmp

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

15. 防火墙配置:只允许远程主机访问本机的80端口

iptables -Fiptables -A INPUT -p tcp --dport 80 -j ACCEPTiptables -A INPUT -p tcp -j REJECT

16. Nginx日志统计:找出访问IP最多的前10个

cat access.log | awk '{print $1}' | sort -n | uniq -c | sort -nr -k1 | head -10

17. Netcat(nc)的常用用法

  • 端口测试:
    nc -vz 192.168.1.2 8080
  • UDP会话测试:
    nc -u 192.168.1.2 8080
  • 启动nc监听:
    nc -l -p 8080
  • 传输测试(从A到B):
    nc -l -p 8080 | pv

    (A端)

    nc 192.168.1.2 8080 < /dev/zero

    (B端)

转载地址:http://rkpi.baihongyu.com/

你可能感兴趣的文章
Python只运行一次while循环
查看>>
python变量的详细教程_Python零基础入门教程之语法入门[变量](第二期)
查看>>
Python变量命名方法-ChatGPT4o作答
查看>>
Python变量与运算符
查看>>
Python变量/运算符/函数/模块/string
查看>>
python发送邮件的时候出现 error (535, b‘5.7.3 Authentication unsuccessful‘) 解决方法
查看>>
python系列【仅供参考】:python flask框架 debug功能
查看>>
python发送notes邮件_使用python在Lotus Notes中发送邮件
查看>>
Python双版本下创建一个Scrapy(西瓜皮)项目
查看>>
Python双版本下No module named 'requests'
查看>>
python及pycharm2018软件安装教程
查看>>
python去重txt文本_Python实现的txt文件去重功能示例
查看>>
python去掉列表的逗号,从Python列表项中删除标点符号
查看>>
Python卸载所有包
查看>>
python单线程下实现多个socket并发
查看>>
Python单元测试框架介绍(超详细~)
查看>>
Python单元测试框架
查看>>
python单元测试之unittest
查看>>
Python单元测试unittest实战
查看>>
Python单元测试-模拟VS补丁
查看>>