bash之find

find命令用於在指定的目錄下,通過指定的查找條件,查找文件,並且能夠對查找到的文件執行相應的處理或者與管道命令結合使用。

find命令格式:

find [options] [-D debugopts] [-Olevel] [path] [expression]

[options]:

      -P: 不查找符號鏈接文件,缺省選項

      -L:查找符號鏈接文件

      -H:不查找符號鏈接,如果查找到的文件不作爲expression的參數

-D debugopts:

       用於打印診斷信息,用於診斷find查不到你想到的結果

       debugopts:

tree:   Show the expression tree in its original and optimised form.

              stat:   Print  messages  as  files  are  examined with the stat and lstat system calls.  The find program
                     tries to minimise such calls.

              opt:    Prints diagnostic information relating to the optimisation of the expression  tree;  see  the  -O
                     option.

              rates:  Prints a summary indicating how often each predicate succeeded or failed.

 

-Olevel:

開啓查詢最優化

level的值爲0 , 1 , 2 , 3

[path]:

         用於指定查找目錄,如果不指定,默認爲當前目錄

[expression]:

        -amin [+|-]n:  文件被訪問的時間(分鐘)

                 +n: n分鐘以前

                 -n:  n分鐘以內

                  n:   n分鐘時

        -cmin [+|-]n: 文件權限修改時間(分鐘)

        -mmin [+|-]n: 文件修改時間(分鐘)

        -atime [+|-]n: 文件被訪問的時間(天數)

        -ctime [+|-]n: 文件權限修改的時間(天數)

                -mtime [+|-]n: 文件被修改的時間(天數)

        -fstype type: 文件所在的文件系統類型

        -user: 文件的屬主

        -nouser: 無屬主的文件

        -uid: 文件的uid

        -group:  文件的屬組

        -nogroup: 無屬組的文件

        -gid: 文件的gid

        -name pattern:  文件名

                  pattern:  文件名通配符

                          *: 匹配任意字符

                          ?: 匹配單個字符

                          []: 匹配指定範圍內的任意單個字符

                          [^]: 匹配指定範圍外的任意單個字符

         -iname pattern: 文件名,忽略大小寫

         -lname pattern:  符號鏈接文件指向的文件

         -ilname pattern: 同-lname,忽略大小寫

         -inum: 指定文件的inode號

         -path pattern: 指定要查找的文件路徑

         -newer file: 比file修改時間更近

         -perm [+|-]mode: 指定文件權限

                  mode: 精確匹配指定的權限

                  +mode:任意一類用戶(u,g,o)的任何一位權限(r,w,x)匹配

                  -mode: 每類用戶的指定要檢查的權限位(r,w,x)都匹配

          -size n[cwbkMG]: 文件大小

                c: 字節

                b: for 512-byte bloc

                w: two-byte words

                k: kilobytes

                M: Megabytes

                G: Gigabytes

 

       -type file_type: 文件類型

                    b:    塊設備

                    c:     字符設備

                    d:     目錄

                    p:     管道文件

                    f:      普通文件

                    l:       鏈接文件

                    s:      socket文件

                   

          -delete : 刪除查找到的文件

          -exec COMMAND: 對查找到的文件執行命令

          -ok COMMAND: 執行命令之前提示

          -fprint : 列出查到的文件到標準輸出

使用舉例:

[root@ccdb ccdb]# date
Wed Aug 27 12:47:57 CST 2014

