linux命令之find命令簡單概述

2022-10-06 20:06:13 字數 4416 閱讀 9314

linux下find命令在目錄結構中搜尋檔案,並執行指定的操作。linux下find命令提供了相當多的查詢條件,功能很強大。由於find具有強大的功能,所以它的選項也很多,其中大部分選項程式設計客棧都值得我們花時間來了解一下。即使系統中含有網路檔案系統( nfs),find命令在該檔案系統中同樣有效,只你具有相應的許可權。 在執行乙個非常消耗資源的find命令時,很多人都傾向於把它放在後台執行,因為遍歷乙個大的檔案系統可能會花費很長的時間(這裡是指30g位元組以上的檔案系統)。

1.命令格式:

find pathname -options [-print -exec -ok ...]

2.命令功能:

用於在檔案樹種查詢檔案,並作出相應的處理

3.命令引數:

pathname: find命令所查詢的目錄路徑。例如用.來表示當前目錄,用/來表示系統根目錄。

-print: find命令將匹配的檔案輸出到標準輸出。

-exec: find命令對匹配的檔案執行該引數所給出的shell命令。相應命令的形式為'command' \;,注意和\;之間的空格。

-ok: 和-exec的作用相同,只不過以一種更為安全的模式來執行該引數所給出的shell命令,在執行每乙個命令之前,都會給出提示,讓使用者來確定是否執行。

4.命令選項:

-name 按照檔名查詢檔案。

-perm 按照檔案許可權來查詢檔案。

-prune 使用這一選項可以使find命令不在當前指定的目錄中查詢,如果同時使用-depth選項,那麼-prune將被find命令忽略。

-user 按照檔案屬主來查詢檔案。

-group 按照檔案所屬的組來查詢檔案。

-mtime -n +n 按照檔案的更改時間來查詢檔案, - n表示檔案更改時間距現在n天以內,+ n表示檔案更改時間距現在n天以前。find命令還有-atime和-ctime 選項,但它們都和-m time選項。

-nogroup 查詢無有效所屬組的檔案,即該檔案所屬的組在/etc/groups中不存在。

-nouser 查詢無有效屬主的檔案,即該檔案的屬主在/etc/passwd中不存在。

-newer file1 ! file2 查詢更改時間比檔案file1新但比檔案file2舊的檔案。

-type 查詢某一型別的檔案,諸如:

b - 塊裝置檔案。

d - 目錄。

c - 字元裝置檔案。

p - 管道檔案。

l - 符號鏈結檔案。

f - 普通檔案。

-size n:[c] 查詢檔案長度為n塊的檔案,帶有c時表示檔案長度以位元組計。-depth:在查詢檔案時,首先查詢當前目錄中的檔案,然後再在其子目錄中查詢。

-fstype:查詢位於某一型別檔案系統中的檔案,這些檔案系統型別通常可以在配置檔案/etc/fstab中找到,該配置檔案中包含了本系統中有關檔案系統的資訊。

-mount:在查詢檔案時不跨越檔案系統mount點。

-follow:如果find命令遇到符號鏈結檔案,就跟蹤至鏈結所指向的檔案。

-cpio:對匹配的檔案使用cpio命令,將這些檔案備份到磁帶裝置中。

另外,下面三個的區別:

-amin n 查詢系統中最後n分鐘訪問的檔案

-atime n 查詢系統中最後n*24小時訪問的檔案

-cmin n 查詢系統中最後n分鐘被改變檔案狀態的檔案

-ctime n 查詢系統中最後n*24小時被改變檔案狀態的檔案

-mmin n 查詢系統中最後n分鐘被改變檔案資料的檔案

-mtime n 查詢系統中最後n*24小時被改變檔案資料的檔案

5.使用例項:

例項1:查詢指定時間內修改過的檔案

命令:find -atime -2

輸出:[root@peidachang ~]# find -atime -2

./logs/monitor

./.bashrc

./.bash_profile

./.bash_history

說明:超找48小時內修改過的檔案

例項2:根據關鍵字查詢

命令:find . -name "*.log"

輸出:[root@localhost test]# find . -name "*.log"

./log_link.log

./log2014.log

./test4/log3-2.log

./test4/log3-3.log

./test4/log3-1.log

./log2013.log

./log2012.log

./log.log

./test5/log5-2.log

./test5/log5-3.log

./test5/log.log

./test5/log5-1.log

./test5/test3/log3-2.log

./test5/test3/log3-3.log

./test5/test3/log3-1.log

./test3/log3-2.log

./test3/log3-3.log

./test3/log3-1.log

說明:在當前目錄查詢 以.log結尾的檔案。 「. 「代表當前目錄

例項3:按照目錄或檔案的許可權來查詢檔案

命令:find /opt/soft/test/ -perm 777

輸出:[root@localhost test]# find /opt/soft/test/ -perm 777

/opt/soft/test/log_link.log

/opt/soft/test/test4

/opt/soft/test/test5/test3

/opt/soft/test/test3

說明:查詢/opt/soft/test/目錄下 許可權為 777的檔案

例項4:按型別查詢

命令:find . -type f -name "*.log"

輸出:[root@localhost test]# find . -type f -name "*.log"

./log2014.log

./test4/log3-2.log

./test4/log3-3.log

./test4/log3-1.log

./log2013.log

./log2012.log

./log.log

./test5/log5-2.log

./test5/log5-3.log

./test5/log.log

./test5/log5-1.log

./test5/test3/log3-2.log

./test5/test3/log3-3.log

./test5/test3/log3-1.log

./test3/log3-2.log

./test3/log3-3.log

./test3/log3-1.log

[root@localhost test]#

說明:查詢當目錄,以.log結尾的普通檔案

例項5:查詢當前所有目錄並排序

命令:find . -type d | sort

輸出:[root@localhost test]# find . -type d | sort

../scf

./scf/bin

./scf/doc

./scf/lib

./scf/service

./scf/service/deploy

./scf/service/deploy/info

./scf/service/deploy/product

./test3

./test4

./test5

./test5/test3

[root@localhost test]#www.cppcns.com

例項6:按大小查詢檔案

命令:find . -size +1000c -print

輸出:[root@localhost test]# find . -size +1000c -print

../test4

./scf

./scf/lib

./scf/service

./scf/service/deploy

./scf/service/deploy/product

./scf/service/deploy/info

./scf/doc

./scf/bin

./log2012.log

./test5

./test5/test3

程式設計客棧./test3

[root@localhost test]#

說明:查詢當前目錄大於1k的檔案

本文標題: linux命令之find命令簡單概述

本文位址: /os/linux/178998.html

Linux命令之find命令

在檔案系統上查詢符合條件的檔案 檔案查詢 locate,find 非實時查詢 資料庫查詢 locate 實時查詢 find locate 依賴於事先構建的索引,索引的構建是在系統較為空閒時自動進行 週期性任務 手動更新資料庫 updatedb 索引構建過程需要遍歷整個根檔案系統,極消耗資源 工作特點...

Linux命令之find命令詳解

linux命令之find命令詳解 find命令是乙個在unix檔案系統中查詢檔案的常用命令,可以進行很多條件查詢。讓我們來看一些find命令的例項。語法 find 路徑 約束條件 如何查詢在檔名中含有指定關鍵字的檔案?下面這條命令查詢在 etc 目錄下所有檔名中含有 mail 的檔案。find et...

find 命令 Linux命令篇 find命令

find name txt o name pdf print演示結果 find regex txt pdf iregex 忽略大小寫的正則 查詢所有非txt文字 find name txt print演示結果 列印出當前目錄的檔案 深度為1 find maxdepth 1 type f演示結果 5....