Linux系統文件查找locate和find命令工具使用

講到Linux的文件查找,首先大家一般在Windows中有過查找過文件,我們知道window是以文件名結尾來識別文件的,使用一些通配符*.doc,*.txt來檢索一類文件,縮小范圍,實現快速定位文件,在Linux中,也有文件查找的需要不過實現的方式將更加靈活;

1)locate工具 

2)find工具

1.locate

Linux中也有像window中那樣模糊匹配的查找方式,那就是locate工具下面來詳細介紹locate;

  

  (1)工作原理:

     locate查找文件不是對當前文件系統遍歷查找,而是在locate自己的數據庫locateDB中查找,根據系統負載狀況,只需要在某個特定的時間點來遍歷整個文件系統,把所有遍歷的文件名放到自己的數據庫中,這樣在使用locate工具查找時只需要檢索自己的數據庫文件,速度是非??斓?,但是又會帶來一下幾個問題;

       a:數據庫更新時需要遍歷整個系統,然后記錄在locateDB中,遍歷整個系統時,需要消耗大量的系統資源;所以系統一般只在特定的時間自動更新,也可以手動;

       b:locate在自己的數據庫文件中檢索,不用在系統中檢索,查找速度是非??斓模?/span>

       c:系統中的文件操作是很頻繁的,創建移動復制刪除一個文件后,要想查找,對于這些最近更新的文件,是沒辦法準確定位的,因為locate工具檢索的是自己的數據庫文件,locateDB數據庫文件只在特定的時間更新,所以很難實現實時查找;


       d:locate工具查找文件時只能對文件名進行globe通配匹配條件,沒辦法根據文件的其它屬性針對查找,也就是說很難實現文件的精確匹配查找;

 

  (2)locate命令工具使用:

        命令:locate

語法:

  locate [option]…pattern…

        -i:ignore-case

-d:locateDB directory

-r:regex

-n #:只顯示前#個匹配查找到的文件;

-c:統計查找到的文件個數;

   (2)locateDB數據庫文件在系統中的位置:

        /var/lib/mlocate/mlocate.db

[root@xia7 ~]# ll /var/lib/mlocate/mlocate.db 
-rw-r----- 1 root slocate 3223706 Aug 16 09:12 /var/lib/mlocate/mlocate.db
[root@xia7 ~]#

   (3).如何手動更新locatdb? 

        updatedb

[root@xia7 ~]# updatedb 
[root@xia7 ~]#

2.find

    前面所說的locate工具雖然速度檢索快,但是不能實現實時查找和特定文件屬性查找,Linux系統中還有一款強

大的查找工具find,能夠完成更多的高級查找條件,實現準確定位文件;

   (1)find工作原理:

  find工具可以指定特定路徑和查找條件,對指定路徑下所有路徑根據查找條件完整遍歷,查找完以后還能做相應的處理動作;工具功能非常強大;簡述特性如下:

       a:find是對指定路徑下完整遍歷,默認不指定是當前目錄,因為查找的是系統路徑,所以是實時查找;

       b:由于查找的路徑是系統完整路徑,尤其是文件系統很大時,查找起來系統負載壓力會很大,同時查找速度相對于locate的數據庫文件查找有點慢;

       c:find的查找條件很多可以基于正則表達式以及其他文件屬性,權限,大小,時間戳,屬主屬組,文件類型作為匹配查找條件,這樣就能對文件實現精確定位查找;

       b:而且對查找到的文件還能多相應的處理動作,一般默認是print,還能多其他很多對文件操作的處理命令;這樣對文件的處理能力得到進一步加強;

   (2)find命令工具使用:

        命令:find 

        語法:

            find [option]…[查找路徑]  [查找條件] [處理動作]

        常用選項:

            -P:查找的文件如果是鏈接文件,將查找軟鏈接文件指向的位置;

 

查找路徑:

   不寫默認從當前路徑向下查找

可以指定路徑

查找條件:

    a:根據文件名和inode查找:

                -name filename :根據文件名查找;

-iname filename :查找文件名時忽略大小寫;