[root@ccdb ccdb]# ll -h /ccdb
total 54G
-rwxrwSrwx 1 root root 479M Aug  9 01:14 arc_CCDB_20140809_4hpffnkg_1_1_L0
-rwxrwSrwx 1 root root 481M Aug 10 00:03 arc_CCDB_20140810_4kpfi7qj_1_1_L1
-rwxrwSrwx 1 root root 487M Aug 11 00:03 arc_CCDB_20140811_4npfks6i_1_1_L1
-rwxrwSrwx 1 root root 502M Aug 12 00:03 arc_CCDB_20140812_4qpfngis_1_1_L1
-rwxrwSrwx 1 root root 466M Aug 13 00:03 arc_CCDB_20140813_4tpfq4uh_1_1_L1
-rwxrwSrwx 1 root root 531M Aug 14 00:03 arc_CCDB_20140814_50pfspah_1_1_L1
-rwxrwSrwx 1 root root 502M Aug 15 00:03 arc_CCDB_20140815_53pfvdmt_1_1_L1
-rwxrwSrwx 1 root root 547M Aug 16 01:15 arc_CCDB_20140816_56pg268q_1_1_L0
-rwxrwSrwx 1 root root 475M Aug 17 00:03 arc_CCDB_20140817_59pg4mei_1_1_L1
-rwxrwSrwx 1 root root 465M Aug 18 00:03 arc_CCDB_20140818_5cpg7aqh_1_1_L1
-rwxrwSrwx 1 root root 607M Aug 19 00:04 arc_CCDB_20140819_5fpg9v8g_1_1_L1
-rwxrwSrwx 1 root root 562M Aug 20 00:03 arc_CCDB_20140820_5ipgcjij_1_1_L1
-rwxrwSrwx 1 root root 486M Aug 21 00:03 arc_CCDB_20140821_5lpgf7uj_1_1_L1
-rwxrwSrwx 1 root root 571M Aug 22 00:04 arc_CCDB_20140822_5opghsce_1_1_L1
-rwxrwSrwx 1 root root 621M Aug 23 01:15 arc_CCDB_20140823_5rpgkks8_1_1_L0
-rwxrwSrwx 1 root root 457M Aug 24 00:03 arc_CCDB_20140824_5upgn52h_1_1_L1
-rwxrwSrwx 1 root root 484M Aug 25 00:03 arc_CCDB_20140825_61pgppei_1_1_L1
-rwxrwSrwx 1 root root 499M Aug 26 00:03 arc_CCDB_20140826_64pgsdqs_1_1_L1
-rwxrwSrwx 1 root root 513M Aug 27 00:03 arc_CCDB_20140827_67pgv26t_1_1_L1
-rwxrwSrwx 1 root root 7.7M Aug 14 00:03 c-3388356195-20140814-00.ctl
-rwxrwSrwx 1 root root 7.7M Aug 15 00:03 c-3388356195-20140815-00.ctl
-rwxrwSrwx 1 root root 7.7M Aug 16 01:15 c-3388356195-20140816-00.ctl
-rwxrwSrwx 1 root root 7.7M Aug 17 00:03 c-3388356195-20140817-00.ctl
-rwxrwSrwx 1 root root 7.7M Aug 18 00:03 c-3388356195-20140818-00.ctl
-rwxrwSrwx 1 root root 7.7M Aug 19 00:05 c-3388356195-20140819-00.ctl
-rwxrwSrwx 1 root root 7.7M Aug 20 00:03 c-3388356195-20140820-00.ctl
-rwxrwSrwx 1 root root 7.7M Aug 21 00:03 c-3388356195-20140821-00.ctl
-rwxrwSrwx 1 root root 7.7M Aug 22 00:04 c-3388356195-20140822-00.ctl
-rwxrwSrwx 1 root root 7.7M Aug 23 01:15 c-3388356195-20140823-00.ctl
-rwxrwSrwx 1 root root 7.7M Aug 24 00:03 c-3388356195-20140824-00.ctl
-rwxrwSrwx 1 root root 7.7M Aug 25 00:03 c-3388356195-20140825-00.ctl
-rwxrwSrwx 1 root root 7.7M Aug 26 00:03 c-3388356195-20140826-00.ctl
-rwxrwSrwx 1 root root 7.7M Aug 27 00:04 c-3388356195-20140827-00.ctl
-rwxrwSrwx 1 root root  10G Aug  9 00:51 db_CCDB_20140809_4gpffjc4_1_1_L0
-rwxrwSrwx 1 root root 4.3G Aug  9 01:12 db_CCDB_20140809_4gpffjc4_2_1_L0
-rwxrwSrwx 1 root root  79M Aug 10 00:01 db_CCDB_20140810_4jpfi7o5_1_1_L1
-rwxrwSrwx 1 root root  85M Aug 11 00:01 db_CCDB_20140811_4mpfks44_1_1_L1
-rwxrwSrwx 1 root root  79M Aug 12 00:01 db_CCDB_20140812_4ppfngg4_1_1_L1
-rwxrwSrwx 1 root root  79M Aug 13 00:01 db_CCDB_20140813_4spfq4s4_1_1_L1
-rwxrwSrwx 1 root root  91M Aug 14 00:01 db_CCDB_20140814_4vpfsp84_1_1_L1
-rwxrwSrwx 1 root root 102M Aug 15 00:01 db_CCDB_20140815_52pfvdk4_1_1_L1
-rwxrwSrwx 1 root root  10G Aug 16 00:51 db_CCDB_20140816_55pg2204_1_1_L0
-rwxrwSrwx 1 root root 4.3G Aug 16 01:12 db_CCDB_20140816_55pg2204_2_1_L0
-rwxrwSrwx 1 root root  79M Aug 17 00:01 db_CCDB_20140817_58pg4mc4_1_1_L1
-rwxrwSrwx 1 root root  77M Aug 18 00:01 db_CCDB_20140818_5bpg7ao4_1_1_L1
-rwxrwSrwx 1 root root 235M Aug 19 00:02 db_CCDB_20140819_5epg9v44_1_1_L1
-rwxrwSrwx 1 root root  89M Aug 20 00:01 db_CCDB_20140820_5hpgcjg4_1_1_L1
-rwxrwSrwx 1 root root  83M Aug 21 00:01 db_CCDB_20140821_5kpgf7s5_1_1_L1
-rwxrwSrwx 1 root root 211M Aug 22 00:02 db_CCDB_20140822_5npghs84_1_1_L1
-rwxrwSrwx 1 root root  10G Aug 23 00:51 db_CCDB_20140823_5qpgkgk6_1_1_L0
-rwxrwSrwx 1 root root 4.3G Aug 23 01:12 db_CCDB_20140823_5qpgkgk6_2_1_L0
-rwxrwSrwx 1 root root  79M Aug 24 00:01 db_CCDB_20140824_5tpgn504_1_1_L1
-rwxrwSrwx 1 root root  81M Aug 25 00:01 db_CCDB_20140825_60pgppc4_1_1_L1
-rwxrwSrwx 1 root root  98M Aug 26 00:01 db_CCDB_20140826_63pgsdo4_1_1_L1
-rwxrwSrwx 1 root root  96M Aug 27 00:01 db_CCDB_20140827_66pgv244_1_1_L1

 

