天天看點

Linux運維常用指令及知識

  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代表目錄。

  (1)、列印根分區大小

  df -h |sed -n '//$/p'|awk '{print $5}'|awk –F ”%” '{print $1}'

  (2)、if條件判斷該大小是否大于90,如果大于90則發送郵件報警

  while sleep 5m

  for i in `df -h |sed -n '//$/p' |awk '{print $5}' |sed 's/%//g'`

  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%”

  [email protected]

  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

文章署名:flycars001

本文轉自 linuxzkq 51CTO部落格,原文連結:http://blog.51cto.com/linuxzkq/1583236