Linux find命令是命令行上很是有用和方便的搜索文件的命令。它可用於根據各類搜索標準查找文件,如權限,用戶全部權,修改時間/日期,大小等。在這篇文章中,咱們將學習使用find命令以及它支持的各類選項。php
大多數Linux發行版find命令默認是可用的,所以你沒必要安裝任何軟件包。在Linux上,若是你想高效的使用Linux命令行,find命令是必須學習的。linux
基本的find命令語法以下安全
$ find location comparison-criteria search-term
這條命令列出當前目錄中的全部文件以及當前目錄中的子目錄。網絡
# find . ./abc.txt ./cool.php ./subdir ./subdir/how.php
這條命令與如下命令等價ide
# find . # find . -print
如下命令將查找當前目錄中test目錄下的文件,默認狀況下將列出全部文件。學習
# find ./test ./test ./test/abc.txt ./test/cool.php ./test/subdir ./test/subdir/how.php
如下命令經過文件名搜索文件ui
# find ./test -name "abc.txt" ./test/abc.txt
咱們也可使用通配符lua
# find ./test -name "*.php" ./test/cool.php ./test/subdir/how.php
請注意,因此有的子目錄都是被遞歸搜索的。所以,這是查找給定擴展名全部文件的一種很是強大的方法。命令行
嘗試搜索根目錄"/",將會搜索整個文件系統包括掛載設備和網絡存儲設備。 因此要當心,固然大家能夠隨時經過Ctrl + c停止命令。unix
當指定目錄時(本例中爲"./test"),能夠省略尾部的斜線,然而,若是目錄是指向其餘位置的連接時,爲了使它工做正常,你必須指定尾部的斜線(find ./test/ ...)
忽略大小寫
當搜索文件名時忽略大小寫一般頗有用。爲了忽略大小寫,只需使用"iname"選項替代"name"選項。
# find ./test -iname "*.Php" ./test/cool.php ./test/subdir/how.php
將搜索項(名稱參數)添加單引號或雙引號老是更好的作法。
不這樣作看起來有時候會起做用,其餘時候又會給出奇怪的結果。
默認狀況下,find命令遞歸的遍歷整個目錄樹。很是消耗時間和資源。然而,能夠指定目錄遍歷的深度。例如,咱們不想遍歷2,3級子目錄,這可使用maxdepth選項完成。
]# find ./test -maxdepth 2 -name '*.php' ./test/cool.php ./test/subdir/how.php # find ./test -maxdepth 1 -name '*.php' ./test/cool.php
第二個示例使用maxdepth 1選項,它意味着不會搜索超過1級深度,僅在當前目錄中。
當咱們但願限制搜索僅在當前目錄或最大1級深度子目錄,這頗有用,沒必要搜索整個目錄,而這將須要大量時間 。
就像maxdepth同樣,有一個名爲mindepth的選項能夠實現如其名稱所暗示的功能,它將至少搜索n級深度。
也能夠搜索與指定名稱或模式不匹配的文件,當咱們知道要從搜索中排除哪些文件時,這頗有用。
# find ./test -not -name '*.php' ./test ./test/abc.txt ./test/subdir
因此在上面的例子中咱們發現全部沒有php擴展名的文件,都是非php文件。find命令也支持用感嘆號替代not
# find ./test ! -name '*.php' ./test ./test/abc.txt ./test/subdir
當指定名稱或反轉時可使用多個條件。例如:
# find ./test -name 'abc*' ! -name '*.php' ./test/abc.txt
上面的find命令查找文件名以abc開頭同時擴展名不是php的文件。這是一個使用find命令構建強大的搜索表達式的示例
OR操做符
當使用多個名稱條件時,find命令將用and操做符組合他們,這意味着只有當文件符合全部條件時才被匹配。然而,若是咱們須要執行一個基於or的匹配,find也有'o'開關。
# find ./test -name '*.php' -o -name '*.txt' ./test/abc.txt ./test/cool.php ./test/subdir/how.php
上面的命令搜索以php或txt爲擴展名結尾的文件
有時候,咱們但願僅查找指定名稱的文件或目錄。find可以輕鬆的實現。
# find ./test -name 'abc*' ./test/abc.txt ./test/abc Only files # find ./test -type f -name 'abc*' ./test/abc.txt Only directories # find ./test -type d -name 'abc*' ./test/abc
至關有用和方便
所以,假如你但願在2個單獨的目錄中進行搜索,一樣,命令很是簡單
# find ./test ./dir2 -type f -name 'abc*' ./test/abc.txt ./dir2/abcdefg.txt
檢查它是否列出了2個獨立目錄中的文件
Linux中隱藏文件以點號開頭(.), 所以,它很容易在名稱條件中指定和顯示全部隱藏文件。
# find ~ -type f -name '.*'
find命令可使用"perm"來查找指定權限的文件。下列的命令查找權限爲0644的文件。
# find . -type f -perm 0644 ./abc.txt ./cool.php ./subdir/how.php ./abc.php
這對查找賦予了錯誤權限的文件頗有用,這些文件可能致使安全問題。取反也可用於權限檢查。
# find . -type f ! -perm 0777 ./abc.txt ./cool.php ./subdir/how.php ./abc.php
find命令的"perm"選項接收與chmod命令同樣的字符模式。下列的命令查找全部權限爲644以及設置了sgid位的文件。
# find / -perm 2644
相似地,使用1644查找權限爲644並設置了粘滯位的文件,"perm"也支持使用替代語法而不是八進制數。
# find / -maxdepth 3 -perm /u=s 2>/dev/null /usr/bin/fusermount /usr/bin/chfn /usr/bin/chsh /usr/bin/chage /usr/bin/gpasswd /usr/bin/newgrp /usr/bin/staprun /usr/bin/mount /usr/bin/crontab /usr/bin/su /usr/bin/umount /usr/bin/pkexec /usr/bin/sudo /usr/bin/passwd /usr/sbin/pam_timestamp_check /usr/sbin/unix_chkpwd /usr/sbin/usernetctl
注意,「2>/dev/null"刪除了帶有"權限錯誤」的條目。
查找全部只讀文件
oot 66176 Aug 4 2017 /bin/ping [root@lanquark test]# find /etc -maxdepth 1 -perm /u=r /etc /etc/fstab /etc/crypttab /etc/mtab /etc/resolv.conf ...省略後續輸出
下面的命令將查找可執行文件
[root@lanquark test]# find /usr/bin -maxdepth 2 -perm /a=x /usr/bin/cp /usr/bin/lua ...省略後續輸出
在全部者爲root的/root目錄下,查找全部或單個名稱爲tecmint.txt的文件
# find . -user root . ./abc.txt ./cool.php ./subdir ./subdir/how.php ./abc ./abc.php
咱們也能夠指定文件的名稱或者任何與用戶條件相關的名稱條件。
# find . -user root -name '*.php' ./cool.php ./subdir/how.php ./abc.php
顯而易見,咱們能夠如何構建條件以縮小咱們匹配的搜索範圍。、
搜索屬於某個特定組的全部文件
# find . -group hjm ./abc.txt
你知道你可使用~符號搜索你的家目錄嗎?
# find ~ -name abc.txt /root/abc.txt /root/test/abc.txt
很簡單
搜索文件和目錄基於修改日期和時間
另外一個find命令支持的重要搜索條件是修改和訪問日期/時間。當咱們想要找出特定時間和日期範圍修改的文件時很是方便。讓咱們來看一些小例子。
# find / -atime 50
查找修改時間在50之前,100天之內的全部文件
# find / -mtime +50 -mtime -100
查找最近1小時內屬性更改的文件。
# find . -cmin -60
查找最近1小時內文件內容更改的全部文件。
# find / -mmin -60
查找最近1小時內訪問的全部文件。
# find / -amin -60
基於大小查找文件和目錄,查找甩的50M的文件,使用
# find / -size 50M
查找全部大於50M小於100M的文件
# find / -size +50M -size -100M
find命令結合ls和sort命令能夠列出最大的文件。
下述命令將顯示當前目錄和它的子目錄下最大的5個文件。這將須要一點時間,取決於命令處理的文件數量。
# find . -type f -exec ls -s {} \; | sort -n | head -5
下列命令使用find命令的"空"選項查找全部空文件。
# find /etc -type f -empty
查找全部的空目錄使用類型"d"
# find /etc -type d -empty
真的很是簡單和容易
一些高級的操做
find命令不只能夠經過一些條件查找文件,它也可使用任何linux命令對這些文件進行操做。例如,咱們想刪除一些文件。
下面是一些簡單的例子。
假設咱們用find命令找到了文件,現要想要像ls命令那樣列出它們,它很簡單。
# find . -exec ls -ld {} \; drwxr-xr-x. 4 root root 77 Nov 19 20:28 . -rw-r--r--. 1 root hjm 0 Nov 10 22:55 ./abc.txt -rw-r--r--. 1 root root 0 Nov 10 22:55 ./cool.php drwxr-xr-x. 2 root root 21 Nov 19 20:30 ./subdir -rw-r--r--. 1 root root 0 Nov 19 20:30 ./subdir/how.php drwxr-xr-x. 2 root root 6 Nov 11 00:49 ./abc -rw-r--r--. 1 root root 0 Nov 19 20:28 ./abc.php
下面的命令將刪除tmp目錄下的全部txt文件。
# find /tmp -type f -name '*.txt' -exec rm -f {} \;
一樣的操做也能夠在目錄上執行,只須要用type d代替type f
讓咱們再看一個咱們想要刪除大於100M的文件的例子
# find /root -type f -name '*.log' -size +10M -exec rm -f {} \;
總結
這是一個關於linux find命令的快速教程。find命令是linux
終端下最基本的命令,它讓搜索文件很是容易,它是系統管理所必須的。所以,學習它。若是有問題,能夠在下面的評論區留言。
原文:https://www.binarytides.com/category/linux/linux-commands/