1.查找17天以前沒有修改過的文件

[root@ccdb ccdb]# find /ccdb -mtime +17
/ccdb/arc_CCDB_20140809_4hpffnkg_1_1_L0
/ccdb/db_CCDB_20140809_4gpffjc4_1_1_L0
/ccdb/db_CCDB_20140809_4gpffjc4_2_1_L0

2.查找17天沒有修改過的文件

[root@ccdb ccdb]# find /ccdb -mtime 17
/ccdb/arc_CCDB_20140810_4kpfi7qj_1_1_L1
/ccdb/db_CCDB_20140810_4jpfi7o5_1_1_L1

3.查找3天內沒有修改過的文件

[root@ccdb ccdb]# find /ccdb/* -mtime -3
/ccdb/arc_CCDB_20140825_61pgppei_1_1_L1
/ccdb/arc_CCDB_20140826_64pgsdqs_1_1_L1
/ccdb/arc_CCDB_20140827_67pgv26t_1_1_L1
/ccdb/c-3388356195-20140825-00.ctl
/ccdb/c-3388356195-20140826-00.ctl
/ccdb/c-3388356195-20140827-00.ctl
/ccdb/db_CCDB_20140825_60pgppc4_1_1_L1
/ccdb/db_CCDB_20140826_63pgsdo4_1_1_L1
/ccdb/db_CCDB_20140827_66pgv244_1_1_L1

