Linux常用命令(3)–檔案搜尋命令

來源:互聯網
上載者:User

1、命令名稱:which

  命令所在路徑:/usr/bin/which

  執行許可權:所有使用者

  功能描述:顯示系統命令所在目錄

  文法:which [命令名稱]

[root@localhost ~]# which ls alias ls='ls --color=auto'        /bin/ls[root@localhost ~]# 

 

2、命令名稱:find

  執行許可權:所有使用者

  功能描述:尋找檔案或目錄

  文法:find [搜尋路徑] [搜尋索引鍵]

   1) -name 根據名字尋找

    * 匹配任一字元

    ? 匹配單個字元

 

在目錄/etc中根據名字尋找檔案init[root@localhost ~]# find /etc -name init/etc/webmin/init/etc/kdump-adv-conf/kdump_initscripts/init/etc/init/etc/sysconfig/init[root@localhost ~]# 使用*萬用字元尋找以init開頭的檔案[root@localhost ~]# find /etc -name init*/etc/selinux/targeted/contexts/initrc_context/etc/inittab/etc/webmin/inittab/etc/webmin/init/etc/kdump-adv-conf/kdump_initscripts/init/etc/init/etc/init/init-system-dbus.conf/etc/rc.d/init.d/etc/sysconfig/init/etc/sysconfig/network-scripts/init.ipv6-global/etc/init.d[root@localhost ~]# 使用?萬用字元尋找以init開頭,後面有三個字元的檔案[root@localhost ~]# find /etc -name init???/etc/inittab/etc/webmin/inittab[root@localhost ~]#

  2)-size 根據檔案大小尋找 以資料區塊(block)為單位 512位元組 = 0.5KB

    大於 +

    小於 -

    等於 直接跟數值

在/etc目錄下尋找大於1MB的檔案 1MB = 1024KB = 2048資料區塊(block)[root@localhost ~]# find  /etc -size +2048/etc/selinux/targeted/policy/policy.24/etc/selinux/targeted/modules/active/policy.kern/etc/gconf/gconf.xml.defaults/%gconf-tree.xml[root@localhost ~]# 

  3)-user 根據檔案所有者尋找

在/home目錄下尋找所有者為luxh的檔案[root@localhost ~]# find /home -user luxh

  4)根據時間尋找

    (以天為單位):ctime、atime、mtime

    (以分鐘為單位):cmin、amin、mmin

    c-->change 改變,表示檔案的屬性被修改過,例如所有者、所屬組、許可權被改變

    a-->access 訪問,標示檔案被訪問過

    m-->modify 修改,表示檔案的內容被修改過

    - 表示多長時間之內

    + 表示超過多少時間

在/etc目錄下尋找在1天之內被修改過屬性的檔案和目錄[root@localhost ~]# find /etc -ctime -1/etc/etc/resolv.conf/etc/mtab/etc/avahi/etc/localtime[root@localhost ~]#在/etc目錄下尋找在兩個小時之內被修改過內容的檔案(兩個小時就是120分鐘)[root@localhost ~]# find /etc -mmin -120/etc/etc/resolv.conf/etc/mtab[root@localhost ~]#

  5)-type 根據檔案類型尋找

    f 二級制檔案,l軟連結檔案,d目錄

尋找/etc目錄下的目錄檔案[root@localhost ~]# find /etc -type d

  6)串連符:

    6.1)-a and 邏輯與, -o or 邏輯或

在/etc目錄下尋找大於80MB小於100MB的檔案[root@localhost ~]# find /etc -size +163840 -a -size -204800

    6.2)find ...  -exec 命令 {} \; 或者 find ...  -ok 命令 {} \;   -ok會有詢問過程     

      {} find查詢的結果

      \   轉義符,使得符號和命令使用本身的含義

      ; 結果,表示語句的結束