-inum inode:根據inode號查找;

-regex "pattern" :根據模式匹配查找文件;

[root@xia7 ~]# find /etc -name passwd
/etc/passwd
/etc/pam.d/passwd

[root@xia7 ~]# find /etc -iname SHADOW
/etc/shadow

[root@xia7 ~]# ll -i /etc/passwd
134347177 -rw-r--r-- 1 root root 2069 Aug 15 10:23 /etc/passwd
[root@xia7 ~]# find / -inum 134347177
/etc/passwd
[root@xia7 ~]# 

[root@xia7 ~]# find / -regex ".*passwd$"
/usr/bin/lppasswd
/usr/bin/passwd
/usr/bin/gpasswd
/usr/bin/smbpasswd
/usr/bin/vncpasswd
/usr/bin/kdepasswd
/usr/sbin/lpasswd
/usr/sbin/chpasswd

b:根據屬主屬組查找:

   

-user username:根據屬主查找文件;

-group groupname:根據屬組查找文件;

-uid userid:根據用戶uid查找文件;

-gid usergid:根據用戶gid查找文件;

-nouser:查找沒有屬主的文件;

-nogroup:查找沒有屬組的文件;

[root@xia7 ~]# find /home -user xia
/home/xia
/home/xia/.mozilla
/home/xia/.mozilla/extensions
/home/xia/.mozilla/plugins
/home/xia/.bash_logout
/home/xia/.bash_profile
[root@xia7 ~]# 

[root@xia7 ~]# find /home -group xiashixiang
/home/xiashixiang
/home/xiashixiang/.mozilla
/home/xiashixiang/.mozilla/extensions
/home/xiashixiang/.mozilla/plugins


[root@xia7 ~]# find /home/ -uid 1000
/home/mageedu
/home/mageedu/.mozilla
/home/mageedu/.mozilla/extensions
/home/mageedu/.mozilla/plugins


[root@xia7 ~]# userdel xiashixiang
[root@xia7 ~]# find /home -nouser 
/home/xiashixiang
/home/xiashixiang/.mozilla/plugins
/home/xiashixiang/.bash_logout
/home/xiashixiang/.bash_profile
/home/xiashixiang/.bashrc
[root@xia7 ~]# ll /home/xiashixiang/.bashrc 
-rw-r--r-- 1 1002 1002 231 Nov 20  2015 /home/xiashixiang/.bashrc
[root@xia7 ~]#

c:根據文件類型查找:

   

-type f:查找文件類型為普通文件;

-type d:查找文件類型為目錄文件;

-type l:查找文件類型為軟連接文件;

-type b:查找文件類型為塊設備文件;

-type c:查找文件類型為字符設備文件;

-type s:查找文件類型為套接字文件;

-type p:查找文件類型為管道文件;

[root@xia7 ~]# find /home -type d
/home
/home/mageedu
/home/mageedu/.mozilla

[root@xia7 ~]# find /run/ -type s
/run/abrt/abrt.socket
/run/NetworkManager/private
/run/gssproxy.sock
/run/systemd/notify
[root@xia7 ~]# ll /run/systemd/notify 
srwxrwxrwx 1 root root 0 Aug 16  2016 /run/systemd/notify
[root@xia7 ~]# 

[root@xia7 ~]# find / -type p
/run/dmeventd-client
/run/dmeventd-server
[root@xia7 ~]# ll /run/dmeventd-client 
prw------- 1 root root 0 Aug 16 15:48 /run/dmeventd-client
[root@xia7 ~]#

d:根據文件大小查找文件:

    

-size [+|-]#[k|M|G]

-size 5k :表示查找文件大小4k-5k之間的所有文件;

-size -5k :表示查找文件大小為4k以下的文件;

-size +5k :表示查找文件大小為5k以上的所有文件;

