find文件查找

 

 
文件的命令;locate  find
 
一、locatelocate這個命令是對數據庫進行遍歷;創建數據庫的命令:updatedb, locate查找文件速度快,模糊匹配
語法:locate [option] filename
選項:-i 忽略大小寫
-n 查找結果的前N
eg: locate  i   passwd
    locate  -n  5   passwd
 
 二、find
Find對指定查找目錄進行遍歷查找,精確查找文件名
 
  使用格式 :語法:find [DIR,...] [CRITERIA 規則] [ACTION ...]
 eg:   find /etc /var   -name passwd   #目錄之間空格
 
        1)根據文件屬性查找
           #  -name       文件名查找)
           #  -iname      忽略大小寫 
 
*任意的字符
       ?任意的單個字符
         [ ]括號裏面的任意一個字符
find /etc –name “*g[br]ub?”
           #  -user         屬主來查找文件
           #  -group   屬組來查找文件
            #find  /etc -uid  500  uid500 文件
           #find  /etc  -gid  888  gid888的文件
 
         2)邏輯符號-a    -o   –not的使用
           # -a  兩個不同的條件必須滿足
          # -o  兩個不同的條件滿足其一
           # -not
      
    3)根據文件時間戳來查找文件
             stat命令來查文件時間信息:
天爲單位:
     -atime 訪問時間
-mtime   修改時間
-ctime 改變時間
 
 分鐘爲單位:
           #-amin
           #-mmin
           #-cmin
 
          #find  /tmp  –atime  +5       五天內沒訪問過的文件
          #find  /tmp  -atime  -5           五天內訪問過的文件
          
          4)根據文件類型來查找
                -type
                      f     普通文件
                      d     目錄文件
                      l     鏈接文件
                      b     塊設備文件
                      c     字符設備文件
                      p     管道文件
                      s     socket文件
   
 文件大小查找 -size
 
   #find  /tmp  -size  2M    //查找在1-2M之間的文件
   #find  /tmp  -size  +2M    //查找大於2M的文件
   #find  /tmp  -size  -2M    //查找小於2M的文件
 
 
   文件權限查找
 
   -perm
 
   #find  /etc  -perm  755   //查找權限是755的文件
  #find  /tmp  -perm  +222  //只要有一類用戶一個權限位匹配就行
 #find  /tmp  -perm  -222 //所有類別用戶要有寫權限的文件
 
 5-nouser  -nogroup
 
  #find  /etc -nogroup –a –nouser  //查找既沒有屬主又沒有屬組的文件
 
 action動作
 
   -print          默認動作
   -ls            ls 顯示出來
     -ok [commend]     查找執行命令後詢問
   -exec [commend]     查找執行命令後不詢問
 
-exec rm {} \;{}表示佔位符,是okexec選項的用法必須以\;結尾
-exec mv {} {}.txt \; 給查找到的文件加後綴.txt
 
實例:
1、查找/var目錄下屬主爲root並且屬組爲mail的所有文件;
# find /var -user root -group mail
 
2、查找/usr目錄下不屬於root,bin,student的文件;
# find /usr -not -user root -a -not -user bin -a -not -user student
# find /usr -not \( -user root -o -user bin -o -user student \)
 
-not !
 
3、查找/etc目錄下最近一週內內容修改過且不屬於rootstudent用戶的文件;
# find /etc -mtime -7 -a -not -user root -a -not -user student
# find /etc -mtime -7 -a -not \( -user root -o -user student \)
 
4、查找當前系統上沒有屬主或屬組且最近1天內曾被訪問過的文件,並將其屬主屬組均修改爲root
# find / \( -nouser -o -nogroup \) -a -atime -1 -exec chown root:root {} \;
 
5、查找/etc目錄下大於1M的文件,並將其文件名寫入/tmp/etc.largefiles文件中;
# find /etc -size +1M -exec basename {} >> /tmp/etc.largefiles
 
6、查找/etc目錄下所有用戶都沒有寫權限的文件,顯示出其詳細信息;
# find /etc -not -perm /222 -ls
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章