作者簡介
一、前言自己平時比較喜歡記筆記,工作四五年,筆記上千篇。最近離職了,加上職場瓶頸,準備好好覆盤和整理一下相關的筆記、梳理一下知識點,可能後面有一系列的博文輸出,從基礎到進階。
二、find命令find命令是Linux系統管理員工具庫中最強大的工具之一,可以使用find命令根據許可權、型別、日期、所有權、大小等搜尋檔案和目錄,它還可以與grep或sed等其他工具結合使用。
語法:
find [options] [path...] [expression]
三、實踐1、按檔名查詢
find / -name access.log
2、透過副檔名查詢檔案 -name
find . -name “*.txt” #在當前目錄下查詢所有txt字尾檔案
3、按許可權查詢檔案 -perm
find . -perm 755 -print #查詢當前目錄下755許可權的檔案find . -perm -007 -print #查詢所有使用者都可以讀、寫、執行的檔案
4、按所有者查詢檔案 -group / -nogroup / -user / -nouser
find ~ -group lile -print #查詢~目錄下所屬者為lile的檔案find /home -group -print #查詢屬主賬戶已經被刪除的檔案,查詢在/etc/passwd裡已經沒有了的賬戶find ~ -user lile -print #查詢~目錄下所屬者為lile的檔案find /home -nouser -print #查詢屬主賬戶已經被刪除的檔案,查詢在/etc/passwd裡已經沒有了的賬戶
5、按修改日期查詢檔案 -mtime
find . -mtime -3 -print #查詢更改時間在3天之內的檔案find . -mtime -2 -print #查詢更改時間在2天之前的檔案
6、按型別查詢檔案 -type
find . -type d -print #查詢當前目錄下的所有目錄find . ! -type d -print #查詢當前目錄下除了目錄的其他所有型別檔案
7、按大小查詢檔案 -size
find . -size 100c -print 查詢當前目錄下檔案長度為100位元組的檔案find . -size +1000000c -print 查詢當前目錄下檔案大於1M位元組的檔案find . -size +10 -print 查詢當前目錄下超過10塊的檔案(1塊=512位元組)
8、排除某個目錄 -prune
find /shell -path "/shell/tt" -prune -o -print #查詢在/shell目錄下除了tt目錄的其他find /shell -path "/shell/tt" -prune -o -name "*.txt" -print #查詢在/shell目錄下除了tt目錄的txt檔案
9、目錄與檔案先後順序 -dept
find /shell -depth -print#有dept:先處理目錄下的子內容,再處理目錄本身#無dept:先處理目錄本身,然後處理目錄下的子內容
10、正則查詢
find . -name "[A-Z]*" -print #查詢當前目錄及子目錄中查詢檔名以大寫字母開頭的檔案find . |xargs grep "YZS" #查詢某個資料夾下面的哪些具體檔案包含某一個欄位find . -regextype "posix-egrep" -regex ".*\.(cc|h)" |xargs cat |grep -v ^$ |wc -l
11、查詢並對結果做相關操作 -exec / -xargs
find . -type l -exec ls -l {} \; #找到為檔案型別為軟連線的檔案find . -type f -print | xargs file #檔案分類find / -name "core*" -print | xargs echo "">/tmp/core.log #找到記憶體資訊轉儲檔案coredump,然後儲存到/tmp/core.log下find . -name "*.txt" -print0 | xargs -0 rm -rf #找到後刪除,慎用find / -path '/etc/ssl/certs' -prune -o -name *.pem | xargs -i cp {} ./pem #找到後並複製cat file.txt | xargs #將多行轉換成單行cat file.txt | xargs -n 3 #指定每行的引數數量 每次執行需要x個引數echo "splitXsplitXsplitXsplit" |xargs -d X #用自己指定的分隔符進行分割echo "splitXsplitXsplitXsplit" |xargs -d X -n 2 #用自己指定的分隔符進行分割,並且指定每行輸出的數量cat args |xargs -I {} bash cecho.sh p {} 1 #從cat裡讀取資料,每讀到一個就替換一次
12、匹配多個檔案
find . \( -name "*.txt" -o -name "*.pdf" \)
13、find排除某個目錄
find / -path '/etc/ssl/certs' -prune -o -name *.pem #find 查詢路徑 -path '排除目錄路徑' -prune -o ....
四、find時間點問題
find與時間有關的選項有-atime(訪問時間)、-ctime(建立時間)、-mtime(屬性修改時間),引數為後面跟的時間n;
find . -atime n 這裡的n表示n天之前的“一天之內”被訪問過的檔案
find . -atime +n 列出在n天之前(不包含n天本身)被訪問過的檔案
find . -atime -n 列出在n天之內(包含n天本身)被訪問過的檔案
例項:
假如現在的時間點為20171209的15:00整,那麼下面幾個查詢表示的具體時間範圍
1:建立檔案
touch -a -d "2021-01-01 15:00" a.txttouch -a -d "2021-01-02 15:00" b.txttouch -a -d "2021-01-03 15:00" c.txttouch -a -d "2021-01-04 15:00" d.txttouch -a -d "2021-01-05 15:00" e.txttouch -a -d "2021-01-06 15:00" f.txttouch -a -d "2021-01-07 15:00" g.txttouch -a -d "2021-01-08 15:00" h.txt
2:以下三條命令得到的結果分別為:
find . -atime 2 找出兩天之前的一天內被訪問檔案(距離現在的 -72小時 ~~ -48小時 之間)
find . -atime -2 找出距離此時兩天之內的被訪問的檔案(距離現在的前48小時之內)
find . -atime +2 找出兩天之前,不包括兩天之前的一天(也就是不包括-2和2的)之前的被訪問的檔案(離此時72小時之前)
3、圖形分析