在/etc目錄下尋找inittab檔案並顯示其詳細資料[root@localhost ~]# find /etc -name inittab -exec ls -l {} \;-rw-r--r--. 1 root root 884 May  5  2012 /etc/inittabtotal 4-rw-------. 1 root bin 42 May 16  2012 config[root@localhost ~]# 尋找並刪除/abc目錄下的testfile[root@localhost ~]# find /abc -name testfile -exec rm -rf {} \;尋找並刪除/abc目錄下的a.txt,使用-ok會提示詢問[root@localhost ~]# find /abc -name a.txt -ok rm {} \;< rm ... /abc/a.txt > ? y
尋找/etc目錄下以init*開頭的目錄檔案並且詳細顯示它們的資訊[root@localhost ~]# find /etc -name init* -a -type f -exec ls -l {} \;-rw-r--r--. 1 root root 30 Dec  7  2011 /etc/selinux/targeted/contexts/initrc_context-rw-r--r--. 1 root root 884 May  5  2012 /etc/inittab-rwxr-xr-x. 1 root root 4781 Oct 29  2009 /etc/kdump-adv-conf/kdump_initscripts/init-rw-r--r--. 1 root root 130 Jul 18  2011 /etc/init/init-system-dbus.conf-rw-r--r--. 1 root root 1154 Dec  8  2011 /etc/sysconfig/init-rwxr-xr-x. 1 root root 4623 Oct  7  2011 /etc/sysconfig/network-scripts/init.ipv6-global[root@localhost ~]#

  7)-inum 根據檔案i節點尋找

    ls -i  顯示檔案的i節點

[root@localhost ~]# ls -i /abc  5355 issue.hard  129390 issue.soft  130438 prem[root@localhost ~]# find /abc -inum 5355/abc/issue.hard[root@localhost ~]#

 

有時候可能別人建立了一些命名奇怪的檔案,使用rm刪不掉[root@localhost ~]# ls -l /abctotal 4-rw-r--r--. 1 root root    0 Nov 20 06:09 a b-rw-r--r--. 3 root root    0 Nov 15 04:51 issue.hardlrwxrwxrwx. 1 root root   10 Nov 15 04:43 issue.soft -> /etc/issuedrwxr-xr-x. 2 root root 4096 Nov 15 05:36 prem[root@localhost ~]# rm a brm: cannot remove `a': No such file or directoryrm: cannot remove `b': No such file or directory[root@localhost ~]# 這個我們可以根據檔案的i節點刪除[root@localhost ~]# ls -i /abc a b    5355 issue.hard  129390 issue.soft  130438 prem[root@localhost ~]# find /abc -inum 129330 -exec rm {} \;[root@localhost ~]# ls -i /abc issue.hard  129390 issue.soft  130438 prem[root@localhost ~]#

  

3、命令名稱:locate

  執行許可權:所有使用者

  功能描述:尋找檔案或目錄

  文法:locate  [搜尋索引鍵]

[root@localhost ~]# locate inittab

  locate 是在一個系統定期更新的檔案資料庫中尋找,速度比較快。系統自動調用updatedb定期更新目錄檔案資料庫

 

4、命令名稱:updatedb

  執行許可權:root

  功能描述:建立整個系統目錄檔案的資料庫

  文法:updatedb

 

5、命令名稱:grep

  執行許可權:所以使用者

  功能描述:在檔案中搜尋字串匹配的行並輸出

  文法:grep [指定字串] [源檔案]

在/etc/services檔案中尋找tftp匹配的行[root@localhost ~]# grep tftp /etc/servicestftp            69/tcptftp            69/udptftp-mcast      1758/tcptftp-mcast      1758/udpmtftp           1759/udp        spss-lmsubntbcst_tftp  247/tcp                 # SUBNTBCST_TFTPsubntbcst_tftp  247/udp                 # SUBNTBCST_TFTPetftp           1818/tcp                # Enhanced Trivial File Transfer Protocoletftp           1818/udp                # Enhanced Trivial File Transfer Protocoltftps           3713/tcp                # TFTP over TLStftps           3713/udp                # TFTP over TLS[root@localhost ~]#

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.