Linux運維常用命令及知識
1、查找當前目錄下所有以.tar結尾的文件然后移動到指定目錄: find . -name “*.tar” -exec mv {} ./backup/ ; 查找當前目錄30天以前大于100M的LOG文件并刪除。 find . -name "*.log" –mtime +30 –type f –size +100M |xargs rm –rf {} ; 寫一個腳本查找最后創建時間是3天前,后綴是*.log的文件并刪除。 find . -mtime +3 -name "*.log" |xargs rm -rf {} ; 寫一個腳本將某目錄下大于100k的文件移動至/tmp下。 find . -size +100k -exec mv {} /tmp ; 2、批量解壓當前目錄下以.zip結尾的所有文件到指定目錄: for i in `find . –name “*.zip” –type f ` do unzip –d $i /data/www/img/ done 如何去掉行首的.字符: sed -i 's/^.//g' test.txt 在行首添加一個a字符: sed 's/^/a/g' test.txt 在行尾添加一個a字符: sed 's/$/a/' tets.txt 在特定行后添加一個c字符: sed '/wuguangke/ac' 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 注解:if …;then …else ..fi:為if條件語句,!嘆號表示反義“不存在“,-d代表目錄。 5、監控linux磁盤根分區,如果根分區空間大于等于90%,發送郵件給Linux SA (1)、打印根分區大小 df -h |sed -n '//$/p'|awk '{print $5}'|awk –F ”%” '{print $1}' (2)、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%” wugk@map.com 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 Sed冒號方式 sed -i ‘s:/tmp:/tmp/abc/:g’test.txt意思是將/tmp改成/tmp/abc/。 11、打印出一個文件里面最大和最小值: cat a.txt |sort -nr|awk ‘{}END{print} NR==1′ cat a.txt |sort -nr |awk ‘END{print} NR==1′ 這個才是真正的打印最大最小值:sed ‘s/ / /g’ a.txt |sort -nr|sed -n ’1p;$p’ 13、修改文本中以jk結尾的替換成yz: sed -e ‘s/jk$/yz/g’ b.txt 14、網絡抓包:tcpdump tcpdump -nn host 192.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端口! 16、顯示最常用的20條命令: cat .bash_history |grep -v ^# |awk ‘{print $1}’ |sort |uniq -c |sort -nr |head -20 19、寫一個防火墻配置腳本,只允許遠程主機訪問本機的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 20、寫一個腳本進行nginx日志統計,得到訪問ip最多的前10個(nginx日志路徑:/home/logs/nginx/default/access.log)。 cd /home/logs.nginx/default sort -m -k 4 -o access.logok access.1 access.2 access.3 ..... cat access.logok |awk '{print $1}'|sort -n|uniq -c|sort -nr |head -10 21.寫出下列命令的含義 (1)MaxKeepAliveRequests 100 連接的最大請求數 (2)Options FollowSymLinks 允許192.168.1.1可以列目錄 Order Deny Allow Deny from all Allow from 192.168.1.1 22.替換文件中的目錄 sed 's:/user/local:/tmp:g' test.txt 或者 sed -i 's//usr/local//tmp/g' test.txt
posted on 2014-09-11 10:43 順其自然EVO 閱讀(391) 評論(0) 編輯 收藏 所屬分類: 測試學習專欄