4. 查找大於4G的文件

[root@ccdb ccdb]# find /ccdb/* -size +4G -exec ls -lh {} \;
-rwxrwSrwx 1 root root 10G Aug  9 00:51 /ccdb/db_CCDB_20140809_4gpffjc4_1_1_L0
-rwxrwSrwx 1 root root 4.3G Aug  9 01:12 /ccdb/db_CCDB_20140809_4gpffjc4_2_1_L0
-rwxrwSrwx 1 root root 10G Aug 16 00:51 /ccdb/db_CCDB_20140816_55pg2204_1_1_L0
-rwxrwSrwx 1 root root 4.3G Aug 16 01:12 /ccdb/db_CCDB_20140816_55pg2204_2_1_L0
-rwxrwSrwx 1 root root 10G Aug 23 00:51 /ccdb/db_CCDB_20140823_5qpgkgk6_1_1_L0
-rwxrwSrwx 1 root root 4.3G Aug 23 01:12 /ccdb/db_CCDB_20140823_5qpgkgk6_2_1_L0

 

5.查找普通文件

[root@ccdb ccdb]# find /root/ -type f
/root/.bash_profile
/root/.bash_logout
/root/install.log
/root/.cshrc
/root/nohup.out
/root/.bash_history
/root/.lesshst
/root/.bashrc
/root/.tcshrc
/root/install.log.syslog
/root/anaconda-ks.cfg

6.查找/etc/sysconfig/中以config結尾的文件

[root@unp sysconfig]# find /etc/sysconfig/ -iname "*config"
/etc/sysconfig/
/etc/sysconfig/ip6tables-config
/etc/sysconfig/authconfig
/etc/sysconfig/iptables-config

 

7.查找/tmp/foo/目錄下以install開頭的文件,並刪除

root@unp foo]# find /tmp/foo/ -iname "install*" -exec  rm -rf {} \; 

[root@unp foo]# find /tmp/foo/ -iname "install*" | xargs rm –rf

 

8.查找/tmp/foo/目錄下以install開頭的文件之外的其它文件,並增加後綴名.txt

[root@unp foo]# find /tmp/foo/*  ! -iname "install*" -exec mv {} {}.txt \;
[root@unp foo]# ll
total 0
-rw-r--r-- 1 root root 0 Aug 27 13:06 aa.txt
-rw-r--r-- 1 root root 0 Aug 27 13:06 bb.txt
-rw-r--r-- 1 root root 0 Aug 27 13:06 cc.txt
-rw-r--r-- 1 root root 0 Aug 27 13:06 dd.txt
-rw-r--r-- 1 root root 0 Aug 27 13:06 install
-rw-r--r-- 1 root root 0 Aug 27 13:07 install.bz2
-rw-r--r-- 1 root root 0 Aug 27 13:07 install.gz
-rw-r--r-- 1 root root 0 Aug 27 13:07 install.xz

 

9.查找/tmp/foo/目錄下nouser的文件

[root@unp foo]# ll
total 0
-rw-r--r-- 1 4008 4008 0 Aug 27 13:06 aa.txt
-rw-r--r-- 1 doc  doc  0 Aug 27 13:06 bb.txt
-rw-r--r-- 1 root root 0 Aug 27 13:06 cc.txt
-rw-r--r-- 1 root root 0 Aug 27 13:06 dd.txt
-rw-r--r-- 1 4008 4008 0 Aug 27 13:06 install
-rw-r--r-- 1 doc  doc  0 Aug 27 13:07 install.bz2
-rw-r--r-- 1 root root 0 Aug 27 13:07 install.gz
-rw-r--r-- 1 root root 0 Aug 27 13:07 install.xz
[root@unp foo]# find /tmp/foo/ -nouser
/tmp/foo/install
/tmp/foo/aa.txt

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章