志在指尖
用双手敲打未来

linux find(linux find -name 模糊查询)

linuxfind

find是Linux中强大的查找指令,不仅能够依照文件名查找文件,还能够依照权限、巨细、时刻、inode号等来查找文件。可是find指令是直接在硬盘中进行查找的,假如指定的查找规模过大,find指令就会耗费较大的体系资源,导致服务器压力过大。所以,在运用find指令查找时,不要指定过大的查找规模。
find指令的基本信息如下:
指令名称:find。
英文原意:searchforfilesinadirectoryhierarchy.
所在途径:/bin/find。
执行权限:一切用户。
功能描述:在目录中查找文件。
指令格局
[root@localhost~]#find查找途径[选项]查找内容
find是比较特别的指令,它有两个参数:
第一个参数用来指定查找途径;
第二个参数用来指定查找内容。
而且find指令的选项比较复杂,咱们一个一个举例来看。
依照文件名查找
[root@localhost~]#find查找途径[选项]查找内容
选项:
-name:依照文件名查找;
-iname:依照文件名查找,不区别文件名巨细;
-inum:依照inode号查找;
这是find最常用的用法,咱们来试试:
[root@localhost~]#find/-nameyum.conf
/etc/yum.conf
#在目录下査找文件名是yum.conf的文件
可是find指令有一个小特性,便是查找的文件名必须和你的查找内容共同才能找到。假如只包括查找内容,则不会找到。咱们做一个试验:
[root@localhost~]#touchyum.conf.bak
#在/root/目录下树立一个文件yum.conf.bak
[root@localhost~]#find/-nameyum.conf
/etc/yum.conf
#查找只能找到yum.conf文件,而不能找到yum.conf.bak文件
find能够找到的是只有和查找内容yum.conf共同的/etc/yum.conf文件,而/root/yum.conf.bak文件尽管含有查找关键字,可是不会被找到。这种特性咱们总结为:find指令是彻底匹配的,必须和查找关键字如出一辙才会列出。
Linux中的文件名是区别巨细写的,也便是说,查找小写文件,是找不到大写文件的。假如想要巨细通吃,就要运用-iname来查找文件。
[root@localhost~]#touchCANGLS
[root@localhost~]#touchcangls
#树立大写和小写文件
[root@localhost~]#find.-inamecangls
./CANGLS
./cangls
#运用-iname,巨细写文件通吃
每个文件都有inode号,假如咱们知道inode号,则也能够依照inode号来查找文件。
[root@localhost~]#ls-iinstall.log
262147install.log
#假如知道文件名,则能够用”ls-i”来査找inode号
[root@localhost~]#find.-inum262147
./install.log
#假如知道inode号,则能够用find指令来査找文件
依照inode号查找文件,也是区别硬链接文件的重要手段,由于硬链接文件的inode号是共同的。
[root@localhost~]#ln/root/install.log/tmp/
#给install.log文件创建一个硬链接文件
[root@localhost~]#ll-i/root/install.log/tmp/install.log
262147-rw-r–r–.2rootroot247721月142014/root/
install.log
262147-rw-r–r–.2rootroot247721月142014/tmp/
install.log
#能够看到这两个硬链接文件的inode号是共同的
[root@localhost~]#find/-inum262147
/root/install.log
/tmp/install.log
#假如硬链接不是咱们自己树立的,则能够通过find指令查找inode号,来确定硬链接文件
依照文件巨细查找
[root@localhost~]#find查找途径[选项]查找内容
选项:
-size[+-]巨细:依照指定巨细查找文件
这儿的”+”的意思是查找比指定巨细还要大的文件,”-“的意思是查找比指定巨细还要小的文件。咱们来试试:
[root@localhost~]#ll-hinstall.log
-rw-r–r–.1rootroot25K1月142014install.log#在当前目录下有一个巨细是25KB的文件
[root@localhost~]#
[root@localhost~]#find.-size25k
./install.log
#当前目录下,査找巨细刚好是25KB的文件,能够找到
[root@localhost~]#find.-size-25klinux find
.
./.bashrc
./.viminfo
./.tcshrc
./.pearrc
./anaconda-ks.cfg
./test2
./.ssh
./.bash_history
./.lesshst
./.bash_profile
./yum.conf.bak
./.bashjogout
./install.log.syslog
./.cshrc
./cangls
#查找小于25KB的文件,能够找到很多文件
[root@localhost~]#find.-size+25k
#而当前目录下没有大于25KB的文件
其实find指令的-size选项是笔者个人觉得比较恶心的选项,为什么这样说?find指令能够依照KB来查找,应该也能够依照MB来查找吧。
[root@localhost~]#find.-size-25m
find:无效的-size类型”m”
#为什么会报错呢?其实是由于假如依照MB来查找,则必须是大写的M
这便是纠结点,千字节必须是小写的”k”,而兆字节必领是大写的”M”。有些人会说:”你别那么执着啊,你就不能不写单位,直接依照字节查找啊?”很傻,很天真,不写单位,你们就以为会依照字节查找吗?咱们来试试:
[root@localhost~]#llanaconda-ks.cfg
-rw——-.1rootroot12071月142014anaconda-ks.cfg
#anaconda-ks.cfg文件有1207字芳
[root@localhost~]#find.-size1207
#但用find查找1207,是什么也找不到的
也便是说,find指令的默许单位不是字节。假如不写单位,那么find指令是依照512Byte来进行査找的。咱们看看find指令的帮助。
[root@localhost~]#manfind
-sizen[cwbkMG]
Fileusesnunitsofspace.Thefollowingsuffixescanbeused:
‘b’for512-byteblocks(thisisthedefaultifnosuffixisused)
#这是默许单位,假如单位为b或不写单位,则依照512Byte查找
‘c’forbytes
#查找单位是c,依照字节查找
‘w’fortwo-bytewords
#查找单位是w,依照双字节(中文)查找
‘k’forKilobytes(unitsof1024bytes)
#依照KB单位查找,必须是小写的k
‘M’forMegabytes(unitsof1048576bytes)
#依照MB单位查找,必须是大写的M
‘G’forGigabytes(unitsof1073741824bytes)
#依照GB单位查找,必须是大写的G
也便是说,假如想要依照字节查找,则需要加查找单位”c”。咱们来试试:
[root@localhost~]#find.-size1207c
./anaconda-ks.cfg
#运用查找单位c,才会依照字节查找
依照修正时刻查找
Linux中的文件有访问时刻(atime)、数据修正时刻(mtime)、状况修正时刻(ctime)这三个时刻,咱们也能够依照时刻来查找文件。
[root@localhost~]#find查找途径[选项]查找内容
选项:
-atime[+-]时刻:依照文件访问时刻查找
-mtime[+-]时刻:依照文改时刻查找
-ctime[+-]时刻:依照文件修正时刻查找
这三个时刻的区别咱们在stat指令中已经解释过了,这儿用mtime数据修正时刻来举例,要点说说”[+-]”时刻的意义。
-5:代表@内修正的文件。
5:代表前5~6天那一天修正的文件。
+5:代表6天前修正的文件。
咱们画一个时刻轴,来解释一下,如图1所示。
图1find时刻轴
每次笔者讲到这儿,”-5″代表5天内修正的文件,而”+5″总有人说代表5天修正的文件。要是笔者能知道5天体系中能树立什么文件,早就去买彩票了,那是未卜先知啊!所以”-5″指的是5天内修正的文件,”5″指的是前5~6天那一天修正的文件,”+5″指的是6天前修正的文件。咱们来试试:
[root@localhost~]#find.-mtime-5
#查找5天内修正的文件
我们能够在体系中把几个选项都试试,就能够理解各选项之间的差别了。
find不仅能够依照atmie、mtime、ctime来査找文件的时刻,也能够依照amin、mmin和cmin来査找文件的时刻,区别只是一切time选项的默许单位是天,而min选项的默许单位是分钟。
依照权限查找
在find中,也能够依照文件的权限来进行查找。权限也支撑[+/-]选项。咱们先看一下指令格局。
[root@localhost~]#find查找途径[选项]查找内容
选项:
-perm权限形式:査找文件权限刚好等于”权限形式”的文件
-perm-权限形式:査找文件权限全部包括”权限形式”的文件
-perm+权限形式:査找文件权限包括”权限形式”的恣意一个权限的文件
为了便于理解,咱们要举几个比如。先树立几个测验文件。
[root@localhost~]#mkdirtest
[root@localhost~]#cdtest/
[root@localhosttest]#touchtestl
[root@localhosttest]#touchtest2
[root@localhosttest]#touchtest3
[root@localhosttest]#touchtest4
#树立测验目录,以及测验文件
[root@localhosttest]#chmod755testl
[root@localhosttest]#chmod444test2
[root@localhosttest]#chmod600test3
[root@localhosttest]#chmod200test4
#设定试验权限。由于是试验权限,所以看起来比较别扭
[root@localhosttest]#ll
总用量0
-rwxr-xr-x1rootroot06月1711:05testl-r–r–r–1rootroot06月1711:05test2
-rw——-1rootroot06月1711:05test3
-w——-1rootroot06月1711:05test4
#查看权限

