bash程式設計之 find檔案查詢工具

2021-08-28 02:00:55 字數 2155 閱讀 2360

find 檔案查詢工具,通過遍歷指定路徑下的檔案系統完成查詢,速度慢,但執行精確查詢;

語法:find [options] [查詢路徑] [查詢條件] [處理動作]

查詢路徑:預設為當前目錄

查詢條件:默示查詢所有檔案

處理動作:顯示至螢幕

1 查詢條件:

1.1 根據檔案名字查詢:

-name 「檔名稱」: 支援使用globbing元字元 *, ?, , [^]

-iname "檔名稱":查詢時忽略檔名稱字元大小寫

-regex pattern: 查詢時以pattern匹配整個檔案路徑字串,而不僅僅包含檔案本身

1.2. 根據屬主、屬組來查詢:

-user username: 查詢屬主為指定使用者的檔案; 

-group groupname: 查詢屬組為指定組的檔案;

-uid userid

-gid groupid

-nouser:查詢沒有屬主的檔案

-nogroup:查詢沒有屬組的檔案

1.3. 組合條件:條件之間要有邏輯關係

與: -a, 同時滿足

或:-o, 滿足乙個條件

非: -not, !, 表示不符合條件

例:-not a -a -not b = -not \( a -o b \)

-not a -o -not b = -not \( a -a b \)

1.4. 根據檔案型別查詢:

-type type

f: 普通檔案, -type f

d: 目錄檔案

l: 符號鏈結檔案

s: 套接字檔案

b: 塊裝置檔案

c: 字元裝置檔案

p:命令管道檔案

1.5 根據檔案大小來查詢:

-size [+|-]#unit

unit(常用單位):k, m, g

#unit: #-1 < x <= #

-#unit: x <= #-1

+#unit:x > #

例如:find / -size 10m   顯示9m-10m之間大小的檔案

find / -size -10m  顯示小於等於9m大小的檔案

find / -size +10m  顯示大於10m大小的檔案

1.6 根據時間戳查詢:

以「天」為單位:

-atime [+|-]#   即access time, 檔案被讀取或者執行的時間

-atime #   #天到#+1天之間訪問的檔案 

-atime -#  #天內訪問的檔案

-atime +#  #天之前訪問的檔案

例項:-atime 0   0天到1天之間訪問的檔案,即24小時內訪問的檔案 

-atime 3   3天到4天之間訪問的檔案,

-atime -3  3天內訪問的檔案

-atime +3  3天之前訪問的檔案

-mtime [+|-]#   即modify time,指檔案內容被修改的時間。

-ctime [+|-]#   即change time,檔案狀態改變時間,如通過chmod修改檔案屬性

以「分鐘」為單位:

-amin [+|-]#

-mmin [+|-]#

-cmin [+|-]#

使用stat可以檢視檔案的atime、ctime、mtime。

例:stat ~/.bashrc

也可以使用ls檢視檔案的atime、ctime、mtime。

ls –l ~/.bashrc指示的是檔案的modify time。

ls –lu ~/.bashrc 或者ls –l –time=atime~/.bashrc指示的是檔案的access time

ls –lc ~/.bashrc 或者ls –l –time=ctime~/.bashrc 指示的是檔案的change time

2. 對查詢到的檔案進行處理

find  命令  | xargs 命令

例:find ~/ -size +10m | xargs du -sh  列出檔案的大小

find ~/ -size +10m | xargs rm -rf  刪除找到的檔案

find ~/ -size +10m | xargs -i cp {} /tmp/ 將找到的檔案複製到/tmp/目錄下,其中-i的作用是將管道中的資料匯入{}中

bash程式設計之 函式

函式 復用 模組程式設計 語法 function f name f name 呼叫 使用函式名 函式名出現的地方,會被自動替換為函式 練習 利用函式改寫此前的服務指令碼 bin bash prog basename 0 lockfile var lock subsys prog start stop...

bash程式設計之陣列

陣列 資料結構,資料序列,儲存連續多個資料,可以使用索引獲取相關元素 宣告陣列 declare a 宣告索引陣列 declare a 宣告關聯數元素賦值 一次賦值乙個元素 root mm alias 0 read root mm echo 一次賦值多個元素 索引預設從0開始 root mm alia...

find檔案查詢

我們很多時候會忘記某個檔案在什麼位置,此時就需要進行檔案查詢。linux檔案查詢主要是使用find命令來進行查詢,find命令可以通過不同的維度來定位到某個想要查詢的檔案。find 查詢的範圍 選項 表示式 動作 使用示例如下 root oldboy find name ls 以根目錄下的所有檔案作...