find命令用法舉例1

2021-08-21 17:50:00 字數 2870 閱讀 4674

linux下find命令在目錄結構中搜尋檔案,並執行指定的操作。

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選項。

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

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

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

-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.用法舉例:

5.1查詢2*24小時內修改該的檔案 

[hduser0401@dev-l002782 ~]$ find -atime -2

../test0043

./.vim

./20160926

。。。。

5.2在當前目錄查詢以.txt結尾的檔案

[hduser0401@dev-l002782 ~]$ find -name "*txt"

./test0829.txt

./test0825.txt

./test_guo.txt

./class_test.txt

5.3在當前目錄查詢以test0開頭, .txt結尾的檔案

[hduser0401@dev-l002782 ~]$ find -name "test0*txt"

./test0829.txt

./test0825.txt

5.4按照目錄或檔案的許可權來查詢檔案

[hduser0401@dev-l002782 ~]$ find /usr/bin -perm 777

/usr/bin/unxz

/usr/bin/hwloc-info

/usr/bin/servertool

/usr/bin/mdel

說明:查詢/usr/bin目錄下許可權為777的檔案

5.5按型別查詢 

[hduser0401@dev-l002782 ~]$ find -name "test09*txt"

./test0908.txt

./test0929.txt

[hduser0401@dev-l002782 ~]$ find . -type f -name "test09*txt"

./test0908.txt

./test0929.txt

說明:查詢當目錄,(以test0開頭)以.txt結尾的普通檔案

5.6查詢當前所有目錄並排序

[hduser0401@dev-l002782 ~]$ find . -type d | sort

../20160926

./20160926/aaa

./20160926/ccc

5.7按大小查詢檔案

[hduser0401@dev-l002782 ~]$ find . -size +1000c > test1080.txt

[hduser0401@dev-l002782 ~]$ find . -size +1000c -print > test1081.txt

[hduser0401@dev-l002782 ~]$ diff test1080.txt test1081.txt

26a27 > ./test1080.txt

181a183 > ./test1081.txt

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

find命令用法

關於查詢 檔案查詢 locate非實時查詢 根據索引查詢 find實時查詢 根據檔案的各種屬性去找到相對應檔案 根據檔案的各種屬性去找到相對應檔案 文字搜尋 grep,egrep,fgrep find的用法 查詢條件 檔名類 使用者和組類 檔案型別 大小和時間 根據許可權查詢 組合條件查詢 處理動作...

Linux的Find命令使用舉例

在當前目錄和子目錄下查詢檔案mycprogram.c find name mycprogram.c 查詢檔案且忽略大小寫 find iname mycprogram.c 查詢不包含mycprogram.c的檔案 find maxdepth 1 not iname mycprogram.c 在 下3到...

Linux的Find命令使用舉例

linux命令之find命令使用舉例。在當前目錄和子目錄下查詢檔案mycprogram.c find name mycprogram.c 查詢檔案且忽略大小寫 find iname mycprogram.c 查詢不包含mycprogram.c的檔案 find maxdepth 1 not iname...