Welcome 微信登录

首页 / 操作系统 / Linux / 文本查找利器find的使用

一、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.conf1234567 [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.conf2、-iname
 文件名不区分大小写查找[root@station103 tmp]# find -iname "*.txt"    不区分大小写查找
./KKG.txt
./aa.txt
./nba.txt
./QW.txt
./hao.txt
./abc.txt
./dW.txt
./hddW.txt3、-uid
 根据uid查找[root@station103 tmp]# find -uid 517
./kd.txt
./ii.txt
./oo.doc4、-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
  • 下一页
Fedora安装之rpmfusion:获取 GPG 密钥失败CentOS软件包管理之源码安装相关资讯      Find 
  • Linxu命令与文件的搜索 - which,  (08/03/2015 09:30:37)
  • Linux下查找文件find命令  (10/28/2014 20:11:29)
  • 功能强大的find命令  (01/15/2014 15:03:36)
  • Linux基础教程:find 与 xargs  (04/05/2015 10:20:11)
  • find、xargs命令使用及方法详解  (03/07/2014 17:49:41)
  • Linux中强大且常用命令:find、  (07/24/2013 20:39:49)
本文评论 查看全部评论 (0)
表情: 姓名: 字数