文本尋找利器find的使用

來源:互聯網
上載者:User

一、find概述;
作為Linux系統營運人員來說find命令也是較長用的一個命令,個人覺得也是最有用的一個,同時也 是最混亂的一個。因為它的文法與其他命令的標準文法不同,同時也支援grep裡面的萬用字元尋找。也可以允許按檔案名稱,檔案類型,使用者甚至是時間戳記尋找。不但可以找到具有這些屬性任意組合的檔案,還可以對它找到的檔案執行操作。

功能:在目錄結構中搜尋檔案,並執行指定的操作。此命令提供了相當多的尋找條件,功能很強大。
文法:find 起始目錄 尋找條件 操作
說明:find命令從指定的起始目錄開始,遞迴地搜尋其各個子目錄,尋找滿足尋找條件的檔案並對之採取相關的操作。

二、find命令的格式;
find [options] [尋找路徑] [尋找條件][處理動作]
  預設的處理動作是顯示

  尋找路徑;預設尋找目前的目錄

  尋找條件;預設尋找指定路徑下的所有檔案

  處理動作;預設為顯示

find常用選項

選 項 功能作用
-name “檔案名稱”;支援使用檔案名稱通配機制 *、?、[]、[^]

-iname

-iname “檔案名稱”;尋找是不區分字元大小寫
-uid 根據給定的uid尋找所有檔案
-gid 根據給定的gid尋找所有檔案
-user 根據屬主尋找檔案
-group 根據組名尋找檔案
-nouser 尋找沒有屬主的檔案
-nogroup 尋找沒有屬組的檔案
-a,-o -a表示為與,同時滿足。-o表示或,滿足尋找條件即可
-type

尋找某一類型文檔;

b;塊裝置文檔、d;目錄、c;字元裝置文檔、

l;符號連結文檔、f;普通文檔

-prune 不在當前指定的路徑尋找。假如同時指定了-depth選項,則prune被忽略
-empty 尋找大小為0的目錄或檔案
-size 根據檔案大小尋找 常用單位有;K、M、G
-depth 尋找是,首先尋找目前的目錄文檔,然後在找子目錄尋找
-exec 對匹配的文檔執行所給的命令 {}預留位置
-ok 互動-exec; 用於指定其他命令的 {}預留位置
-ls 長格式輸出各檔案資訊
-print 列印在標準輸出上
-mtime -n,+n 按照文檔更改時間尋找。 -n指距離現在時間n天以內;+n 指n天以外
-atime +,指定天數之外被訪問過 -,是指定天數之內被訪問過
-fstype f1,f2 查更改時間比f1新但比f2舊的文檔
-perm 按許可權來尋找

 

find樣本;
1、 -name
  按檔案名稱尋找以.conf結尾的檔案,兩個結合了萬用字元尋找
[root@station103 ~]# find /etc -name "*.conf"  #按檔案名稱尋找
/etc/sasl2/smtpd.conf
/etc/openldap/ldap.conf
/etc/lftp.conf
/etc/mtools.conf
/etc/NetworkManager/NetworkManager.conf
/etc/ld.so.conf
/etc/plymouth/plymouthd.conf

1234567 [root@station103 ~]# find /etc -name "???.conf"
/etc/yum.conf
/etc/gai.conf
/etc/sos.conf
/etc/samba/smb.conf
/etc/init/tty.conf
/etc/init/rcS.conf

2、-iname
  檔案名稱不區分大小寫尋找

[root@station103 tmp]# find -iname "*.txt"    不區分大小寫尋找
./KKG.txt
./aa.txt
./nba.txt
./QW.txt
./hao.txt
./abc.txt
./dW.txt
./hddW.txt

3、-uid
  根據uid尋找

[root@station103 tmp]# find -uid 517
./kd.txt
./ii.txt
./oo.doc

4、-gid
  根據gid尋找

-rw-rw-r--  1  517  518      0 Feb 19 08:07 oo.doc
-rw-r--r--  1 root root      0 Feb 19 08:00 QW.txt
drwx------  2 root root  4096 Feb 16 07:32 yang
[root@station103 tmp]# find -gid 518
./kd.txt
./ii.txt
./oo.doc
[root@station103 tmp]#

5、-user
  根據使用者尋找檔案或目錄

[root@station103 tmp]# find -user yang
./hao.doc
./kkk.txt
./aabb.txt
[root@station103 tmp]#

6、-group
  根據組名尋找檔案或目錄
[root@station103 tmp]# ll
total 200
drwxr-xr-x  2 aa  hao    4096 Feb 20 04:56 aa
-rw-r--r--  1 aa  hao      0 Feb 20 04:57 abcd.txt
-rw-rw-r--  1 yang yang      0 Feb 19 08:05 hao.doc
-rw-rw-r--  1 aa    518      0 Feb 19 08:07 ii.txt
-rw-rw-r--  1 aa    518      0 Feb 19 08:07 kd.txt
-rw-rw-r--  1 aa    518      0 Feb 19 08:07 oo.doc
-rw-r--r--  1 aa  hao      0 Feb 20 04:57 yang.doc
[root@station103 tmp]# find -group hao
./aa
./abcd.txt
./yang.doc
[root@station103 tmp]#

7、-nouser
  列出/tmp下沒有使用者的檔案
-rw-rw-r--  1  517  518      0 Feb 19 08:07 ii.txt
-rw-rw-r--  1  517  518      0 Feb 19 08:07 kd.txt
drwxr-xr-x 11 root root  4096 Feb 15 21:40 logs
-rw-rw-r--  1  517  518      0 Feb 19 08:07 oo.doc
drwx------  2 root root  4096 Feb 16 07:32 yang
-rw-r--r--  1  517 hao      0 Feb 20 04:57 yang.doc
[root@station103 ~]# find /tmp -nouser
/tmp/aa
/tmp/abcd.txt
/tmp/kd.txt
/tmp/yang.doc
/tmp/ii.txt
/tmp/oo.doc
[root@station103 ~]#

8、-nogroup
  列出/tmp下沒有使用者組的檔案
drwxr-xr-x  2 root root  4096 Feb 16 10:09 hello
-rw-rw-r--  1  517  518      0 Feb 19 08:07 ii.txt
-rw-r--r--  1 root root    884 Feb 20 05:32 inittab
-rw-rw-r--  1  517  518      0 Feb 19 08:07 kd.txt
-rw-rw-r--  1  517  518      0 Feb 19 08:07 oo.doc
[root@station103 ~]# find /tmp -nogroup
/tmp/kd.txt
/tmp/ii.txt
/tmp/oo.doc

  • 1
  • 2
  • 下一頁

聯繫我們

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