博客
关于我
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/

你可能感兴趣的文章
python4.0 gil_Python 并发编程系列--1、浅谈GIL历史及对多线程的影响
查看>>
python60行绘图程序_天底下最简单的QT画图板,就一个类,60行代码
查看>>
Python: AES加密与解密
查看>>
Python: Range() 最大尺寸;动态的还是静态的?
查看>>
Python: Windows下用 multiprocessing 的深坑
查看>>
python:Could not find platform independent libraries <prefix>解决方法
查看>>
Python:datetime tzinfo 时区名称文档
查看>>
Python:deepcopy(list) vs new_list = old_list[:]
查看>>
Python:Google API - 从消息中获取 mimeTypes
查看>>
Python:Google API - 从消息中获取 mimeTypes
查看>>
Python:subprocess.call,stdout to file,stderr to file,在屏幕上实时显示stderr
查看>>
Python:TypeError:无法连接“str“和“int“对象
查看>>
Python:XPath 在 ElementTree 中不可用
查看>>
Python:从 FTP 服务器下载文件
查看>>
python:从列表中创建元组列表
查看>>
Python:从嵌套列表中删除重复项
查看>>
Python:从间隔映射到值
查看>>
Python:使用 shell=False 的子进程调用不起作用
查看>>
Python:修改 PATH 环境变量的平台无关方式
查看>>
Python:假与 0
查看>>