歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> linux_find文件查找命令

linux_find文件查找命令

日期:2017/3/3 12:11:20   编辑:Linux技術

文件查找:

locate,非實時查找,在臨時數據庫裡進行查找,模糊匹配。

[root@localhost ~]# locate passwd 查找當前系統裡有沒有passwd這個文件

updatede 手動生成臨時數據庫

優勢:速度快,占用資源少

find:實時查找,精確查找,遍歷指定目錄中的所有文件,速度慢。支持眾多查找標准。支持正則表達式

格式:find 查找路徑查找標准 查找到以後的處理動作

省略查找路徑,就默認為當前目錄

省略查找標准,默認為指定路徑下的所有文件

處理動作:默認為顯示

匹配標准:-name 根據文件名精確查找

[root@localhost~]# find /etc -name 'passwd' 查找etc下是否有passwd這個文件

文件名通配:

? 匹配文件名中的任何單個字符。[...] 匹配[ ]中所包含的任何字符。[!...] 匹配[ ]中非感歎號!之後的字符。* 匹配文件名中的任何字符串,包括空字符串。如:5* 5開頭的所有字符串*5 5結尾的所有字符串*5? 以5為倒數第二個字符的字符串[0-9] 所有以數字的字符[1,2] 1或者2[!0-9] 不是數字的字符 [root@localhost ~]# find /etc -name 'passwd*' 查找etc下passwd開頭的所有文件[root@localhost ~]# find /etc -name '*passwd' 查找etc下passwd結尾的所有文件[root@localhost ~]# find /etc -name '*passwd*'查找etc下名字包含passwd的所有文件-iname 文件名匹配不區分大小寫-regex PATTERN基於正則表達式進行文件名匹配-user 根據文件的屬主進行進行查找[root@localhost ~]# find /tmp -user root 查找tmp下屬於root的文件 -group 根據屬組進行查找-uid根據uid查找-gid 根據gid查找-nouser 查找沒有屬主的文件 [root@localhost ~]# find /tmp –nouser 查找 tmp下沒有屬主的文件-nogroup 查找沒有屬組的文件-type根據文件類型查找 f 普通文件d 目錄c字符設備b 塊設備l 鏈接文件p 管道設備s套接字文件[root@localhost ~]# find /tmp -type d 查找tmp下的目錄[root@localhost ~]# find /tmp -type s查找tmp下的套接字文件-size 指定查找文件的大小[+|-]#K#M[root@localhost ~]# find /etc/ -size 1M –ls 查找etc下1M或接近1M的文件。並執行ls[root@localhost ~]# find /etc/ -size -1M –ls 查找etc下 所有小於1M的文件並執行ls[root@localhost ~]# find /etc/ -size +1M –ls查找etc下 所有大於1M的文件並執行ls#G組合條件:-a與-o 或-not 非默認是與操作[root@localhost ~]# find /tmp -nouser –a -type d 查找tmp下沒有屬主並且文件類型為目錄的文件[root@localhost ~]# find /tmp -nouser -o -type d 查找tmp下沒有屬主或者文件類型為目錄的文件[root@localhost ~]# find /tmp -not -type d 查找tmp下非目錄的文件 1.查找tmp不是目錄或者不是套接字文件的文件[root@localhost ~]# find /tmp -not -type d -o -not -type s 2.查找當前文件屬主下既不是user1又不是user2的文件[root@localhost ~]# find ./ -not -user user1 -a -not -user user2[root@localhost ~]# find ./ -not \( -user user2 -o -user user1 \) 3.查找當前目錄下屬主不是user1或者類型不是目錄的文件[root@localhost tmp]# find ./ -not -user user1 -o -not -type d[root@localhost tmp]# find ./ -not \( -user user1 -a -not -type d \) -mtime 最近一次的創建時間,多少天-ctime 最近一次的改變時間-atime 最近一次的訪問時間 [+|-]# +表示至少幾天沒訪問 -5表示5天之內沒有訪問過 5表示剛好5天沒有訪問過

-mmin 多少分鐘-cmin-amin [+|-]# [root@localhost~]# find /tmp -amin +5 查找tmp下至少5分鐘沒有訪問過的文件,或者說5分鐘之前訪問過的文件 [root@localhost ~]# find /tmp -atime +7 查找tmp下至少7天沒有訪問過的文件 -perm MODE 根據文件權限查找,默認為精確匹配,9位權限都必須匹配[root@localhost~]# find /tmp -perm 755 -ls 查找tmp下文件權限為755的文件並顯示 -perm –MODE包含匹配9位權限 ,列如查找644權限的文件,755也會顯示,只要權限包含有644的9位權限都能被查找 -perm /MODE9位權限中有1位匹配就行[root@localhost~]# find /tmp -perm /755 -ls 查找 tmp下9個權限中有一個權限能匹配到的文件 查找當前文件下 其他用戶有執行權限的文件[root@localhost~]# find ./ -perm -001 動作: -print:顯示 -ls:類似類似ls- l的形式顯示每個匹配到的詳細文件 -ok COMMAND {} \; -ok會讓用戶確認每一個操作 。{}表示引用查找到的文件 -exec COMMAND {} \;[root@localhost~]# find ./ -perm -006 -exec chmod o-w {} \; 查找當前目錄下其他用戶有讀和寫權限的文件,並把寫權限去掉 [root@localhost~]# find ./ -type d -exec chmod +x {} \;找到當前文件下為目錄的文件並把屬主,屬組合其他用戶 都加上執行權限 [root@localhost~]# find ./ -perm -020 -exec mv {} {}.new \;查找當前文件下屬組有寫權限的文件並把其名字改成原名字加.new 查找當前目錄下文件名為.sh結尾並且所有用戶都有執行權限的文件,然後把其他用戶的執行權限刪除[root@localhost~]# find ./ -name "*.sh" -a -perm -111 -exec chmod o-x {} \; 1、 查找/var目錄下屬主為root並且屬組為mail的所有文件

[root@localhost ~]# find /var -user'root' -a -group 'mail' 2、 查找/usr目錄下不屬於root,bin,或student的文件

[root@localhost ~]# find /usr -not-user 'root' -a -not -user 'bin' -a -not -user 'student' 3、 查找/etc目錄下最近一周內內容修改過且不屬於root及student用戶的文件

[root@localhost ~]# find / -ctime -7-a -not -user 'root' -a -not -user'student' 4、 查找當前系統上沒有屬主或者屬組且最近1天內容曾被訪問過的文件,並將其屬組屬主改為root

[root@localhost ~]# find / \( -nouser-o -nogroup \) -a -atime -1 -exec chown root:root {} \;5、 查找/etc目錄下大於1M的文件,並將其文件名寫入/tmp/etc.largefile文件中

[root@localhost ~]# find /etc -size+1M >> /tmp/etc.largefiles[root@localhost tmp]# find /etc -size+1M -exec echo {} >> /tmp/etc.largefiles \;[root@localhost tmp]# find /etc -size+1M | xargs echo > /tmp/etc.largefiles 6、 查找/etc目錄下所有用戶都沒有寫權限的文件,顯示出其詳細信息

[root@localhost ~]# find /etc -not -perm /222 –ls

xargs: 不用占位符{} ,也不需要\ 。但是需要管道|放到xargs前面

本文出自 “linux運維” 博客,謝絕轉載!

Copyright © Linux教程網 All Rights Reserved