Linux find命令:在目录中查找文件(超详解) 您所在的位置:网站首页 mes是什么文件 Linux find命令:在目录中查找文件(超详解)

Linux find命令:在目录中查找文件(超详解)

#Linux find命令:在目录中查找文件(超详解)| 来源: 网络整理| 查看: 265

首页 教程 VIP会员 辅导班 嵌入式学习路线 首页 C语言教程 C++教程 Python教程 Java教程 Linux入门 更多>> 首页 > 编程笔记 Linux find命令:在目录中查找文件(超详解) find 是 Linux 中强大的搜索命令,不仅可以按照文件名搜索文件,还可以按照权限、大小、时间、inode 号等来搜索文件。但是 find 命令是直接在硬盘中进行搜索的,如果指定的搜索范围过大,find命令就会消耗较大的系统资源,导致服务器压力过大。所以,在使用 find 命令搜索时,不要指定过大的搜索范围。 find 命令的基本信息如下: 命令名称:find。 英文原意:search for files in a directory hierarchy. 所在路径:/bin/find。 执行权限:所有用户。 功能描述:在目录中查找文件。 命令格式

[root@localhost ~]# find 搜索路径 [选项] 搜索内容

find 是比较特殊的命令,它有两个参数: 第一个参数用来指定搜索路径; 第二个参数用来指定搜索内容。 而且find命令的选项比较复杂,我们一个一个举例来看。 按照文件名搜索

[root@localhost ~]#find 搜索路径 [选项] 搜索内容

选项: -name: 按照文件名搜索; -iname: 按照文件名搜索,不区分文件名大小; -inum: 按照 inode 号搜索; 这是 find 最常用的用法,我们来试试:

[root@localhost ~]# find /-name yum.conf /etc/yum.conf #在目录下査找文件名是yum.conf的文件

但是 find 命令有一个小特性,就是搜索的文件名必须和你的搜索内容一致才能找到。如果只包含搜索内容,则不会找到。我们做一个实验:

[root@localhost ~]# touch yum.conf.bak #在/root/目录下建立一个文件yum.conf.bak [root@localhost ~]# find /-name yum.conf /etc/yum.conf #搜索只能找到yum.conf文件,而不能找到 yum.conf.bak 文件

find 能够找到的是只有和搜索内容 yum.conf 一致的 /etc/yum.conf 文件,而 /root/yum.conf.bak 文件虽然含有搜索关键字,但是不会被找到。这种特性我们总结为:find 命令是完全匹配的,必须和搜索关键字一模一样才会列出。 Linux 中的文件名是区分大小写的,也就是说,搜索小写文件,是找不到大写文件的。如果想要大小通吃,就要使用 -iname 来搜索文件。

[root@localhost ~]# touch CANGLS [root@localhost ~]# touch cangls #建立大写和小写文件 [root@localhost ~]#find.-iname cangls ./CANGLS ./cangls #使用-iname,大小写文件通吃

每个文件都有 inode 号,如果我们知道 inode 号,则也可以按照 inode 号来搜索文件。

[root@localhost ~]#ls -i install.log 262147 install.log #如果知道文件名,则可以用"ls -i"来査找inode号 [root@localhost ~]# find.-inum 262147 ./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--.2 root root 24772 1 月 14 2014/root/ install.log 262147 -rw-r--r--.2 root root 24772 1 月 14 2014/tmp/ install.log #可以看到这两个硬链接文件的inode号是一致的 [root@localhost ~]# find /-inum 262147 /root/install.log /tmp/install.log #如果硬链接不是我们自己建立的,则可以通过find命令搜索inode号,来确定硬链接文件

按照文件大小搜索

[root@localhost ~]#find 搜索路径 [选项] 搜索内容

选项: -size[+-]大小:按照指定大小搜索文件 这里的"+"的意思是搜索比指定大小还要大的文件,"-" 的意思是搜索比指定大小还要小的文件。我们来试试:

