find檔案尋找

來源:互聯網
上載者:User

標籤:find   檔案尋找   

grep、egrep、fgrep這些指令稱為文本尋找,即在給定的文本中找出匹配的內容。

而檔案尋找則是尋找系統中的指定的檔案是否存在,這些指令有locate和find。


locate:非即時尋找,模糊比對,根據資料庫的內容進行搜尋,搜尋速度較快。

如果要產生或更新locate所使用的資料庫的話,可以用updatedb這個指令。


find:是即時、精確尋找,支援眾多的條件,使用範圍較廣。

find文法格式:find 尋找路徑  尋找標準  尋找到以後的處理動作說明:尋找路徑:預設為目前的目錄。尋找標準:預設為制定路徑下的所有檔案。處理動作:預設為顯示。

樣本1:

[[email protected] tmp]# find../.esd-0./.esd-0/socket./.ICE-unix[[email protected] tmp]#
匹配標準:-name ‘filename‘:根據檔案名稱尋找,精確尋找。區分大小寫。-iname ‘filename‘:檔案名稱匹配不區分大小寫。    檔案名稱萬用字元:     *:匹配任意長度的任一字元。     ?:匹配任意一個字元。     []:匹配括弧中的任意一個字元。樣本2:[[email protected] tmp]# find /etc -name passwd /etc/pam.d/passwd/etc/passwd[[email protected] tmp]# 樣本3:[[email protected] tmp]#  find /etc -name ‘*shadow‘/etc/gshadow/etc/shadow[[email protected] tmp]# -user:根據檔案的擁有者進行尋找。-group:根據檔案的屬組進行尋找。-uid:根據uid尋找。-gid:根據gid尋找。-nouser:尋找沒有屬主的檔案。-nogroup:尋找沒有屬組的檔案。樣本4:[[email protected] tmp]# find /home -user frame/home/frame/home/frame/.bash_logout/home/frame/.viminfo/home/frame/.bash_profile/home/frame/.bashrc/home/frame/.bash_history/home/frame/.mozilla/home/frame/.mozilla/plugins/home/frame/.mozilla/extensions/home/frame/.gnome2[[email protected] tmp]# 樣本5:[[email protected] tmp]# find /home -uid 500/home/xguest/home/xguest/.bash_logout/home/xguest/.bash_profile/home/xguest/.bashrc/home/xguest/.bash_history/home/xguest/.mozilla/home/xguest/.mozilla/plugins/home/xguest/.mozilla/extensions/home/xguest/.gnome2[[email protected] tmp]# -type:根據檔案類型尋找。    f:普通檔案    d:目錄    b:塊裝置檔案    c:字元裝置檔案    s:socket檔案    p:pipe管道檔案    l:連結檔案樣本6:[[email protected] keepalived-1.1.17]# find /dev -type s/dev/log[[email protected] keepalived-1.1.17]# ls -l /dev/log srw-rw-rw- 1 root root 0 Jul 12 14:52 /dev/log[[email protected] keepalived-1.1.17]# -size [[+-]nk|M|G]:根據檔案大小尋找。如果沒有跟上大小,則預設單位為位元組。+:表示大於指定的單位,-:表示小於指定大小的檔案。樣本7:[[email protected] keepalived-1.1.17]# find / -size +1G -ls534397 3593220 -rw-r--r--   1 root     root     3679453184 May 11 15:36 /opt/rhel6.3x64.iso[[email protected] keepalived-1.1.17]# 組合條件:    -a:與    -o:或    -not:非    如果指定了多個找到檔案,但是沒有指定組合條件,則預設為-a。樣本8:[[email protected] ~]# find /home -type d -a -user frame/home/frame/home/frame/.mozilla/home/frame/.mozilla/plugins/home/frame/.mozilla/extensions/home/frame/.gnome2[[email protected]er3 ~]# find /home -type d -user frame/home/frame/home/frame/.mozilla/home/frame/.mozilla/plugins/home/frame/.mozilla/extensions/home/frame/.gnome2[[email protected] ~]# -mtime [[+-]n]:修改時間,檔案內容被修改-ctime [[+-]n]:改變時間,檔案屬性被修改-atime [[+-]n]:訪問時間,檔案被查看上述n為天數。-mmin [[+-]n]-cmin [[+-]n]-amin [[+-]n]上述n表示的是分鐘。+n     for greater than n-n     for less than nn      for exactly n說明:如果時間為+5,則表示5天/分鐘前。如果為-5,則表示5天/分鐘內,如果為5,則表示第五天/分鐘這一天。-perm MODE:根據檔案的許可權匹配。確切的匹配許可權,必須為指定的許可權。-perm -MODE:mode中的所有許可權位都必須要設定才滿足條件。-perm /MODE:mode中的任何一個許可權位被設定都滿足條件。find的動作-print:預設的動作。-ls:類似於ls -l的形式顯示每一個檔案的詳細資料。-ok COMMAND {} \; :要執行的動作,和-exec一樣的。{}表示引用找到的檔案。\;結束符,為固定格式。-exec COMMAND {} \; :要執行的動作。樣本:[[email protected] tmp]# find ./ -perm 640 -ls541403    0 -rw-r-----   1 root     root            0 Jul 12 16:00 ./a[[email protected] tmp]#說明:第一個為inode號(541403)。第二個(0)為檔案的大小。剩下的就和ls -l一樣了。[[email protected] tmp]# find ./ -perm 640 -ok chmod 644 {} \;< chmod ... ./a > ? y[[email protected] tmp]# ls -l a-rw-r--r-- 1 root root 0 Jul 12 16:00 a[[email protected] tmp]# [[email protected] tmp]# ls -l a-rw-r----- 1 root root 0 Jul 12 16:00 a[[email protected] tmp]# find ./ -perm 640 -exec chmod 644 {} \;[[email protected] tmp]# ls -l a-rw-r--r-- 1 root root 0 Jul 12 16:00 a[[email protected] tmp]# 說明:-ok和-exec的區別是,-ok需要對每一個動作進行確認,-exec不需要確認。







本文出自 “HeZhang” 部落格,請務必保留此出處http://hezhang.blog.51cto.com/1347601/1437511

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.