linuxfind-name模糊查询

说道查找指令,第一个想到的就是find,用法多样,加上-exec,你能够玩的很高兴。小缺陷是需要遍历目录下一切,查找会比较慢。例如遍历/下查找,尤其是体系中文件十分多时,你能够先去听首歌,再来看成果(当然了,这有点夸大!)。
一、语法格局
1、基本例子
[root@salt-mastertest03]#find/etc/-namenetwork
/etc/sysconfig/network
/etc/vmware-tools/scripts/vmware/network
/etc/rc.d/init.d/network
一般情况下,查找范围越大,目录层级越深,查找速度就越慢。
[root@salt-mastertest03]#timefind/-nameifcfg-eth0
real0m0.850s
user0m0.239s
sys0m0.598s
注意:real远大于user加上sys,因为find需要遍历各个目录,需要很多的I/O操作,而磁盘I/O通常是最慢的环节,因此大部分时刻find进程都在等待磁盘I/O完结。
2、按条件查找
1、依照文件名查找
-name:按姓名查找
-iname:忽略大小写
[root@salt-mastertest03]#find/etc-namenetworkmanager
[root@salt-mastertest03]#find/etc-inamenetworkmanager
/etc/NetworkManager
2、依照文件类型查找-type
f:一般文件
d:目录
l:软衔接
……
[root@salt-mastertest03]#ll
total4
drwxr-xr-x2rootroot4096Dec1315:4701
-rw-r–r–1rootroot0Dec1315:4702
[root@salt-mastertest03]#find.-typef
./02
[root@salt-mastertest03]#find.-typed
.
./01
3、依照时刻查找
[root@salt-mastertest03]#stat文件名能够查看文件的三个时刻
atime:访问时刻,catmoreless……
mtime:文件的内容发生变化的时刻,vim……(也是ll所显示的时刻)
ctime:文件的属性发生变化的时刻,比如:权限改动、大小改动、一切者、所属组等改动
-atimen以天为单位
-aminn以分钟为单位
-mtimen以天为单位
n为数字,前面能够带+或许-号
+n:n+1天之前
n:n到n+1天之间
-n:n天以内
1.查找/find下修正时刻在24小时(1天)之内的文件和目录
[root@salt-mastertest03]#find.-mtime-1
./01
./02
2.查找/find下修正时刻在2天前的一般文件
[root@salt-mastertest03]#ll
total8
drwxr-xr-x2rootroot4096Dec1315:4701
-rw-r–r–1rootroot0Dec1315:4702
-rw-r–r–1rootroot193Apr12201911.txt
[root@salt-mastertest03]#find.-typef-mtime+1
./11.txt
4、依照用户和组查找
-user用户名
-group组名
-uiduid
-gidgid
-nouser:孤儿文件,没有一切者的文件
-nogroup:没有所属组的文件
1.查找体系中一切者是finduser的文件
[root@salt-mastertest03]#useraddfinduser
[root@salt-mastertest03]#find/-userfinduser-typef
/var/spool/mail/finduser
/home/finduser/.bash_logout
/home/finduser/.bashrc
/home/finduser/.bash_profile
2.查找体系中的孤儿文件
[root@salt-mastertest03]#userdelfinduser
[root@salt-mastertest03]#find/home/-typef-nouser
/home/finduser/.bash_logout
/home/finduser/.bashrc
/home/finduser/.bash_profile
6、依照文件大小查找-size
+大于
-小于
直接数字等于
‘b’for512-byteblocks(thisisthedefaultifnosuffixisused)
‘c’forbytes
‘w’fortwo-bytewords
‘k’forKilobytes(unitsof1024bytes)
‘M’forMegabytes(unitsof1048576bytes)
‘G’forGigabytes(unitsof1073741824bytes)
[root@salt-mastertest03]#ll-h
total5.1M
drwxr-xr-x2rootroot4.0KDec1315:4701
-rw-r–r–1rootroot0Dec1315:4702
-rw-r–r–1rootroot5.0MDec1316:4004
-rw-r–r–1rootroot193Apr12201911.txt
[root@salt-mastertest03]#find.-typef-size3M
[root@salt-mastertest03]#find.-typef-size-3M
./02
./11.txt
[root@salt-mastertest03]#find.-typef-size3M
[root@salt-mastertest03]#find.-typef-size+3M
./04
3、动作
-exec动作–找到成果之后直接履行动作
-ok动作–履行动作之前先提示,即需要交互
[root@salt-mastertest03]#find.-typef-size+3M
./04
[root@salt-mastertest03]#find.-typef-size+3M-execls-l{}\;
-rw-r–r–1rootroot5242880Dec1316:40./04
{}——用来代替找到的成果
\;——表明完毕标志
[root@salt-mastertest03]#find.-typef-size+3M-okls-l{}\;
<ls…./04>?y
-rw-r–r–1rootroot5242880Dec1316:40./04
[root@salt-mastertest03]#find.-typef-size+3M-okls-l{}\;
<ls…./04>?n
例如,有时候只想修正当时目录下一切文件权限为644,但不想修正目录权限,能够如下操作
find./*-typef-execchmod644{}\;
了解了-exec后,find和其他指令的组合起来使用,会使它的功能变得会十分强大和方便。

未经允许不得转载:IT技术网站 » linux find(linux find -name 模糊查询)
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

志在指尖 用双手敲打未来

登录/注册IT技术大全

热门IT技术

C#基础入门   SQL server数据库   系统SEO学习教程   WordPress小技巧   WordPress插件   脚本与源码下载