Sed案例總結:php
1.取出some.jpg的權限:node
# stat some.jpg|sed -n 's#^.*(0\([0-7].*\)/-r.*$#\1#gp'mysql 777nginx # stat process.py |sed -rn 's#^.*0([0-7]{3}).*$#\1#pg'sql 644shell |
2.在config_ip.sh的第一行前加上#!/bin/bashcentos
sed '1i\#!/bin/bash' config_ip.sh數組 |
3.在config_ip.sh的第一行前加上空行bash
sed -i '1i\ ' config_ip.shdom |
4.在hosts文件中加兩個域名解析
sed '2a\192.168.1.20 jinfen\n192.168.1.21 xiaoming' /etc/hosts 127.0.0.1 localhost localhost.localdomain VM_137_230_centos www-jfedu-net ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.1.20 jinfen 192.168.1.21 xiaoming |
5.取出config_ip.sh的第二行和第六行的內容
sed -n '2p;6p' config_ip.sh #!/bin/bash while true sed -n -e '2p' -e '6p' config_ip.sh #!/bin/bash while true |
6..取出config_ip.sh的第二行到第六行的內容
sed -n '2,6p' config_ip.sh |
7.優化ssh配置文件(優化的參數見下):
1. Port 52113 2. PermitRootLogin no 3. PermitEmptyPasswords no 4. UseDNS no 5. GSSAPIAuthentication no |
sed -n 's/#Port.*$/Port 52113/pg' /etc/ssh/sshd_config Port 52113 |
sed -n 's/#PermitRootLogin.*$/PermitRootLogin no/gp' /etc/ssh/sshd_config PermitRootLogin no |
8.提取出某一個字符:
echo I am to like you|sed 's#^.*o \([a-z].*\) y.*$#\1#g' like |
echo I am to like you|sed -r 's#^.*o ([a-z]+) y.*$#\1#g' like //-r 擴展正則 |
9.系統開機啓動項優化:
chkconfig --list|grep '3:on'|grep -vE 'sshd|crond|network|reysstat|sysstat'|awk '{print $1}'|sed -r 's#^(.*)#chkconfig \1 off#g'|bash |
echo -e "hello\nword"|sed 's#hello#-----&------#g'-----hello------word |
sed -n '/root/p' /etc/passwdgrep 'root' /etc/passwd |
sed -n '20,/mysql/p' /etc/passwd |
echo "17/Apr/2015:09:29:24 +0800"|sed -nr "s#(..)/(...)/(....):(.*) (.*)#\3-\2-\1 \4\5#;s#Apr#04#p"2015-04-17 09:29:24+0800 |
sed -i '13 iPort 52113\nPermitRootLogin no\nPermitEmptyPasswords no\nUseDNS no\nGSSAPIAuthentication no' sshd_config |
ifconfig eth0|sed -nr 's#.*addr:(.*) Bca.*$#\1#p'10.104.137.230ifconfig eth0|sed -n 's#.*ddr:##;s# Bca.*##p'10.104.137.230 |
# sed '1c\this is root' /etc/passwdthis is root |
sed '/^xiaodong.*$/d' /etc/passwdsed '4,$d' /etc/passwd |
seq 10|sed 'n;d' |
seq 10|sed -n 'n;p' |
# seq 10|sed 'G' |
sed = /etc/fstab |sed 'N;s/\n/\./'1./dev/vda1 / ext3 noatime,acl,user_xattr 1 |
sed -e '/song/s/^/&1./g' passwd1.song1:x:20533:20533::/home/song1:/bin/bash |
name=jiajiesed -n '/'"$name"'/p' 1.ah |
grep 'root' /etc/passwd |
grep -n 'root' /etc/passwd |
egrep -v '^$|^#' /usr/local/zabbix/etc/zabbix_server.conf |
Centos7.不支持egrepgrep -E -v '^#|^ *#|^$' conf/nginx.conf |
grep -n 'jfedu[12]' /etc/passwd32:jfedu1:x:510:510::/home/jfedu1:/bin/bash33:jfedu2:x:511:511::/home/jfedu2:/bin/bash |
grep -n 'jfedu[^1]' /etc/passwd31:jfedu:x:509:509::/home/jfedu:/bin/bash33:jfedu2:x:511:511::/home/jfedu2:/bin/bash34:jfedu999:x:512:512::/tmp//jfedu999:/bin/bash35:jfedu666:x:513:513::/home/jfedu666:/bin/bash49:jfedu001:x:20532:20532::/home/jfedu001:/bin/bash |
grep '[0-9]' which.sh |
grep '^#' which.sh |
grep -v '^$' which.sh |
grep 'j...u' /etc/passwd |
ifconfig eth0|egrep -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'+ 表明匹配前面出現的字符一次或者屢次。-o 只打印匹配的字符ifconfig eth0|egrep -o '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' |
grep -c 'root' /etc/passwd |
egrep -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' access_log|sort|uniq -c|sort -nr|head -10 |
ps -ef|grep mysql|grep -v grep |
ss -tnl|grep 3306LISTEN 0 50 *:3306 *:* |
grep -l root /etc/passwd /etc/shadow /etc/fstab /etc/issue/etc/passwd/etc/shadow |
grep -B 4 xiaoming /etc/passwd //前四行 beforegrep -A 4 xiaoming /etc/passwd //後四行 aftergrep -C 4 xiaoming /etc/passwd //先後四行 |
grep --color=auto 'root' /etc/passwd |
ll |grep '.txt'-rw-r--r-- 1 root root 0 Aug 24 11:29 1.txt-rw-r--r-- 1 root root 139018 Aug 23 16:29 all_php.txt-rw-r--r-- 1 root root 97 Aug 23 14:58 jfedu.txt |
grep '[[:space:]]' jfedu.txt |
grep '[[:punct:]]' jfedu.txt |
ifconfig eth0 |awk -F '[ :]+' 'NR==2 {print $4}' |
last -n 5|awk '{print $1}' |
awk -F: '{print $1}' /etc/passwd |
cat /etc/passwd|awk -F: 'BEGIN{print "name,shell"}{print $1","$7}END{print "ending,/bin/bash"}' |
awk '/root/' /etc/passwdgrep root /etc/passwdsed -n '/root/p' /etc/passwd |
awk -F: '/root/{print $1"-"$7}' /etc/passwd |
awk -F: '{print "filename:"FILENAME",linenumber:"NR",column:"NF",linecontent:"$0}' /etc/passwdawk -F: '{printf("filename:%10s,linenumber:%s,columns:%s,linecontent:%s\n",FILENAME,NR,NF,$0)}' /etc/passwd |
awk 'BEGIN{count=0}{count++;print $0;}END{print "user count is",count}' /etc/passwd |
ls -l|awk 'BEGIN{size=0}{size=size+$5}END{print "size is " size/1024/1024,"M"}'size is 44.5918 M |
ls -l|awk 'BEGIN{size=0;print "[START]size is",size}{if($5!=4096){size=size+$5;}}END{print"[END]size is",size/1024/1024,"M"}'[START]size is 0[END]size is 44.5606 M |
awk -F: 'BEGIN{count=0;}{name[count]=$1;count++;}END{for(i=0;i<NR;i++)print i,name[i]}' /etc/passwd |
awk -F: '$1=="root"' /etc/passwdroot:x:0:0:root:/root:/bin/bash |
echo "I am a bird"|awk '{print NF}'4 |
echo "I am a bird"|awk '{print $NF}'bird |
echo "I am a bird"|awk 'BEGIN{OFS="#"}{print $1,$2,$3,$4}'I#am#a#bird |
awk '/tcp/{print $0}' test.txt |
awk '/blp5/||/3gpp/{print $0}' test.txt |
awk '/blp5/&&/tcp/{print $0}' test.txt |
awk '/3gpp/,/blp5/{print $0}' test.txt |
awk '/^$/{x+=1}END{print x}' /etc/ssh/ssh_config4 |
df|grep 'boot'|awk '{if($4<20000)print"alart";else print "ok"}' |
seq 5|awk '{if($0==3)print $0;else print "no"}' |
ifconfig eth0|awk '/Bcast/'|awk -F'[ :]+' '{print $4}' |
awk '/2017:16:52/,/2017:15:21/' access_log|awk '{print $1}'|sort|uniq -c|sort -nr|head -10 |
awk -F"=" '{print "rpm -e --nodeps "$2}' test.txt
Find命令總結:
語法格式:
find path -option [ -print ] [ -exec -ok command ] { } \; |
1.列出當前目錄及其子目錄下的全部文件及其文件夾:
find . [-print] |
2.根據文件名來查找:
# find . -name "*.txt" ./jfedu.txt ./.jenkins/userContent/readme.txt ./.jenkins/jobs/www.jfedu.net/svnexternals.txt |
3.忽略大小寫:
# find . -iname "*.txt" ./jfedu.txt ./.jenkins/userContent/readme.txt ./.jenkins/jobs/www.jfedu.net/svnexternals.txt ./index.TXT |
4.匹配多個條件中的 -o(or) -a(and)
# find . -name "*.txt" -o -name "*.pdf" ./n.txt ./new.txt ./text.pdf # find . -name "*.txt" -a -name "n.*" ./n.txt |
5.指定最大深度查找目錄 -mindepth 遍歷最小深度
# find . -maxdepth 2 -type d . ./.jenkins ./.jenkins/userContent ./.jenkins/fingerprints ./.jenkins/plugins ./.jenkins/cache ./.jenkins/updates |
6.根據文件類型搜索:
# find . -type f ./n.txt ./which.sh f:普通文件 d:目錄 c:塊設備 l:符號連接 b:套接字 |
7.根據文件時間進行搜索:
# find . -type f -atime -7 打印7天內被訪問過的文件 ./n.txt ./which.sh ./echo_hello.sh ./new.txt -atime 訪問時間 -mtime 文件的最後一次修改時間 -ctime 文件的變化時間(如:權限) 如下是基於分鐘的: -amin 訪問時間 -mmin 修改時間 -ctime 變化時間 + 大於 –小於 |
8.指定比較的時間戳的參考文件,而後找出比參考問間新的文件:
# find . -type f -newer which.sh ./n.txt ./new.txt ./text.pdf ./some.jpg |
9.基於文件的大小進行文件搜索:
# find . -type f -size +1M //大於1M的文件 ./.jenkins/plugins/maven-plugin.jpi ./.jenkins/plugins/subversion.jpi ./.jenkins/plugins/subversion/WEB-INF/lib/svnkit-1.7.10-jenkins-1.jar b 塊設備 c 字節 w 字 k 千字節 M兆字節 G 吉字節 |
10.刪除匹配的文件:-delete
# find . -type f -name "*.swp" -delete |
11.匹配文件權限:-perm
# find . -type f -perm 644 |
12.根據文件的全部權進行搜索:-user
# find / etc -type f -user mysql |
13.結合find執行命令或者動做:
找到/tmp目錄下全部權不是root的文件,並修改爲root權限 # find /tmp/ -type f ! -user root -exec chown root {} \; 找到全部php文件,讀取放到all_php.txt文件中 # find . -type f -name "*.php" -exec cat {} \;>all_php.txt 將10天前的文件複製到old中 # find . -type f -mtime +10 -name "*.txt" -exec cp {} OLD \; |