1. 程式人生 > >find命令的用法

find命令的用法

轉自:https://www.oschina.net/translate/15-practical-linux-find-command-examples
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 查詢系統中最後n24小時訪問的檔案
-cmin n 查詢系統中最後N分鐘被改變檔案狀態的檔案
-ctime n 查詢系統中最後n
24小時被改變檔案狀態的檔案
-mmin n 查詢系統中最後N分鐘被改變檔案資料的檔案
-mtime n 查詢系統中最後n*24小時被改變檔案資料的檔案

find命令的16種用法:

  1. 用檔名查詢檔案

這是find命令的一個基本用法。下面的例子展示了用MyCProgram.c作為查詢名在當前目錄及其子目錄中查詢檔案的方法。

find -name “MyCProgram.c”

./backup/MyCProgram.c
./MyCProgram.c

2.用檔名查詢檔案,忽略大小寫

這是find命令的一個基本用法。下面的例子展示了用MyCProgram.c作為查詢名在當前目錄及其子目錄中查詢檔案的方法,忽略了大小寫。

find -iname “MyCProgram.c”

./mycprogram.c
./backup/mycprogram.c
./backup/MyCProgram.c
./MyCProgram.c

  1. 使用mindepth和maxdepth限定搜尋指定目錄的深度

在root目錄及其子目錄下查詢passwd檔案。

find / -name passwd

./usr/share/doc/nss_ldap-253/pam.d/passwd
./usr/bin/passwd
./etc/pam.d/passwd
./etc/passwd

在root目錄及其1層深的子目錄中查詢passwd. (例如root — level 1, and one sub-directory — level 2)

find -maxdepth 2 -name passwd

./etc/passwd

在root目錄下及其最大兩層深度的子目錄中查詢passwd檔案. (例如 root — level 1, and two sub-directories — level 2 and 3 )

find / -maxdepth 3 -name passwd

./usr/bin/passwd
./etc/pam.d/passwd
./etc/passwd

在第二層子目錄和第四層子目錄之間查詢passwd檔案。

find -mindepth 3 -maxdepth 5 -name passwd

./usr/bin/passwd
./etc/pam.d/passwd

青崖白鹿
青崖白鹿
翻譯於 2013/09/26 13:59
4. 在find命令查詢到的檔案上執行命令

下面的例子展示了find命令來計算所有不區分大小寫的檔名為“MyCProgram.c”的檔案的MD5驗證和。{}將會被當前檔名取代。

find -iname “MyCProgram.c” -exec md5sum {} ;
d41d8cd98f00b204e9800998ecf8427e ./mycprogram.c
d41d8cd98f00b204e9800998ecf8427e ./backup/mycprogram.c
d41d8cd98f00b204e9800998ecf8427e ./backup/MyCProgram.c
d41d8cd98f00b204e9800998ecf8427e ./MyCProgram.c

  1. 相反匹配

顯示所有的名字不是MyCProgram.c的檔案或者目錄。由於maxdepth是1,所以只會顯示當前目錄下的檔案和目錄。

find -maxdepth 1 -not -iname “MyCProgram.c”
.
./MybashProgram.sh
./create_sample_files.sh
./backup
./Program.c

青崖白鹿
青崖白鹿
翻譯於 2013/09/26 14:07
6. 使用inode編號查詢檔案

任何一個檔案都有一個獨一無二的inode編號,藉此我們可以區分檔案。建立兩個名字相似的檔案,例如一個有空格結尾,一個沒有。

touch “test-file-name”

touch "test-file-name "

[Note: There is a space at the end]

ls -1 test*

test-file-name
test-file-name

從ls的輸出不能區分哪個檔案有空格結尾。使用選項-i,可以看到檔案的inode編號,藉此可以區分這兩個檔案。

ls -i1 test*
16187429 test-file-name
16187430 test-file-name

你可以如下面所示在find命令中指定inode編號。在此,find命令用inode編號重新命名了一個檔案。

find -inum 16187430 -exec mv {} new-test-file-name ;

ls -i1 test

16187430 new-test-file-name
16187429 test-file-name

你可以在你想對那些像上面一樣的糟糕命名的檔案做某些操作時使用這一技術。例如,名為file?.txt的檔名字中有一個特殊字元。若你想執行“rm file?.txt”,下面所示的所有三個檔案都會被刪除。所以,採用下面的步驟來刪除"file?.txt"檔案。

ls
file1.txt file2.txt file?.txt

找到每一個檔案的inode編號。

ls -i1
804178 file1.txt
804179 file2.txt
804180 file?.txt

如下所示: 使用inode編號來刪除那些具有特殊符號的檔名。

find -inum 804180 -exec rm {} ;

ls

file1.txt file2.txt
[Note: The file with name “file?.txt” is now removed]

青崖白鹿
青崖白鹿
翻譯於 2013/09/26 14:24
7. 根據檔案許可權查詢檔案

下面的操作時合理的:

找到具有指定許可權的檔案
忽略其他許可權位,檢查是否和指定許可權匹配
根據給定的八進位制/符號表達的許可權搜尋

此例中,假設目錄包含以下檔案。注意這些檔案的許可權不同。

ls -l
total 0
-rwxrwxrwx 1 root root 0 2009-02-19 20:31 all_for_all
-rw-r–r-- 1 root root 0 2009-02-19 20:30 everybody_read
---------- 1 root root 0 2009-02-19 20:31 no_for_all
-rw------- 1 root root 0 2009-02-19 20:29 ordinary_file
-rw-r----- 1 root root 0 2009-02-19 20:27 others_can_also_read
----r----- 1 root root 0 2009-02-19 20:27 others_can_only_read