[root@xia7 dir]# ll -h /dir/
total 28M
-rw-r--r-- 2 root root   15 Aug 16 15:57 ceshi
-rw-r--r-- 2 root root   15 Aug 16 15:57 ceshi_hlink
-rw-r--r-- 1 root root 2.0M Aug 16 16:20 file2
-rw-r--r-- 1 root root 3.0M Aug 16 16:21 file3
-rw-r--r-- 1 root root 4.0M Aug 16 16:21 file4
-rw-r--r-- 1 root root 5.0M Aug 16 16:21 file5
-rw-r--r-- 1 root root 6.0M Aug 16 16:21 file6
-rw-r--r-- 1 root root 7.0M Aug 16 16:22 file7
[root@xia7 dir]# find /dir/ -size 4M
/dir/file4
[root@xia7 dir]# find /dir/ -size +4M
/dir/file5
/dir/file6
/dir/file7
[root@xia7 dir]# find /dir/ -size -4M
/dir/
/dir/ceshi
/dir/ceshi_hlink
/dir/file2
/dir/file3
[root@xia7 dir]#

e:根據文件的時間戳查找文件:

 -atime -mtime -ctiem :以天為單位24小時;

 -amin -mmin -cmin :表示以分鐘為單位;


 -atime [+|-]#

 -atime 5 :表示查找的文件訪問時間為5-6天之內的文件;

 -atime +5 :表示查找的文件訪問時間為6天以上的文件;

 -atime -5 :表示查找的文件訪問時間為5天以內的文件;

 其他時間表示的區間用法一樣;

[root@xia7 dir]# find / -atime 3
find: ‘/proc/1558/task/1558/fd/6’: No such file or directory
find: ‘/proc/1558/task/1558/fdinfo/6’: No such file or directory
find: ‘/proc/1558/fd/6’: No such file or directory
find: ‘/proc/1558/fdinfo/6’: No such file or directory
/usr/bin/kill
/usr/share/man/man1/sort.1.gz
/usr/share/man/man1p/sort.1p.gz
/usr/share/man/man3/sort.3pm.gz
/etc/systemd/journald.conf
/var/log/messages-20160813
/var/log/cron-20160814
/var/log/maillog-20160814
/var/log/secure-20160814
/var/log/spooler-20160814
/var/spool/abrt/ccpp-2016-08-03-05:17:40-3996/not-reportable
[root@xia7 dir]# stat /usr/bin/kill
  File: ‘/usr/bin/kill’
  Size: 29264     	Blocks: 64         IO Block: 4096   regular file
