用到的時候措手不及,不用的時候一大坨. 基於這個緣由,打算重整旗鼓,常常用到的命令和栗子整理以下javascript
像是割草同樣,我不信搞不完全.搞不順手.html
find+xargs/sed&sed後向引用+awk多匹配符+過濾行絕招總結&&產生隨機數java
sort-uniq
awk運算-解決企業統計pv/ip問題mysql
1.mkdir
2.ls -l -d 顯示目錄 -F 給文件夾結尾加/標識 -a 以.開頭的都是隱藏文件 -rt 按照修改時間倒序排列(最新修改的在最下) ls -lrth
3.cd
4.pwd
5.touch
6.vi
7.vim
8.echo 配合 > >> -n 不換行 -e 內容攜帶轉義(\n回車 \t tab) - 不換行 [root@n6 ~]# echo -n '123' 123[root@n6 ~]# - 讓\n等轉義 默認是: [root@n6 ~]# echo 'mao\ntai' mao\ntai 加-e後 [root@n6 ~]# echo -e 'mao\ntai' mao tai
9.cat -n 顯示行號
10.xargs: http://man.linuxde.net/xargs -n max-args 多少個一組,默認是1 -i [replace-str] 後向引用 - 用法展現 echo stu{1..20}|xargs -n 2 > 2.md - 單行輸出(默認-n1) cat test.txt a b c d e f g h i j k l m n o p q r s t u v w x y z cat test.txt | xargs a b c d e f g h i j k l m n o p q r s t u v w x y z cat test.txt | xargs -n3 a b c d e f g h i j k l m n o - 查找替換(處理木馬) find . -type f|xargs sed -i 's#<script type="text/javascript" src=http://%4%66ccxxx&W@#@$@#$@$%@#$SFADS@#$></script>##g'; - 在移動和重命名時候才須要 -i,不然不須要 find . -type f -name '1.md'|xargs -i mv {} /tmp/ find . -type f -name '1.md'|xargs -i mv {} /tmp/{}.bak
11.cp -a 等價於-pbr -f(force) -r(recursive)遞歸
12.rm 1.以tmp爲回收站 2.先cd,後find . -name ""|xargs rm -f
13.mv
14.find: http://www.cnblogs.com/iiiiher/p/8507948.html -type f(file) d(directory) c(character) b(block) s(socket) l(link) -name -size -mtime 修改時間: +7(超過7天) 7 -7(7天內) -maxdepth 查找深度 ! 取反, -a(and)交集(默認), -o or並集 -exec - 移動或者重命名時xargs 須要-i後向引用. find . -type f -name '1.md'|xargs -i mv {} /tmp/ - 刪除 find . -type f -mtime +7 |xargs rm -f - 刪除 find . -type f -mtime +7 -exec rm -f {} \;
15.grep:三劍客老三 -i 不區分大小寫 -n 對匹配到的內容顯示行號 -v 排除: 生產監控進程: ps -ef|grep "/sshd"|grep -v "grep" -E 等價於egrep '1|2' -o 只顯示匹配到的內容 -P 使用perl正則 -ABC 栗子: - 過濾ip(注意192. 的點要轉義) [root@n1 ~]# ifconfig eth0|grep -oP "([0-9]{1,3}\.){3}([0-9]{1,3})" 192.168.2.11 255.255.255.0 192.168.2.255 - 優化啓動項 [root@n6 ~]# chkconfig --list|grep -E "rsyslog|sshd|network|crond|sysstat" |awk '{print "chkconfig",$1,"on"}' chkconfig crond on chkconfig network on chkconfig rsyslog on chkconfig sshd on chkconfig sysstat on - 精簡nginx配置 egrep -v '^$|#' nginx.conf.default > nginx.conf
21.sed -n 取消默認輸出,僅輸出匹配想要的 -p 打印, sed -n'20'p a.log; sed -n '20,30'p a.log 過濾功能(正則):sed -n '/^d/p' g與s聯合使用,表示替換: sed -i 's#maotai#maotai#g' a.log #是分隔符 sed過濾功能: ls -l|sed -n '/^d/p' ls -l|sed -n '/\/$/p' 過濾文件權限:stat maotai.txt |sed -nr '4s#^.*\(0(.*)/-.*$#\1#gp' 過濾ip: ifconfig eth0|sed -nr '2s#^.*net (.*) net.*$#\1#gp' 栗子: sed 後向引用是一個絕招
22.awk #過濾/輸出內容,一門語言. NR 行號: awk 'NR>30 && NR<40' awk 'NR>30 && NR<40 {print $2}' m.txt $1 第一列 $2第二列 $0整個行: awk '{print $1,$2}' awk '{print $1"#"$2}' awk '{if($2>1) print$0}' cat svc.txt|grep -E "rsyslog|sshd|network|crond|sysstat" |awk '{print "chkconfig",$1,"on"}' 顯示行號: awk '{print NR,$0}' 過濾功能(正則): 正則匹配: awk '/^d/' (過濾目錄)ls -l|awk '/^d/' (過濾目錄)ls -l|awk '/\/$/' -F 分隔符 ,以多個分割符分割 awk -F '[: ]+' 過濾文件權限: stat maotai.txt |sed -nr '4s#^.*\(0(.*)/-.*$#\1#gp' 過濾ip: ifconfig eth0|sed -nr '2s#^.*net (.*) net.*$#\1#gp' 栗子: awk多分割符是一個絕招 過濾權限 過濾ip awk運算是一個絕招: http://www.cnblogs.com/iiiiher/p/8576537.html - top url - top ip - tcp11種狀態統計
16.head -n5: 顯示前5行, 習慣head -5 -c: 顯示str的多少個字符 栗子 - 生產隨機數 [root@n1 ~]# echo $RANDOM|md5sum|head -c10 bcb6293ae4[root@n1 ~]#
17.tail
18.alias
19.unalias
20.seq sequence -s指定分割符 seq 開始 結束: seq 1 10 seq 開始 公差 結束: seq 1 2 10 栗子: [root@n6 ~]# seq 1 2 10 1 3 5 7 9 [root@n6 ~]# - 指定空格爲分隔符 [root@n6 ~]# seq -s ' ' 1 2 10 1 3 5 7 9 - for循環 for i in `seq 1 10`;do echo $i;done for i in {1..10};do echo $i;done 對比{1..10}
23,useradd
24,passwd 非交互式改密碼: echo "12345"|passwd --stdin maotai
25.uname: -m 32or64 -r 內核版本 -a(all) -n(主機名)
26.
27.
28,init: 切換運行級別,後面接對應級別的數字,例如: init 6重啓, init 0關機.
29.shutdown -h now -r -h
30.reboot(init 6)重啓, shutdown -r now last, 對應/var/log/wtmp; 成功登陸用戶 lastb, 對應/var/log/btmp; 嘗試登陸信息 lastlog,對應/var/log/lastlog; 顯示最近登陸信息 > /var/log/wtmp > /var/log/btmp > /var/log/lastlog > /root/.bash_history - 不記錄日誌 $ <空格>command /etc/profile HISTSIZE=1000設置爲0,不記錄
32.dmeseg 顯示系統故障信息
33.ifup和ifdown, 啓動中止網卡
34.nl number lines,顯示文件行號
35.less 和more相反,回車一次一行,空格一次一屏,按b能夠一次回退一屏.
36.more (不經常使用) 按頁一次一屏,不能回退
37, wc -l(lines) 顯示總行數 生產監控進程: ps -ef|grep "/sshd"|grep -v "grep"|wc -l
38,chkconfig 設置開機自啓動,默認管理2345級別 也能夠:/etc/rc.local --list 顯示全部 --list sshd --level 234 chkconfig sshd on/off chkconfig --level 234 sshd on/off chkconfig --list #列出全部的系統服務 chkconfig --add httpd #增長httpd服務 chkconfig --del httpd #刪除httpd服務 chkconfig --level httpd 2345 on #設置httpd在運行級別爲二、三、四、5的狀況下都是on(開啓)的狀態 chkconfig --list #列出系統全部的服務啓動狀況 chkconfig --list mysqld #列出mysqld服務設置狀況 chkconfig --level 35 mysqld on #設定mysqld在等級3和5爲開機運行服務,--level 3
39,tar 打包 z c v f j x X N p P C --exclude tar -cvf test.tgz test/ --exclude *.txt --exclude dir1 參考: http://www.cnblogs.com/iiiiher/p/8571517.html
40,cut 切割 取列 -d分隔符 -f取列 -c字符 -d, --delimiter=DELIM 以...分割 -f, --fields=LIST 第幾列 -c, --characters=LIST 取多少個字符 -c2-10 栗子: - 取passwd第一列 cat /etc/passwd|cut -d ':' -f 1 - 取隨機數 [root@n1 ~]# echo $RANDOM|md5sum|cut -c 1-10 6241129142
41,tr 逐個字符替換 [root@n1 ~]# echo {a..z}|tr '[a-z]' '[A-Z]' A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
42stat 顯示文件和文件系統狀態(查看文件屬性)
43,file 查看文件的類型
44.last 查看用戶登陸信息 /var/log/wtmp數據文件
45,lastlog 顯示最近用戶登陸信息, /var/log/wtmp
46,df -h
47.dumpe2fs 查看文件系統的內部信息(元數據)
48,tree 查看目錄 -L level: Max display depth of the directory tree 取dir多少層 -d List directories only:僅顯示目錄 栗子: - 僅顯示一層目錄 tree -Ld 1 /
51.du -sh # 查看文件和目錄的大小 -h, --human-readable -s, --summarize: display only a total for each argument, 顯示大小. -s和-d衝突,同時僅能用一個 -d(depth), --max-depth=N: 查看幾級目錄 栗子: - (面試題)僅查看一級的目錄大小(結合tree -Ld 1 /) du -hd 1 / du -sh /* #這個也ok - 查看某個dir大小 du -sh .
49,id 查看用戶和組的信息
50,ln 建立軟硬連接 -s軟(readlink)
52.which 從PATH變量所在路徑查找程序路徑
curl命令 -I --max-time <seconds> # 超時時間 -o/--output # 把輸出寫到該文件中 -s/--silent # 靜音模式。不輸出任何東西 -w/--write-out [format] # 什麼輸出完成後 -A/--user-agent <string> # 設置用戶代理髮送給服務器 -b/--cookie <name=string/file> # cookie字符串或文件讀取位置 -c/--cookie-jar <file> # 操做結束後把cookie寫入到這個文件中 -C/--continue-at <offset> # 斷點續轉 -D/--dump-header <file> # 把header信息寫入到該文件中 -e/--referer # 來源網址 -f/--fail # 鏈接失敗時不顯示http錯誤 -O/--remote-name # 把輸出寫到該文件中,保留遠程文件的文件名 -r/--range <range> # 檢索來自HTTP/1.1或FTP服務器字節範圍 -T/--upload-file <file> # 上傳文件 -u/--user <user[:password]> # 設置服務器的用戶和密碼 -x/--proxy <host[:port]> # 在給定的端口上使用HTTP代理 -#/--progress-bar # 進度條顯示當前的傳送狀態 - 獲取狀態碼頭部 curl -I -m 10 -o /dev/null -s -w %{http_code} cnblogs.com
wget - 測速(能夠看到三大isp的速度) wget -qO- bench.sh | bash
lsof lsof -i:80 # 根據端口查進程 查看80對應的進程名 lsof -p 32555 # (**ms**)根據進程id查進程打開的文件 查看pid爲32555打開的文件
ipcs - provide information on ipc facilities ipcs -m # (**ms**) 查看共享內存
[參考](http://topspeedsnail.com/clear-last-linux-login-log/) history: /etc/profile HISTSIZE=1000設置爲1 -c clear清空 -r 清除當前session的 -w 當即更新 $ <空格>command # 不記錄 $ cat .bash_history # 刪除指定行
last, 對應/var/log/wtmp; 成功登陸用戶 lastb, 對應/var/log/btmp; 嘗試登陸信息 lastlog,對應/var/log/lastlog; 顯示最近登陸信息
特殊變量: PATH 全部命令的路徑所在地,用冒號隔離 LANG 字符集變量
1,過濾出已知當前目錄下test/中全部一級目錄(不含test/下的子目錄的子目錄,及隱藏目錄) 方法1: 目錄是d ls -l|grep "^d" 方法2: 給目錄加上/ ls -lF|grep "\/$" 方法3: find找一層,排除當前目錄 [root@n1 test]# find ./ -maxdepth 1 -type d ! -name '.' ./dir1 ./dir2 方法4: 第二列(硬鏈接大於1的) ls -l|awk '{if($2>1) print$0}' 方法5: sed過濾功能(正則) ls -l|sed -n '/^d/p' ls -l|sed -n '/\/$/p' 方法6: awk過濾功能(正則) ls -l|awk '/^d/' ls -l|awk '/\/$/'
2.查找最新建立的兩個文件/ - 造數據 for i in `seq 14`;do date -s "2018/01/$i";touch access_www_$(date +%F).log;done [root@n1 test]# find . -type f -mtime +7 ./access_www_2018-01-01.log ./access_www_2018-01-02.log ./access_www_2018-01-03.log ./access_www_2018-01-04.log ./access_www_2018-01-05.log ./access_www_2018-01-06.log ./access_www_2018-01-07.log ./access_www_2018-01-08.log ./access_www_2018-01-09.log ./access_www_2018-01-10.log ./access_www_2018-01-11.log ./access_www_2018-01-12.log ./access_www_2018-01-13.log ./access_www_2018-01-14.log find . -type f -mtime +7 |xargs rm -f find . -type f -mtime +7 -exec rm -f {} \; rm -f `find . -type f -mtime +7`
3.打印文件內容帶行號: 造數據: echo stu{1..20}.md|xargs -n 1 > nginx.conf cat -n nginx.conf grep -n . nginx.conf vim: set nu awk '{print NR,$0}' nginx.conf
第三波測試:linux
[root@n1 ~]# stat maotai.txt |sed -nr '4s#^.(0(.)/-.*$#\1#gp'
644nginx
- 顯示/tmp下的文件和目錄 [root@n1 ~]# tree /tmp/ /tmp/ ├── 1.md └── dir1 └── dir11 - 顯示/tmp下的目錄 [root@n1 ~]# tree /tmp/ -d /tmp/ └── dir1 └── dir11 - 顯示/tmp下的一級目錄 [root@n1 ~]# tree -Ld 1 /tmp/ /tmp/ └── dir1
參考:
深刻淺出linux三劍客之sed必殺技一例
深刻淺出linux三劍客之awk必殺技一例面試
- 一個空目錄的硬連接數是2 [root@n1 tmp]# mkdir dir1 [root@n1 tmp]# ls -ldi dir1/ 1045209 drwxr-xr-x 2 root root 6 Mar 4 16:52 dir1/ [root@n1 tmp]# ls -ldi dir1 dir1/. 1045209 drwxr-xr-x 2 root root 6 Mar 4 16:52 dir1 1045209 drwxr-xr-x 2 root root 6 Mar 4 16:52 dir1/. - 建立一個子目錄: 硬鏈接變成了3 [root@n1 tmp]# mkdir dir1/dir11 [root@n1 tmp]# tree dir1/ dir1/ └── dir11 [root@n1 tmp]# ls -ldi dir1 dir1/. dir1/dir11/.. 33980567 drwxr-xr-x 3 root root 19 Mar 4 16:51 dir1 33980567 drwxr-xr-x 3 root root 19 Mar 4 16:51 dir1/. 33980567 drwxr-xr-x 3 root root 19 Mar 4 16:51 dir1/dir11/. 這是由於: 1.建立的目錄自己爲一個硬連接 2.新木路下隱藏目錄(點好)爲建立的新目錄的一個硬連接,也算一個連接數,所以,硬連接數是2.
參考sql
參考:
深刻淺出linux三劍客之sed必殺技一例
深刻淺出linux三劍客之awk必殺技一例vim
方法1: awk多個字符分割,打印某行的某列 [root@n1 tmp]# ifconfig eth0|awk -F '[ ]+' 'NR==2 {print $3}' 192.168.2.11 方法2: [root@n1 tmp]# ifconfig eth0|sed -nr '2s#^.*net (.*) net.*$#\1#gp' 192.168.2.11 先定位行: sed -n '2p' awk {NR>20&&NR<30} 在定位列
grep -v '^$' sed '/^$/d' 1.txt awk '/^[^$]/' 1.txt #表示過濾非空行的開頭,過濾出以非空行的行,就是過濾出非空行.
Linux系統基礎網絡配置老鳥精華篇
深刻淺出之-route命令實戰使用指南bash
[root@n1 test]# find . -type f |xargs sed -n 's#maotai#maomao#gp' maomao [root@n1 test]# find . -type f -exec sed -n 's#maotai#maomao#gp' {} \; maomao
當網站打開時候,就會調用這個地址,顯示一個廣告條,形成惡劣的影響.
思路:
遍歷全部文件,把以上被植入的內容刪除掉.
find . -type f -exec sed -i 's###g' {} ;
find . -type f|xargs sed -i 's###g';
處理過程: 1,運營發現問題,確認狀況 2,制定處理方案,先備份已有數據,而後執行命令批量修改替換 3,寫解決說明,留存 4,查看問題來源 5.亡羊補牢方案(站點目錄權限及上線發佈思路)
從發現到解決: 1,運營人員,網站用戶發現問題,網站有彈出 2,運營人員報告給開發,開發聯繫運維共同處理 3,開發發現的問題緣由就是全部的站點目錄被嵌入js代碼 4,運維解決問題,a:備份原始出問題的文件 b:歷史備份覆蓋 c:find+sed替換 5,詳細查日誌,尋找發生來源 6,提供亡羊補牢方案(站點目錄權限及上線發佈思路)
[root@n1 test]# echo 'mao';echo 'tai' mao tai [root@n1 test]# echo -n 'mao';echo 'tai' maotai [root@n1 test]# echo -ne 'mao\t';echo 'tai' mao tai [root@n1 test]# echo -e 'mao\ntai' mao tai
date -s "2019/03/04 18:33" [root@n1 test]# date +%Y-%m-%d 2018-03-04 [root@n1 test]# date +%F 2018-03-04 [root@n1 test]# date +%w 0 [root@n1 test]# date +%Y-%m-%d\ %H:%M:%S 2018-03-04 18:35:10 [root@n1 test]# date +%F\ %X 2018-03-04 06:35:48 PM 打包帶時間: tar zvcf test_$(date +%F).tar.gz /tmp/ tar zvcf test_$(date +%F -d "-1day").tar.gz /tmp/ 前一天 [root@n1 test]# date +%F -d "-1day" 2018-03-03 [root@n1 test]# date +%F -d "+24Hour" 2018-03-05