找到具有組讀許可權的檔案。使用下面的命令來找到當前目錄下對同組使用者具有讀許可權的檔案,忽略該檔案的其他許可權。

find . -perm -g=r -type f -exec ls -l {} ;
-rw-r–r-- 1 root root 0 2009-02-19 20:30 ./everybody_read
-rwxrwxrwx 1 root root 0 2009-02-19 20:31 ./all_for_all
----r----- 1 root root 0 2009-02-19 20:27 ./others_can_only_read
-rw-r----- 1 root root 0 2009-02-19 20:27 ./others_can_also_read

找到對組使用者具有隻讀許可權的檔案。

find . -perm g=r -type f -exec ls -l {} ;
----r----- 1 root root 0 2009-02-19 20:27 ./others_can_only_read

找到對組使用者具有隻讀許可權的檔案(使用八進位制許可權形式)。

find . -perm 040 -type f -exec ls -l {} ;
----r----- 1 root root 0 2009-02-19 20:27 ./others_can_only_read

青崖白鹿
青崖白鹿
翻譯於 2013/09/26 14:34
8. 找到home目錄及子目錄下所有的空檔案(0位元組檔案)

下面命令的輸出檔案絕大多數都是鎖定檔案盒其他程式建立的place hoders

find ~ -empty

只列出你home目錄裡的空檔案。

find . -maxdepth 1 -empty

只列出當年目錄下的非隱藏空檔案。

find . -maxdepth 1 -empty -not -name “.*”

  1. 查詢5個最大的檔案

下面的命令列出當前目錄及子目錄下的5個最大的檔案。這會需要一點時間,取決於命令需要處理的檔案數量。

find . -type f -exec ls -s {} ; | sort -n -r | head -5

  1. 查詢5個最小的檔案

方法同查詢5個最大的檔案類似,區別只是sort的順序是降序。

find . -type f -exec ls -s {} ; | sort -n | head -5

上面的命令中,很可能你看到的只是空檔案(0位元組檔案)。如此,你可以使用下面的命令列出最小的檔案,而不是0位元組檔案。

find . -not -empty -type f -exec ls -s {} ; | sort -n | head -5

青崖白鹿
青崖白鹿
翻譯於 2013/09/26 14:45
11. 使用-type查詢指定檔案型別的檔案

只查詢socket檔案

find . -type s

查詢所有的目錄

find . -type d

查詢所有的一般檔案

find . -type f

查詢所有的隱藏檔案

find . -type f -name “.*”

查詢所有的隱藏目錄

find -type d -name “.*”

  1. 通過和其他檔案比較修改時間查詢檔案

顯示在指定檔案之後做出修改的檔案。下面的find命令將顯示所有的在ordinary_file之後建立修改的檔案。

ls -lrt
total 0
-rw-r----- 1 root root 0 2009-02-19 20:27 others_can_also_read
----r----- 1 root root 0 2009-02-19 20:27 others_can_only_read
-rw------- 1 root root 0 2009-02-19 20:29 ordinary_file
-rw-r–r-- 1 root root 0 2009-02-19 20:30 everybody_read
-rwxrwxrwx 1 root root 0 2009-02-19 20:31 all_for_all
---------- 1 root root 0 2009-02-19 20:31 no_for_all

find -newer ordinary_file

.
./everybody_read
./all_for_all
./no_for_all

  1. 通過檔案大小查詢檔案

使用-size選項可以通過檔案大小查詢檔案。

查詢比指定檔案大的檔案

find ~ -size +100M

查詢比指定檔案小的檔案

find ~ -size -100M

查詢符合給定大小的檔案

find ~ -size 100M

注意: – 指比給定尺寸小,+ 指比給定尺寸大。沒有符號代表和給定尺寸完全一樣大。
14. 給常用find操作取別名

若你發現有些東西很有用,你可以給他取別名。並且在任何你希望的地方執行。

常用的刪除a.out檔案。

alias rmao=“find . -iname a.out -exec rm {} ;”

rmao

刪除c程式產生的core檔案。

alias rmc=“find . -iname core -exec rm {} ;”

rmc

青崖白鹿
青崖白鹿
翻譯於 2013/09/26 14:59
15. 用find命令刪除大型打包檔案

下面的命令刪除大於100M的*.zip檔案。

find / -type f -name *.zip -size +100M -exec rm -i {} ;"

用別名rm100m刪除所有大雨100M的*.tar檔案。使用同樣的思想可以建立rm1g,rm2g,rm5g的一類別名來刪除所有大於1G,2G,5G的檔案。

alias rm100m=“find / -type f -name *.tar -size +100M -exec rm -i {} ;”

alias rm1g=“find / -type f -name *.tar -size +1G -exec rm -i {} ;”

alias rm2g=“find / -type f -name *.tar -size +2G -exec rm -i {} ;”

alias rm5g=“find / -type f -name *.tar -size +5G -exec rm -i {} ;”

rm100m

rm1g

rm2g

rm5g

16、手動刪除過期的php session(做成crontab即可實現定時刪除):
find /tmp -cmin +24 -type f -name “sess_" -exec rm -f {} ;
或者
find /tmp -cmin +24 -type f -name "sess_
” | xargs rm
找出24分鐘前修改過的檔案並刪除.

比如,在當前目錄中搜索內容包含的字尾為log大小小於1M的30分鐘內修改過的檔案並按"檔名+關鍵詞所在行"顯示出來.
find . -type f -name “*.log” -size -1024k -cmin -30 | xargs grep “keyword”
這個查詢涉及到的搜尋條件包括搜尋路徑/檔名/型別/大小/修改時間/包含內容.