Skip to content

Instantly share code, notes, and snippets.

@m41039
Last active December 27, 2017 01:14
Show Gist options
  • Select an option

  • Save m41039/6f4848024834b47cee3c91309675a654 to your computer and use it in GitHub Desktop.

Select an option

Save m41039/6f4848024834b47cee3c91309675a654 to your computer and use it in GitHub Desktop.
若要即時監控使用中的連線資訊,可以使用 watch :
watch -d -n0 "netstat -atnp | grep ESTA"
列出完整的 URL 位址
netstat -tup -W
這個指令可以將所有連線的 IP 位址列出來,並依照每個 IP 位址的連線數排序
netstat -an | grep ESTABLISHED | awk '/^tcp/ {print $5}' | awk -F: '{print $1}' | sort | uniq -c | sort -nr
Linux如何找出佔用較大空間的檔案
du -h --max-depth=1 (–max-depth是表示查詢子目錄的層級)
像如要找到home下最大前5名如下
du -a /home | sort -n -r | head -n 5
find 指令使用範例教學
https://blog.gtwang.org/linux/unix-linux-find-command-examples/
利用 find指令如
find / -type f -size +5G
我們可以利用此種方式找出大於5G 的檔案
find / -type f -exec du {} \; 2>/dev/null |sort -n | tail -n 10 | xargs -n 1 du -h 2>/dev/null
"find / -type f" 的意思是「搜尋根目錄中的所有檔案」。
-exec du {} \;" 代表「每個找到的檔案都用 du 指令執行以取得以 bytes 為單位的檔案大小資訊」。
"2>/dev/null" 是指將所有的錯誤訊息丟棄。
"sort -n" 會將所有的檔案依大小列出,
"tail -n 10" 則是顯示最後 10 筆,兩個指令合起來就會顯示出依大小排序的前 10 大檔案
1. 用find指令在Linux上列出舊的檔案
find /var/log/apache2 -mtime +30
上面這個指令的意思是在/var/log/apache2底下,-mtime是限定修改時間(單位是24小時),後面接著的數字的表示法如下:
+n:表示比n單位以上的檔案 (例如:+30,代表30天之前)
–n:表示比n單位之內的檔案 (例如:-30,代表30天之內)
n:不給給正負號代表正好n單位的檔案。
此外還有time跟min也都是代表修改時間,但是後面接的數字的單位變成分鐘。此外其他相關的指令如下:
-mtime、-mmin:檔案內容(data)最後的修改(modify)時間
-atime、-amin:檔案最後被存取(access)的時間
-ctime、-cmin:檔案狀態(status)最後被修改的時間(change)。 (註:檔案狀態是指:修改擁有者或群組(即調用chown、chgrp)、修改檔案存取權限(即調用chmod)
2. find資料後執行刪除指令
find到檔案後可以用-exec指令來進行相應的處理,下面這個指令示範的是直接下rm指令刪除
find /var/log/apache2 -mtime +30 -exec rm -f {} ;
-exec是表示找到檔案後要執行的指令,現在要執行移除檔案的指令rm,但rm指令會與使用者確認是不是要刪除檔案,因為每個檔案都會問所以挺麻煩的所以加入-f (force) 參數讓rm強制移除檔案。然後 {} 則代表指令接檔案名稱的地方,
find會將找到檔案的檔名與路徑帶入到{} 這邊。最後加入; 代表指令結束,不可以省略。
希望這樣介紹可以讓大家更了解find這個指令。
用find指令搭配xargs把33天前的檔案list出來並且搬移到指定目錄
find . -type f -mtime -34 | sudo xargs -I '{}' mv '{}' /opt/_customer/resume/parse/1900000348/KSHR/
首先就 find 相關的參數作說明:
find /tmp/ -type f -name "*.txt"
/tmp/ 要尋找的目標資料夾
-type f 尋找類型為檔案
-name "*.txt" 尋找檔名為 .txt 結尾
若有使用萬用字元星號*的話建議加上引號,如 "*.txt" 或 '*.txt',
否則在 find 時似乎會有不明錯誤發生,有些該找到的檔案會找不到!
接著說明 -exec 後方的部分
-exec cp {} ~/txt \;
cp 指令
{} 找到的結果檔案
~/txt cp 的參數
\; 指令的結束符號
下面這行指令就是找到 /tmp/ 資料夾底下超過一天的 .log 檔,然後把他們都刪掉,
其中 -mtime +1 就是要找超過一天沒有被修改的檔案。
# find /tmp/ -type f -name "*.log" -mtime +1 -exec rm -rf {} \;
這篇是個簡單的速記,就不囉唆。只要使用以下指令即可:
$ find * -maxdepth 0 -mtime +365 -exec rm -r {} \;
其中幾個重點說明如下:
使用 find 可以找出符合特定條件的檔案並對該檔案做點事情(下指令)
-maxdepth 是用來限制 find 往下搜尋的層數,由於我僅針對此目錄下的所有目錄做處理,故僅使用「find * -maxdepth 0」
-mtime 是針對 modified time 做限制,後面接 +n 表示「早於 n 天前修改的檔案(比 n 天還舊)」,若是接上 -n 則表示「晚於 n 天前修改的檔案(比 n 天還新)」。類似的選項還有 atime, amin, ctime, cmin, mmin。可自行查看 manpage。值得注意的是,manpage 中是以「n*24 hours」來說明,不曉得是否有何特殊之處?
-exec 則是針對找到的檔案進行動作,後面接上要執行的指令,並以 {} 代表找到的檔案,結尾並加上 \; 即可。
網路上找到的通常會是刪除「老舊檔案」而非目錄,以下亦附上參考用法。
$ find . -mtime +365 -exec rm {} \;
開機如果跟nfs mount有關,無法mount vm,無法開機時請下指令
mount -o remount,rw /
for指令
for file in `ls /etc/autofs |grep "172.30.12" * | awk -F ":" '{print $1}' | uniq`;do sudo sed -i 's/172.30.12.252:\/vol\/vldata/172.19.1.21:\/data/' $file ;done
計算linux mem使用率
free | grep Mem | awk '{print $4/$2 * 100.0}'
free | grep Mem | awk '{ printf("free: %.0f %\n", $4/$2 * 100.0) }'
rsync 最簡單的用法就是複製本地端的檔案:
rsync -avh myfile.gz /home/pi/tmp/
sending incremental file list
myfile.gz
sent 14.34M bytes received 35 bytes 28.67M bytes/sec
total size is 14.33M speedup is 1.00
其效果就跟 cp -r 類似,可將 myfile.gz 複製到 /home/pi/tmp/ 目錄中,不過如果執行第二次時,rsync 就會自動跳過沒有變動的檔案:
rsync -avh myfile.gz /home/pi/tmp/
sending incremental file list
sent 74 bytes received 12 bytes 172.00 bytes/sec
total size is 14.33M speedup is 166,658.15
這種用法對於檔案或目錄都適用:
rsync -avh /path/to/myfolder /home/pi/tmp/
而這裡我們多加入一個 -z 參數,目的是讓 rsync 可以自動將資料壓縮後再傳送,並在遠端接收到資料後自動解壓縮,減少網路傳輸的資料量。
rsync 也可以將遠端的檔案備份至本地端,其語法也跟 scp 類似:
rsync -avzh pi@192.168.1.12:/mypath/myfile.gz /mybackup/
rsync -avzh -e 'ssh -p 12345' /mypath/myfile.gz pi@192.168.1.12:/mybackup/
這裡我們多加入一個 -e 參數,其用途是指定遠端登入所要使用的指令,預設的指令就是 ssh,而這裡我們將指令變更為 ssh -p 12345,也就是使用 12345 這個連接埠登入 ssh 的意思(請參考 ssh 指令的 -p 參數用法)。
顯示傳輸進度
如果要讓 rsync 在傳輸檔案時可以即時顯示進度,可以加上 --progress 參數:
rsync -avzh --progress pi@192.168.1.12:/mypath/myfile.gz /mybackup/
如果您想要讓 rsync 也同步將不存在於來源端的檔案刪除的話,可以加上 --delete 參數,如果沒有來源檔案只有新增、沒有減少的話,它就跟一般的複製動作相同:
rsync -avh --delete myfolder/ backup/
sending incremental file list
./
data1.txt
data2.txt
data3.txt
data4.txt
sent 432 bytes received 95 bytes 1.05K bytes/sec
total size is 116 speedup is 0.22
這時候若我們將來源檔案的 data1.txt 與 data2.txt 刪除,並且增加 data5.txt,在執行一次 rsync:
rsync -avh --delete myfolder/ backup/
sending incremental file list
deleting data2.txt
deleting data1.txt
./
data5.txt
sent 190 bytes received 64 bytes 508.00 bytes/sec
total size is 87 speedup is 0.34
這時候 rsync 就會同步將備份端的 data1.txt 與 data2.txt 刪除,並且同時新增 data5.txt。
如果這裡我們沒有加上 --delete 參數的話,rsync 就只會新增 data5.txt,不會刪除任何檔案。
若要讓 rsync 在備份檔案時,排除所有 *.txt 的文字檔檔案,可以使用 --exclude 參數:
rsync -avh --exclude '*.txt' myfolder/ backup/
sending incremental file list
./
chinese.py
find_edimax.c
src/
src/pack.c
sent 3.91K bytes received 88 bytes 7.99K bytes/sec
total size is 3.59K speedup is 0.90
我們可以使用多個 --exclude 來排除多種檔案,例如:
rsync -avh --exclude '*.txt' --exclude '*.py' myfolder/ backup/
取得前十名 access 最多的 IP 位址
cat access_log|awk '{print $1}'|sort|uniq -c|sort -nr|head -10
增量備份
使用--backup選項在同步備份過程中可以讓目的端的檔案,被覆蓋或刪除之前先進行更名的動作以保留下來。
rsync -a --delete --backup a/ b/
取得前十名 access 最多的網頁
cat access_log|awk '{print $11}'|sort|uniq -c|sort -nr|head -10
取得前十名下載流量最大的 zip 檔案
cat access.log |awk '($7~/\.zip/){print $10 " " $1 " " $4 " " $7}'|sort -nr|head -10
取得前十名 Loading 最大的頁面 (大於60秒的 php 頁面)
cat access_log |awk '($NF > 60 && $7~/\.php/){print $7}'|sort -n|uniq -c|sort -nr|head -10
取得前十名 User access 最久的頁面
cat access_log |awk '($7~/\.php/){print $NF " " $1 " " $4 " " $7}'|sort -nr|head -10
取得 access log 平均流量 (GB)
cat access_log |awk '{sum+=$10} END {print sum/1024/1024/1024}'
取得所有 404 Link
awk '($9 ~/404/)' access_log | awk '{print $9,$7}' | sort
取得所有 access code 的 stats 數量
cat access_log | awk -F' ' '$9 == "400" || $9 == "404" || $9 == "408" || $9 == "499" || $9 == "500" || $9 =="502" || $9 =="504" {print $9}' | sort | uniq -c | more
1 、對訪問ip進行排序:
cat /var/log/apache/access.log |awk '{print $1}'|sort |uniq -c|sort -n
2、查看訪問某一頁面的使用者:
de>grep "GET /url/to/some/file" /var/log/apache/access.log |awk '{print $1}' |sort |uniq -c |sort -nde>
搜尋重複檔案
這邊介紹兩種可以搜尋重複檔案的方法。第一個方法透過下列指令來檢查檔案的 MD5 切細值 (hash):
$ find . -type f -exec md5sum '{}' ';' | sort | uniq --all-repeated=separate -w 24
上述指令將會計算每個檔案的 MD5 切細值並藉此加以排序,分開顯示在不同的行上,並比對每個切細值的前 24 位數。
第二種方法則是檢查檔案大小:
$ find . -type f -printf "%p - %s\n" | sort -nr -k3 | uniq -D -f1
兩種方法各有優缺,比對 MD5 切細值會得到非常精準的結果,可是比對檔案大小則速度比較快。
Add default gw and route
=============================================
先用 route -n 確認一下有沒有一條10.0.0.0的路由
如果沒有 先加一條10.0的路由 不然網路會斷
route add -net 10.0.0.0 netmask 255.0.0.0 gw 172.19.1.252
加完之後再route -n 確認一下
有路由之後 先移除舊的gateway
route del default
移除完route -n 看一下應該沒有default gw了
route add default gw 172.19.1.254
再route -n 看一下 default gw如果是172.19.1.254 應該就ok了
Delete Files Older Than x Days on Linux
find /path/to/files* -mtime +5 -exec rm {} \;
https://blog.gtwang.org/linux/unix-linux-find-command-examples/
列出唯讀的檔案:
find . -perm /u=r
列出可執行的檔案:
find / -perm /a=x
sed -e 's|/usr/loca/|/opt/|g' httpd.conf (預設呈現)
sed -i 's|/usr/local/|/opt/|g' httpd.conf (-i 直接改)
sed 's/\/opt\/indexCopy\//\/opt\/shm\//g' dagama.AreaAc.xml
sed -e 's/^/#/g' filename (在句首加上#)
sed -e 's/#//g' filename (在句首去掉#)
sed -i 's/\/opt\/indexCopy\//\/opt\/shm\//g' dagama.AreaAc.xml
如果要看某一段區間的ip存取次數,可用
sed -n '/2017:17:30/,/2017:18:30/p' /log/io_access.log | awk '{print $1}' | sort -n | uniq -c | sort -n
把中間的時間區間和log path換掉,就可以了
nfsstat 指令常用參數
> nfsstat --help (http://notes.yuting.cc/home/nfsperformance)
Usage: nfsstat [OPTION]...
-m 顯示mount的NFS目錄及mount參數
-c 顯示NFS 用戶端的使用狀態
-s 顯示NFS 伺服器的使用狀態
-v 顯示目前設備的所有NFS相關狀態訊息
--help 使用說明
同時複製多個重複檔案
for i in {1..2};do cp -R abc_$i abc_$i.bak;done
loop目錄名稱執行指令
for i in *; do 7za a -tzip $i.zip $i/*.*; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment