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时间轴

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

本文地址:https://www.hosteonscn.com/3152.html

评论

0条评论

发表评论

邮箱地址不会被公开。 必填项已用*标注