Device: 803h/2051d	Inode: 588838      Links: 1
Access: (0755/-rwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2016-08-13 11:34:01.328118074 +0800
Modify: 2015-11-20 20:37:48.000000000 +0800
Change: 2016-07-28 01:45:05.517002449 +0800
 Birth: -
[root@xia7 dir]# date
Tue Aug 16 16:30:15 CST 2016
[root@xia7 dir]# 

[root@xia7 dir]# find /etc/init.d/ -atime -3
/etc/init.d/
/etc/init.d/functions
/etc/init.d/network
[root@xia7 dir]# stat /etc/init.d/network 
  File: ‘/etc/init.d/network’
  Size: 6630      	Blocks: 16         IO Block: 4096   regular file
Device: 802h/2050d	Inode: 7807        Links: 1
Access: (0755/-rwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2016-08-15 19:25:33.748523241 +0800
Modify: 2015-09-16 19:51:07.000000000 +0800
Change: 2016-07-28 01:45:13.605002630 +0800
 Birth: -
[root@xia7 dir]# date
Tue Aug 16 16:33:17 CST 2016
[root@xia7 dir]# 

[root@xia7 dir]# find /etc/init.d/ -atime +7
/etc/init.d/README
/etc/init.d/netconsole
[root@xia7 dir]# stat /etc/init.d/netconsole 
  File: ‘/etc/init.d/netconsole’
  Size: 2989      	Blocks: 8          IO Block: 4096   regular file
Device: 802h/2050d	Inode: 7806        Links: 1
Access: (0755/-rwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2016-08-03 02:56:47.264675229 +0800
Modify: 2015-09-16 19:51:07.000000000 +0800
Change: 2016-07-28 01:45:13.605002630 +0800
 Birth: -
[root@xia7 dir]# date 
Tue Aug 16 16:35:11 CST 2016
[root@xia7 dir]#

f:根據權限查找文件:

  

  -perm[/|-] mode 

  -perm 644 :查找權限為644的文件,精確查找;

  -perm -444 :查找每組權限只要有讀權限的文件;

  -perm /222 :查找每組權限只要有一組權限是寫權限的文件;

  -perm -020 :表示查找屬組有寫權限的文件,0表示忽略權限位;

[root@xia7 dir]# find /home -perm 644
/home/mageedu/.bash_logout
/home/mageedu/.bash_profile
/home/mageedu/.bashrc
/home/xia/.bash_logout
/home/xia/.bash_profile
/home/xia/.bashrc
/home/xiashixiang/.bash_logout
/home/xiashixiang/.bash_profile
/home/xiashixiang/.bashrc
[root@xia7 dir]# ll /home/xiashixiang/.bashrc 
-rw-r--r-- 1 1002 1002 231 Nov 20  2015 /home/xiashixiang/.bashrc
[root@xia7 dir]# 

[root@xia7 dir]# find /tmp -perm -222
/tmp
/tmp/.X11-unix
/tmp/.font-unix
/tmp/.ICE-unix
/tmp/.Test-unix
/tmp/.XIM-unix
/tmp/systemd-private-6ca5fb894d61414da9daecbef334b022-cups.service-q0ZQQD/tmp
[root@xia7 dir]# ll /tmp/.font-unix -d
drwxrwxrwt. 2 root root 6 Jul 28 01:45 /tmp/.font-unix
[root@xia7 dir]# 

[root@xia7 dir]# ll
total 27656
-rw-r--r-- 2 root root      15 Aug 16 15:57 ceshi
-rw-r--r-- 2 root root      15 Aug 16 15:57 ceshi_hlink
-rw-r--r-- 1 root root 2097152 Aug 16 16:20 file2
-r-xrwxr-x 1 xia  xia  3145728 Aug 16 16:21 file3
-rw-r--r-- 1 root root 4194304 Aug 16 16:21 file4
-rw-r--r-- 1 root root 5242880 Aug 16 16:21 file5
-rw-r--r-- 1 root root 6291456 Aug 16 16:21 file6
-rw-r--r-- 1 root root 7340032 Aug 16 16:22 file7
[root@xia7 dir]# su - xia
Last login: Tue Aug 16 16:55:09 CST 2016 on pts/0
[xia@xia7 ~]$ find /dir -perm /111
/dir
/dir/file3
[xia@xia7 ~]$

  

 

g:組合查找條件:

   與:-a :并且的關系,條件之間可以省略;

或:-o :條件之間是或者的關系;

非:-not,!:對條件取反;

根據多個相同條件之間可以使用德.摩根定律,這樣對后面的動作處理對象有個明確的范圍;

非( A -a B ) = (非A) -o (非B)

非( A -o B ) = (非A) -a (非B)

root@xia7 dir]# find /home/ -type d -a -nouser
/home/xiashixiang
/home/xiashixiang/.mozilla
/home/xiashixiang/.mozilla/extensions
/home/xiashixiang/.mozilla/plugins
[root@xia7 dir]# ll /home/xiashixiang/ -d
drwx------ 3 1002 1002 74 Aug 15 10:23 /home/xiashixiang/
[root@xia7 dir]#


h:處理動作:

   -print:這是默認的處理動作。顯示到stdout;

-ls :相當于于執行ls -l 命令查看匹配到的文件詳細信息;

-delete:對查找的文件刪除,注意:動作很危險,在工作中謹慎使用,避免誤操作;

-fls file:對查找的文件ls -l的詳細輸出保存到指定文件中;

            

            -ok COMMAND {} \;對查找到的文件,每個文件都執行CMMMAND命令,后面的{}花括號表示引用查找

                到的文件自身;“\;“表示特定固定格式;-ok,表示對每條執行都進行確認問答,直接

                回車表示忽略;

            -exce COMMAND {} \; -exce:表示忽略全部執行前的確認,直接執行命令;

            

find | xargs COMMAND – :命令一般對參數會有限制,這時超過限制參數量,命令會報錯,為了避免報錯,

                可以使用xargs命令,把查找到的結果,全部通過管道傳遞給xargs命令,在做相應的操作命令;

[root@xia7 dir]# find /home/ -type d -a -nouser -ls
 28066    0 drwx------   3 1002     1002           74 Aug 15 10:23 /home/xiashixiang
67380614    0 drwxr-xr-x   4 1002     1002           37 Jul 28 01:43 /home/xiashixiang/.mozilla
134369665    0 drwxr-xr-x   2 1002     1002            6 Jun 10  2014 /home/xiashixiang/.mozilla/extensions
201368685    0 drwxr-xr-x   2 1002     1002            6 Jun 10  2014 /home/xiashixiang/.mozilla/plugins
[root@xia7 dir]#

                   

           

           

          

原創文章,作者:xiashixiang,如若轉載,請注明出處:http://www.www58058.com/36454

(0)
xiashixiangxiashixiang
上一篇 2016-08-18 10:10
下一篇 2016-08-18 10:10

相關推薦

  • 基于ssl功能實現mysql主從復制

    基于ssl功能實現mysql主從復制         證書準備:                                  CA證書…

    2016-11-22
  • Linux系統網絡屬性管理之bond

    Bonding 就是將多塊網卡綁定同一IP地址對外提供服務,可以實現高可用或者負載均衡。當然,直接給兩塊網卡設置同一IP地址是不可能的。通過bonding,虛擬一塊網卡對外提供連接,物理網卡的被修改為相同的MAC地址。 Bonding的工作模式 ?Mode 0 (balance-rr) 輪轉(Round-robin)策略:從頭到尾順序的在每一個slave接口…

    Linux干貨 2016-09-18
  • 通過FTP服務怒刷基礎功法熟練度(匿名篇)

        Linux門派多種多樣,那么本次就講講本人刷基本命令熟練度的方法。FTP原理什么的都不說了,網上有很多。直接上酸菜~學徒水平,大師勿笑。     本篇搭載的是FTP匿名用戶訪問,同時可以在服務器上進行創建刪除等操作。危險系數有點點大,僅推薦用來刷命令熟練度使用。我用的Li…

    2017-07-25
  • awk用法二

      3、awk的printf命令     格式:printf format, item1 item2…     要點:      (1) 要指定format;      (2) 輸出時不會自動換行,如需換行則…

    Linux干貨 2015-12-24
  • 關于IO的同步,異步,阻塞,非阻塞

    上次寫了一篇文章:Unix IO 模型學習。恰巧在這次周會的時候,@fp1203 (goldendoc成員之一) 正好在講解poll和epoll的底層實現。中途正好討論了網絡IO的同步、異步、阻塞、非阻塞的概念,當時講下來,大家的理解各不相同,各執己見。搜索了網絡上的一些文章,觀點也各不相同,甚至連wiki也將異步和非阻塞當成一個概念在解釋。  &…

    Linux干貨 2015-04-02
  • 計算機的組成和其功能

    圖:計算機組成架構 計算機由硬件和軟件組成 硬件部分: CPU:又稱中央處理器,整個系統最高執行單元,執行各種運算,控制電腦自動協調地完成各種操作。 主板:它把計算機的各個部件緊密的連接在一起,各個部件通過主板進行數據傳輸,計算機重要的“交通樞紐”都在主板上,他的工作穩定性影響整機的工作穩定性。因同CPU的插腳和性能不同,所以針對不同的CPU也有不同的主板?!?/p>

    Linux干貨 2016-08-08

評論列表(1條)

  • 馬哥教育
    馬哥教育 2016-08-19 11:37

    find命令是筆試中常見的考點,需要我們多加練習,予以掌握,課堂練習需要按時完成,通過練習,可以鞏固我們所學的東西。

欧美性久久久久