码迷,mamicode.com
首页 > 系统相关 > 详细

linux-find查找

时间:2018-08-26 18:36:25      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:时间   配置文件   access   通配符   --   hello   cti   数据   etc   

导语:
which
whereis
locate
find

查找
which 只能查询命令
#which rpm

whereis
可以查询命令和配置文件的位置
#whereis rpm
#whereis passwd

whatis
#whatis rpm 和下面命令一样的效果,查询rpm命令都在哪章man有解释
#man -f rpm

locate
维护着一个查询数据库
#vim /etc/updatedb.conf
1)文件系统类型
2)目录
如果被更改之后,需要更新数据库
#updatedb 手动更新数据库

#locate 被查找的关键字
#locate *.txt
*是通配符 

find
#find 路径 条件 跟条件相关的操作符 [-exec|-ok 动作]
路径
默认不写路径时查找的是当前路径
例:
/etc
./
/
/var/ftp

条件
名称 大小 时间 文件类型 用户 组 权限 ...

-name 文件名称 按名称查找

find / -name a.txt

#find / -name a.t??
 find / -name a.tx?
#find / -name ‘*a.txt‘
 find / -iname ‘*a.txt‘

-iname 不区分大小写

?表示单个字符
*表示所有字符

一般情况下{}不能用
{1..100}
{abc,abd,efg}

通配符:

  • ? [] {}
  • 表示所有字符
    ? 表示任意单个字符
    [] 表示其中任意一个单个字符
    例:
    [abc]
    [a-z]
    [a-Z]
    [a-zA-Z]
    [!a-z] !取反
    [0-9]

    a到Z的匹配顺序是aAbBcC...
    [root@server python]# ls
    a.txt E.txt j.txt N.txt s.txt W.txt
    A.txt f.txt J.txt o.txt S.txt x.txt
    b.txt F.txt k.txt O.txt t.txt X.txt
    B.txt g.txt K.txt p.txt T.txt y.txt
    c.txt G.txt l.txt P.txt u.txt Y.txt
    C.txt h.txt L.txt q.txt U.txt z.txt
    d.txt H.txt m.txt Q.txt v.txt Z.txt
    D.txt i.txt M.txt r.txt V.txt
    e.txt I.txt n.txt R.txt w.txt

    {hello,hi,king,xiaoxuan}
    #touch {python,wing,haha}.txt
    {a..z}
    {1..100}
    /dev/vdc{1,2,3}

按大小查找
-size
#find / -size 50M
#find / -size +50M
#find / -size -50M
查找大于10M小于20M
#find / -size +10M -a -size -20M

-a可以换成-and
#find / -size -10M -o -size +20M

-o可以换成-or
find ./ ! -size -10M
find / -size -50M -a -name "wing"
find / ! ( -size -50M -a -name "wing" )
!取反

( )
\ 转义字符 把有意义的变的没意义 把没意义的变的有意义

附加:用dd命令做测试数据
#dd if=/dev/zero of=/tmp/aa.txt bs=5M count=2

按文件类型查找
-type
f
d
b
c
l
s
p
#find / -type c -exec ls -l {} \;

#find /tmp/ -name aa.txt -exec rm -i {} \;
find /tmp/ -name aa.txt -ok rm {} \;
< rm ... /tmp/aa.txt > ? y

-exec 对之前查找出来的文件做进一步操作
-ok 和-exec一样,只不过多了提示

按权限查找:
-perm
find ./ -perm 644 -ls
./dd.txt

按用户和组查找
-user
-group
#find ./ -user wing
./bb.txt
find ./ -group user3
./cc.txt

按时间
-atime access时间
-mtime modify时间
-ctime change时间
-amin
-mmin
-cmin

time表示单位是天
min 表示分钟
#stat 文件

查找两分钟内访问过的文件
#find /tmp -amin -2
/tmp/a.txt

查找两分钟前访问过的文件
find /tmp -amin +2

查找一个文件的硬链接:
ln a.txt heihei
#ll -i
439360 -rw-r--r-- 2 root root 12 Nov 29 22:22 a.txt
439360 -rw-r--r-- 2 root root 12 Nov 29 22:22 heihei
find . -samefile a.txt
./a.txt
./heihei

指定查找的目录深度:
-maxdepth levels
-mindepth levels
find / -maxdepth 3 -a -name "ifcfg-eth0"

按正则表达式查找:
find /etc -regex ‘.*ifcfg-ens[0-9][0-9]‘



-exec -ok
#find . -name wing.txt -exec cp {}  /root/Desktop/ \;

防止被查找到的文件过多,导致内存溢出错误
 find  .  -name wing.txt | xargs  -i cp {} /root/Desktop

linux-find查找

标签:时间   配置文件   access   通配符   --   hello   cti   数据   etc   

原文地址:http://blog.51cto.com/13939728/2164555

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!