除了在一個目錄結構下查找文件這種基本的操作,你還可以用find命令實現一些實用的操作,使你的命令行之旅更加簡易。
本文將介紹15種無論是于新手還是老鳥都非常有用的Linux find命令。
首先,在你的home目錄下面創建下面的空文件,來測試下面的find命令示例。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | # vim create_sample_files.sh touch MybashProgram.sh touch mycprogram.c touch MyCProgram.c touch Program.c mkdir backup cd backup touch MybashProgram.sh touch mycprogram.c touch MyCProgram.c touch Program.c # chmod +x create_sample_files.sh # ./create_sample_files.sh # ls -R .: backup MybashProgram.sh MyCProgram.c create_sample_files.sh mycprogram.c Program.c . /backup : MybashProgram.sh mycprogram.c MyCProgram.c Program.c |
1. 用文件名查找文件
這是find命令的一個基本用法。下面的例子展示了用MyCProgram.c作為查找名在當前目錄及其子目錄中查找文件的方法。
1 2 3 | # find -name "MyCProgram.c" . /backup/MyCProgram .c . /MyCProgram .c |
2.用文件名查找文件,忽略大小寫
這是find命令的一個基本用法。下面的例子展示了用MyCProgram.c作為查找名在當前目錄及其子目錄中查找文件的方法,忽略了大小寫。
1 2 3 4 5 | # find -iname "MyCProgram.c" . /mycprogram .c . /backup/mycprogram .c . /backup/MyCProgram .c . /MyCProgram .c |
3. 使用mindepth和maxdepth限定搜索指定目錄的深度
在root目錄及其子目錄下查找passwd文件。
1 2 3 4 5 | # 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)
1 2 | # find -maxdepth 2 -name passwd . /etc/passwd |
在root目錄下及其最大兩層深度的子目錄中查找passwd文件. (例如 root — level 1, and two sub-directories — level 2 and 3 )
1 2 3 4 | # find / -maxdepth 3 -name passwd . /usr/bin/passwd . /etc/pam .d /passwd . /etc/passwd |
在第二層子目錄和第四層子目錄之間查找passwd文件。
1 2 3 | # find -mindepth 3 -maxdepth 5 -name passwd . /usr/bin/passwd . /etc/pam .d /passwd |
4. 在find命令查找到的文件上執行命令
下面的例子展示了find命令來計算所有不區分大小寫的文件名為“MyCProgram.c”的文件的MD5驗證和。{}將會被當前文件名取代。
1 2 3 4 5 | find -iname "MyCProgram.c" - exec md5sum {} \; d41d8cd98f00b204e9800998ecf8427e . /mycprogram .c d41d8cd98f00b204e9800998ecf8427e . /backup/mycprogram .c d41d8cd98f00b204e9800998ecf8427e . /backup/MyCProgram .c d41d8cd98f00b204e9800998ecf8427e . /MyCProgram .c |
5. 相反匹配
顯示所有的名字不是MyCProgram.c的文件或者目錄。由于maxdepth是1,所以只會顯示當前目錄下的文件和目錄。
1 2 3 4 5 6 | find -maxdepth 1 -not -iname "MyCProgram.c" . . /MybashProgram .sh . /create_sample_files .sh . /backup . /Program .c |
6. 使用inode編號查找文件
任何一個文件都有一個獨一無二的inode編號,借此我們可以區分文件。創建兩個名字相似的文件,例如一個有空格結尾,一個沒有。
1 2 3 4 5 6 | 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編號,借此可以區分這兩個文件。
1 2 3 | ls -i1 test * 16187429 test - file -name 16187430 test - file -name |
你可以如下面所示在find命令中指定inode編號。在此,find命令用inode編號重命名了一個文件。
1 2 3 4 5 | 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”文件。
1 2 | ls file1.txt file2.txt file ?.txt |
找到每一個文件的inode編號。
1 2 3 4 | ls -i1 804178 file1.txt 804179 file2.txt 804180 file ?.txt |
如下所示:?使用inode編號來刪除那些具有特殊符號的文件名。
1 2 3 4 | find -inum 804180 - exec rm {} \; # ls file1.txt file2.txt [Note: The file with name "file?.txt" is now removed] |
7. 根據文件權限查找文件
下面的操作時合理的:
- 找到具有指定權限的文件
- 忽略其他權限位,檢查是否和指定權限匹配
- 根據給定的八進制/符號表達的權限搜索
此例中,假設目錄包含以下文件。注意這些文件的權限不同。
1 2 3 4 5 6 7 8 | 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 |
找到具有組讀權限的文件。使用下面的命令來找到當前目錄下對同組用戶具有讀權限的文件,忽略該文件的其他權限。
1 2 3 4 5 | 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 |
找到對組用戶具有只讀權限的文件。
1 2 | find . -perm g=r - type f - exec ls -l {} \; ----r----- 1 root root 0 2009-02-19 20:27 . /others_can_only_read |
找到對組用戶具有只讀權限的文件(使用八進制權限形式)。
1 2 | find . -perm 040 - type f - exec ls -l {} \; ----r----- 1 root root 0 2009-02-19 20:27 . /others_can_only_read |
8. 找到home目錄及子目錄下所有的空文件(0字節文件)
下面命令的輸出文件絕大多數都是鎖定文件盒其他程序創建的place hoders
1 | find ~ -empty |
只列出你home目錄里的空文件。
1 | find . -maxdepth 1 -empty |
只列出當年目錄下的非隱藏空文件。
1 | find . -maxdepth 1 -empty -not -name ".*" |
9. 查找5個最大的文件
下面的命令列出當前目錄及子目錄下的5個最大的文件。這會需要一點時間,取決于命令需要處理的文件數量。
1 | find . - type f - exec ls -s {} \; | sort -n -r | head -5 |
10. 查找5個最小的文件
方法同查找5個最大的文件類似,區別只是sort的順序是降序。
1 | find . - type f - exec ls -s {} \; | sort -n | head -5 |
上面的命令中,很可能你看到的只是空文件(0字節文件)。如此,你可以使用下面的命令列出最小的文件,而不是0字節文件。
1 | find . -not -empty - type f - exec ls -s {} \; | sort -n | head -5 |
11. 使用-type查找指定文件類型的文件
只查找socket文件
1 | find . - type s |
查找所有的目錄
1 | find . - type d |
查找所有的一般文件
1 | find . - type f |
查找所有的隱藏文件
1 | find . - type f -name ".*" |
查找所有的隱藏目錄
1 | find - type d -name ".*" |
12. 通過和其他文件比較修改時間查找文件
顯示在指定文件之后做出修改的文件。下面的find命令將顯示所有的在ordinary_file之后創建修改的文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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 |
13. 通過文件大小查找文件
使用-size選項可以通過文件大小查找文件。
查找比指定文件大的文件
1 | find ~ -size +100M |
查找比指定文件小的文件
1 | find ~ -size -100M |
查找符合給定大小的文件
1 | find ~ -size 100M |
注意: – 指比給定尺寸小,+ 指比給定尺寸大。沒有符號代表和給定尺寸完全一樣大。
14. 給常用find操作取別名
若你發現有些東西很有用,你可以給他取別名。并且在任何你希望的地方執行。
常用的刪除a.out文件。
1 2 | alias rmao= "find . -iname a.out -exec rm {} \;" # rmao |
刪除c程序產生的core文件。
1 2 | alias rmc= "find . -iname core -exec rm {} \;" # rmc |
15. 用find命令刪除大型打包文件
下面的命令刪除大于100M的*.zip文件。
1 | find / - type f -name *.zip -size +100M - exec rm -i {} \;" |
用別名rm100m刪除所有大雨100M的*.tar文件。使用同樣的思想可以創建rm1g,rm2g,rm5g的一類別名來刪除所有大于1G,2G,5G的文件。
1 2 3 4 5 6 7 8 9 | 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 |
Find命令示例(第二部分)
若你喜歡這篇關于find命令的Mommy文章,別忘了看看第二部分的關于find命令的Daddy文章。《爹地,我找到了!15個極好的Linux find命令示例》