Linux之find命令详解(查找文件内容)
Tags: findfindstrLinuxOS命令介绍查找文件内容
find常用命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | find / -type f -size +10000000c -exec du -sh {} \; # 查找大于10M的文件 find . -name '.phtml' -type f -mmin -30 # 查找当前目录下.phtml文件中,最近30分钟内修改过的文件。 find . -name '.phtml' -type f -mmin -30 -ls # 查找当前目录下.phtml文件中,最近30分钟内修改过的文件,的详细情况。 find . -type f -mtime -1 # 查找当前目录下,最近1天内修改过的常规文件 find . -type f -mtime +1 # 查找当前目录下,最近1天前(2天内)修改过的常规文件。 find . -ctime +3 -exec rm -rf {} \; #删除一个目录下几天前的文件和目录 find / -type f -size +10000000c -exec du -sh {} \; 2>/dev/null #查找大于10M的文件并列出文件大小 find /home -size +10k #意思是说查找/home目录下大小为10k的文件 find . -type f -mtime 0 find . -type f -mtime +1 find . -name '*.doc' -mtime 0 find / -name access_log 2>/dev/null find . -name '*.doc' 2>/dev/null find / -amin -10 # 查找在系统中最后10分钟访问的文件 find / -atime -2 # 查找在系统中最后48小时访问的文件 find /tmp -size +10000000c -and -mtime +2 find /tmp -size +10000000c -or -mtime +2 find / -empty # 查找在系统中为空的文件或者文件夹 find / -group cat # 查找在系统中属于 groupcat的文件 find / -mmin -5 # 查找在系统中最后5分钟里修改过的文件 find / -mtime -1 #查找在系统中最后24小时里修改过的文件 find / -nouser #查找在系统中属于作废用户的文件 find / -user fred #查找在系统中属于FRED这个用户的文件 find . -type f -mtime 0 -exec ls -lrt {} \; # 查看当天修改过的文件 find . -type f -mtime 0 #最近24小时内修改过的文件 find . -type f -mtime 1 #前48~24小时内修改过的文件,而不是48小时以内修改过的文件 # 近3天内修改过的文件 find . -type f -mtime 0 -o -mtime 1 -o -mtime 2 find . -type f -mtime 0 -or -mtime 1 -or -mtime 2 |
find查找文件内容
1 2 3 4 5 6 7 8 | -- 查找文件内容 -- 比如查找当前目录下面所有的php文件里面某个关键字 find ./ -type f -name "*.php" | xargs grep '关键字' -- Window查找文件内容 findstr /S /I "普通用户" *.sql |
删除目录下最后一次访问时间超过一年的日志文件
如果你的 Linux 服务器上有一个名为 logs 的目录,如何删除该目录下最后一次访问时间超过一年的日志文件呢?
答案:首先我们需要使用 cd 命令进入对应的目录,然后,命令如下:
1 | find . -type f -atime +365 -exec rm -rf {} \; |
按名称或正则表达式查找文件
让我们从最简单的用法开始。要按特定名称搜索文件,命令如下:
1 | find . -name test.txt |
如何查找所有格式为 pdf 的书籍?使用正则表达式:
1 | find ./yang/books -name "*.pdf" |
默认情况下,find 命令会搜索常规文件,但最好进行指定(-type f)以使所有内容更清晰:
1 | find ./yang/books -type f -name "*.pdf" |
查找不同类型的文件
除了搜索常规文件外,我们还可以通过指定 -type 选项来搜索其他类型的文件。
例如目录:
1 | find . -type d -name "yang*" |
或者符号链接:
1 | find . -type l -name "yang*" |
按指定的时间戳查找文件
要按指定的时间戳搜索文件,我们需要知道 Linux 系统中的 3 个不同的时间戳:
- 访问时间戳(atime):最后一次读取文件的时间。
- 修改时间戳 (mtime):文件内容最后一次被修改的时间。
- 更改时间戳 (ctime):上次更改文件元数据的时间(如,所有权、位置、文件类型和权限设置)
所以,正如开头提到的面试题,要搜索 atime 超过一年的文件,我们可以编写如下命令:
1 | find . -type f -atime +365 |
如果我们需要查找 mtime 正好是 5 天前的文件,请不要包含 +,因为它的意思是“大于”。
1 | find . -type f -mtime 5 |
显然,+ 表示“大于”,- 表示“小于”。所以我们可以搜索 ctime 在 5~10 天前的文件:
1 | find . -type f -ctime +5 -ctime -10 |
按大小查找文件
-size
选项使我们能够按指定大小查找文件。我们可以将其计量单位指定为以下约定:
1 2 3 4 5 6 | b:512 字节块(默认) c:字节 w:双字节字 k:KB M:MB G:GB |
类似于按时间戳查找文件,+
表示“大于
”,-
表示“小于
”。例如,要查找大小为 10 MB ~ 1 GB 的文件:
1 | find . -type f -size +10M -size -1G |
按权限查找文件
合理控制文件的权限是 Linux 管理员的一项重要任务。find 命令的 -perm 选项可以帮助我们按指定权限查找文件:
1 | find . -type f -perm 777 |
例如,上面的命令会搜索所有具有 777 权限的文件,这意味着一个文件对其持有者、组和所有用户具有所有的读、写和可执行权限。
按所有权查找文件
这个任务很简单。我们可以使用 -user 选项指定用户名。例如,以下命令将查找所有属于 yang 的文件:
1 | find -type f -user yang |
在找到文件后执行命令
在大多数情况下,我们希望在找到我们需要的文件后进行后续操作。例如将其删除,或检查它们的详细信息等等。-exec 命令使这些所有事情变得更加容易。
现在,要了解如何使用它,让我们回到之前提到的面试问题:
1 | find . -type f -atime +365 -exec rm -rf {} \; |
上述命令在 -exec 选项后是 rm -rf,其用于删除文件。{} 是用于查找结果的占位符。
注意:占位符 {} 非常重要,尤其是在您想删除文件时。因为,如果您不使用它,该命令将对所有文件执行(而不是您刚刚通过 find 命令找到的文件)。
做一个尝试,请在终端上执行以下两个命令,并检查它们的结果有何不同:
一个使用占位符:
1 | find . -type f -atime +5 -exec ls {} \; |
另一个不使用:
1 | find . -type f -atime +5 -exec ls \; |
-exec
选项后面的命令必须以分号(;)结束。众所周知,转义字符用于去除单个字符的特殊含义。在 Linux 中,反斜杠\
用作转义字符。所以我们将它用于分号字符。
查命令绝对路径:
which 用于查找并显示给定命令的绝对路径, 环境变量中 PATH 参数也可以被查出来。
1 2 3 4 5 6 | [root@localhost ~]# which bash /usr/bin/bash [root@localhost ~]# which ls alias ls='ls --color=auto' /usr/bin/ls |
寻找特定文件:
whereis 命令用来定位指令的二进制程序、源代码文件和 man 手册页等相关文件的路径, 该命令只能用于程序名的搜索
1 2 3 4 5 6 7 | [root@localhost ~]# whereis --help 语法格式:[ whereis [选项] 文件名 ] -b #只找二进制文件 -m #只找man文档 -s #只找源代码 |
使用 whereis -b 命令找二进制文件,与帮助手册。
1 2 3 4 5 | [root@localhost ~]# whereis -b ifconfig ifconfig: /usr/sbin/ifconfig [root@localhost ~]# whereis -m ifconfig ifconfig: /usr/share/man/man8/ifconfig.8.gz |
缓存查找文件:
locate 搜索一个数据库 / var/lib/mlocatedb, 这个数据库中含有本地所有文件信息, Linux 系统自动创建这个数据库, 并且每天自动更新一次, 所以使用 locate 命令查不到最新变动过的文件, 为了避免这种情况, 可以在使用 locate 之前, 先使用 updatedb 命令, 手动更新数据库, updatedb 命令会根据 / etc/updatedb.conf 来更新文件.
1 2 3 4 5 6 7 8 | [root@localhost ~]# yum install -y mlocate [root@localhost ~]# locate --help 语法格式:[ locate [选项] 文件名 ] -d 目录 #指定数据库所在的目录 -i #忽略大小写差异 -r #后面接正则表达式 |
使用 locate 命令查询一个文件.
1 2 3 4 | [root@localhost ~]# updatedb [root@localhost ~]# locate /etc/passwd /etc/passwd /etc/passwd- |
遍历文件查找:
find 命令可以说是最重要的查找命令了,该命令参数较多。
1 2 3 4 5 6 7 8 9 10 11 12 | [root@localhost ~]# find --help 语法格式:[ find [目录] [属性] 文件名 ] -name #按文件名查找 -size #根据大小查找 -user #根据属主查找 -perm #根据权限查找 -type #根据类型查找 -time #按时间查找 -inum #根据i节点查询 -exec #查找后执行命令 |
-name 按文件名查找:
常用查询通配符
1 2 3 | \* #匹配任意一个或多个字符 ? #匹配任意一个字符 [] #指定范围,外侧加引号 |
查找 / var / 目录下, 以. log 结尾的文件.
1 2 3 4 5 6 | [root@localhost ~]# find /var/ -name "*.log" /var/log/tuned/tuned.log /var/log/audit/audit.log /var/log/anaconda/X.log /var/log/anaconda/program.log ....省略.... |
查找 / root / 目录下, 以 [1-3 之间], 结尾是. txt 的文件
1 2 3 4 5 6 7 | [root@localhost ~]# ls 1.txt 2.txt 3.txt Catalog File [root@localhost ~]# find /root/ -name "[1-3].txt" /root/1.txt /root/2.txt /root/3.txt |
查找 / etc / 目录下, 开头是 6 个任意字符的文件
1 2 3 4 5 6 | [root@localhost ~]# find /etc/ -name "??????" /etc/grub.d /etc/grub.d/README /etc/shells /etc/init.d ....省略.... |
-size 根据大小查找
1 2 3 | 单位是 block 数据块 一块是512字节 1M -> 1024k -> 2048 块 (1块是0.5k 也就是512字节) 100M -> 102400k -> 204800块 |
查找 / etc / 目录下, 小于 10k 的文件
1 2 3 4 5 | root@localhost ~]# find /etc/ -size -10k /etc/crypttab /etc/.pwd.lock /etc/environment ....省略.... |
查找 / etc / 目录下, 大于 1M 的文件
1 2 3 4 5 6 7 8 | [root@localhost ~]# find /etc/ -size +1M #查询大于1M的文件 /etc/udev/hwdb.bin /etc/selinux/targeted/active/policy.kern /etc/selinux/targeted/contexts/files/file_contexts.bin /etc/selinux/targeted/policy/policy.31 ....省略.... #注意:+-号如果没有,是精确到这么大,通常都会带上+或-号表示一个范围.公众号:入门小站 |
-user 根据属主与权限查找
在 / root 目录中查找属于 wang 用户的文件
1 2 3 4 5 | [root@localhost ~]# find /root/ -user wang /root/1.txt /root/2.txt /root/3.txt #注意:系统中要存在该用户,否则会报错误. |
查找 / boot / 目录中权限是 644 的文件
1 2 3 4 5 6 | [root@localhost ~]# find /boot/ -perm 0644 /boot/grub2/device.map /boot/grub2/i386-pc/gcry_rmd160.mod /boot/grub2/i386-pc/acpi.mod /boot/grub2/i386-pc/gcry_rsa.mod ....省略.... |
-type 根据类型查找
1 2 3 | -type f 二进制文件(普通文件) -type l 软链接文件 -type d 目录 |
查找 / usr/bin / 目录下, 类型是二进制文件.
1 2 3 4 5 6 7 | [root@localhost ~]# find /usr/bin/ -type f /usr/bin/cp /usr/bin/gzip /usr/bin/alias /usr/bin/csplit /usr/bin/bash ....省略.... |
-time 按时间查找
1 2 3 4 5 6 | 按天数 ctime atime mtime 按分钟 cmin amin mmin c change #表示属性被修改过:所有者、所属组、权限 a access #被访问过(被查看过) m modify #表示内容被修改过 |
查找 / etc / 目录下, 在 120 分钟以内, 内容被修改过的文件
1 2 3 4 5 6 7 8 | [root@localhost ~]# find /etc/ -mmin -120 /etc/ /etc/resolv.conf /etc/group- /etc/gshadow- /etc/group /etc/gshadow ....省略.... |
查找 / etc / 目录下, 在 7 天之前, 属性被修改过的文件
1 2 3 4 5 | [root@localhost ~]# find /etc/ -ctime +7 /etc/resolv.conf /etc/group- /etc/gshadow- ....省略.... |
-inum 根据 i 节点查询
有一些文件的硬链接数量很多,有相同的 i 节点,查找其中一个文件的 i 节点号,一次性删除。
1 | [root@localhost ~]# find ./ -inum 1024 -exec rm{} \; #删除相同I节点的数据 |
-and or 逻辑连接符
1 2 | -a (and 逻辑与) -o (or 逻辑或) |
在 / etc / 目录下, 查找大于 1k, 并且小于 10k 的文件
1 2 3 4 5 6 7 8 9 10 | [root@localhost ~]# find /etc/ -size +1k -a -size -10k /etc/ /etc/grub.d/00_header /etc/grub.d/20_ppc_terminfo /etc/grub.d/00_tuned /etc/rc.d/init.d/README /etc/rc.d/init.d/netconsole /etc/rc.d/init.d/network /etc/pam.d ....省略.... |
-exec 命令执行连接符
1 2 3 4 5 6 7 8 9 10 11 12 13 | [查询格式] find ... -exec 命令 {} \; {} #表示find查询的结果集 \ #是转义符,不使用命令别名,直接使用命令本身 ; #分号是表示语句的结束. #注意:固定格式,只能这样写.注意中间的空格. (公众号:入门小站)----------------------------------------------------------------- 说明: 转义符的作用是什么? 在linux中有一个别名机制,如rm删除文件,执行的却是rm -i(用which rm 可以查看命令别名), 使用rm删除文件前会提示,就是因为rm -i这个参数。如果想使用命令原意,可以在加\转义, 如:\rm test.txt 则不会提示,直接删除 |
查找 / var/log / 目录下名字以. log 结尾的文件, 找到后执行 ls -l 显示详细信息. 公众号:入门小站
1 2 3 4 5 6 7 8 9 10 11 12 13 | [root@localhost ~]# find /var/log/ *.log -exec ls -l {} \; total 1176 drwxr-xr-x. 2 root root 204 Sep 18 09:12 anaconda drwx------. 2 root root 23 Sep 18 09:12 audit -rw-------. 1 root root 53001 Sep 19 00:57 boot.log -rw-------. 1 root utmp 384 Sep 18 09:22 btmp drwxr-xr-x. 2 chrony chrony 6 Apr 12 13:37 chrony -rw-------. 1 root root 3523 Sep 19 01:01 cron -rw-r--r-- 1 root root 119414 Sep 19 00:57 dmesg -rw-r--r-- 1 root root 119599 Sep 18 23:35 dmesg.old -rw-r--r--. 1 root root 1320 Sep 19 00:23 firewalld -rw-r--r--. 1 root root 193 Sep 18 09:05 grubby_prune_debug .... |
查找 / etc / 目录下名字以 "init*" 开头的文件, 找到后, 只列出文件, 过滤掉目录, 并执行 ls -l 显示详细信息.
1 2 3 4 5 6 | [root@localhost ~]# find /etc/ -name "init*" -a -type f -exec ls -l {} \; -rw-r--r--. 1 root root 511 Apr 11 01:09 /etc/inittab -rw-r--r--. 1 root root 798 Apr 11 01:09 /etc/sysconfig/init -rwxr-xr-x. 1 root root 5419 Jan 2 2018 /etc/sysconfig/network-scripts/init.ipv6-global -rw-r--r--. 1 root root 30 Apr 11 14:12 /etc/selinux/targeted/contexts/initrc_context |
查找 / tmp / 下, 的 yum.log 文件, 找到后直接删除.
1 2 | [root@localhost tmp]# find /tmp/ -name yum.log -exec rm {} \; [root@localhost tmp]# |
查找根下, 找关于 lyshark 用户的所有文件, 找到后直接删除.
1 2 3 4 5 6 7 8 9 | [root@localhost ~]# find / -user lyshark -exec rm -r {} \; find: ‘/proc/1465/task/1465/fd/6’: No such file or directory find: ‘/proc/1465/task/1465/fdinfo/6’: No such file or directory find: ‘/proc/1465/fd/5’: No such file or directory find: ‘/proc/1465/fdinfo/5’: No such file or directory find: ‘/root/Catalog’: No such file or directory find: ‘/home/lyshark’: No such file or directory #rm -r 连带目录一起删除.报错原因:-exec 不适合大量传输,速率慢导致. |
在根下, 查找 lyshark 用户的文件, 找到后删除, 删除前会提示是否删除.
1 2 3 4 5 6 | [root@localhost ~]# find / -user lyshark -ok rm -r {} \; find: ‘/proc/1777/task/1777/fd/6’: No such file or directory find: ‘/proc/1777/task/1777/fdinfo/6’: No such file or directory < rm ... /root/LyShark > ? y # -ok的使用和-exec是一样的,区别是-ok,执行时会提示你是否进行下一步操作. |
参考
https://zhuanlan.zhihu.com/p/527958063
https://mp.weixin.qq.com/s/qllnjOfo93ENfeEbn1WhRQ