[root@localhost ~]# ll -h install.log -rw-r--r--.1 root root 25K 1月 14 2014 install.log #在当前目录下有一个大小是25KB的文件 [root@localhost ~]# [root@localhost ~]# find.-size 25k ./install.log #当前目录下,査找大小刚好是25KB的文件,可以找到 [root@localhost ~]# find .-size -25k . ./.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 ~]# ll anaconda-ks.cfg -rw-------.1 root root 1207 1 月 14 2014 anaconda-ks.cfg #anaconda-ks.cfg文件有1207字芳 [root@localhost ~]# find.-size 1207 #但用find查找1207,是什么也找不到的

也就是说,find 命令的默认单位不是字节。如果不写单位,那么 find 命令是按照 512 Byte 来进行査找的。 我们看看 find 命令的帮助。

[root@localhost ~]# man find -size n[cwbkMG] File uses n units of space. The following suffixes can be used: 'b' for 512-byte blocks (this is the default if no suffix is used) #这是默认单位,如果单位为b或不写单位,则按照 512Byte搜索 'c' for bytes #搜索单位是c,按照字节搜索 'w' for two-byte words #搜索单位是w,按照双字节(中文)搜索 'k'for Kilobytes (units of 1024 bytes) #按照KB单位搜索,必须是小写的k 'M' for Megabytes (units of 1048576 bytes) #按照MB单位搜索,必须是大写的M 'G' for Gigabytes (units of 1073741824 bytes) #按照GB单位搜索,必须是大写的G

也就是说,如果想要按照字节搜索,则需要加搜索单位"c"。我们来试试:

[root@localhost ~]# find.-size 1207c ./anaconda-ks.cfg #使用搜索单位c,才会按照字节搜索

按照修改时间搜索 Linux 中的文件有访问时间(atime)、数据修改时间(mtime)、状态修改时间(ctime)这三个时间,我们也可以按照时间来搜索文件。

[root@localhost ~]# find搜索路径 [选项] 搜索内容

选项: -atime [+-]时间: 按照文件访问时间搜索 -mtime [+-]时间: 按照文改时间搜索 -ctime [+-]时间: 按照文件修改时间搜索 这三个时间的区别我们在 stat 命令中已经解释过了,这里用 mtime 数据修改时间来举例,重点说说 "[+-]"时间的含义。 -5:代表@内修改的文件。 5:代表前5~6天那一天修改的文件。 +5:代表6天前修改的文件。 我们画一个时间轴,来解释一下,如图 1 所示。 图 1 find时间轴 每次笔者讲到这里,"-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 ~]# mkdir test [root@localhost ~]# cd test/ [root@localhost test]# touch testl [root@localhost test]# touch test2 [root@localhost test]# touch test3 [root@localhost test]# touch test4 #建立测试目录,以及测试文件 [root@localhost test]# chmod 755 testl [root@localhost test]# chmod 444 test2 [root@localhost test]# chmod 600 test3 [root@localhost test]# chmod 200 test4 #设定实验权限。因为是实验权限,所以看起来比较别扭 [root@localhost test]# ll 总用量0 -rwxr-xr-x 1 root root 0 6月 17 11:05 testl -r--r--r-- 1 root root 0 6月 17 11:05 test2 -rw------- 1 root root 0 6月 17 11:05 test3 -w------- 1 root root 0 6月 17 11:05 test4 #查看权限

【例 1】"-perm权限模式"。 这种搜索比较简单,代表査找的权限必须和指定的权限模式一模一样,才可以找到。

[root@localhost test]#find.-perm 444 ./test2 [root@localhost test]#find.-perm 200 ./test4 #按照指定权限搜索文件,文件的权限必须和搜索指定的权限一致,才能找到

【例 2】"-perm-权限模式"。 如果使用"-权限模式",是代表的是文件的权限必须全部包含搜索命令指定的权限模式,才可以找到。

[root@localhost test]#find .-perm -200 ./test4



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有