字符串模糊查找 | 您所在的位置:网站首页 › Linux模糊查找文件路径 › 字符串模糊查找 |
请关注本头条号,每天坚持更新原创干货技术文章。 如需学习视频,请在微信搜索公众号“智传网优”直接开始自助视频学习 1. 前言 本文主要讲解如何在Linux系统下使用命令行工具模糊查找文件,本文的工具包括find命令的用法也介绍grep命令的使用方法,同时也有find与grep两者结合,能更精确根据条件查找文件。 ![]() linux模糊查找文件命令 2. 根据文件名模糊查找文件 例如: 在目录/var/zcwyou里,找到.txt后缀的文件,文件名大小写敏感。即只能匹配.txt后缀的 [root@zcwyou ~]# find /var/zcwyou -name '*.txt'在目录/var/zcwyou里,找到.txt后缀的文件,忽略大小定。即可以匹配.txt后缀,也可以匹配.Txt后缀,也可以匹配.TXT后缀的文件。 [root@zcwyou ~]# find /var/zcwyou -iname '*.txt'![]() linux根据文件名模糊查找文件 3. 根据文件名和文件内容模糊查找文件 使用方法: 结合find命令与xargs和grep -i ‘目标匹配关键字’ 案例1: 在当前目录搜索文件内容含有某字符串(大小写敏感)的文件: 语法: find /etc/ -type f | xargs grep '目标关键词' 案例2: 在/etc/目录下,查找带zcwyou关键词的文件 [root@zcwyou ~]# find /etc/ -type f | xargs grep 'zcwyou'可以找到以下文件和文件对应的内容: /etc/group-:zcwyou:x:1000: /etc/gshadow-:zcwyou:!!:: /etc/passwd-:zcwyou:x:1000:1000:zcwyou:/home/zcwyou:/bin/bash /etc/group:zcwyou:x:1000:zcwyou /etc/shadow-:zcwyou: 是不是非常高效? 案例3: 在当前目录搜索文件内容含有某字符串(忽略大小写)的特定文件: 借用find命令查找当前目录下所有以.c后缀结尾的文件和子目录里以.c后缀结尾的文件,并把结果以参数形式传递给grep,由grep进行搜索和过滤。 [root@zcwyou ~]#find . -type f -name ‘*.c’ | xargs grep -i '目标搜索关键词'![]() linux根据文件名和文件内容模糊查找文件 4. 使用`find`命令模糊查找linux文件 在当前目录下,模糊查找在系统中最后10分钟访问的文件 [root@zcwyou ~]# find . -amin -10在当前目录下,模糊查找在系统中最后48小时访问的文件 [root@zcwyou ~]# find . -atime -2在当前目录下,查找在系统中为空的文件或者文件夹 [root@zcwyou ~]# find . -empty在当前目录下,查找在系统中属于group cat的文件 [root@zcwyou ~]# find . -group cat模糊查找在系统中最后5分钟里修改过的文件 [root@zcwyou ~]# find . -mmin -5查找在系统中最后24小时里修改过的文件 [root@zcwyou ~]# find . -mtime -1![]() 使用find命令模糊查找linux文件 5. 根据用户属性模糊查找Linux文件 查找在系统中属于作废用户的文件 [root@zcwyou ~]# find . -nouser查找在系统中属于zcwyou这个用户的文件 [root@zcwyou ~]# find . -user zcwyou![]() 根据用户属性模糊查找Linux文件 6. 通过文件大小模糊查找文件 查找根目录下超过1M的文件,并打印 [root@zcwyou ~]# find / -size +1M -type f -print查找当前目录下等于500字节的文件,包括子目录,并打印结果 [root@zcwyou ~]# find -size 500c -print查找home目录下小于600k的文件 [root@zcwyou ~]# find /home -size -600k -print![]() linux通过文件大小模糊查找文件 7. 通过文件新旧查找文件 查找比old.txt新的文件 [root@zcwyou ~]# find -newer "old.txt" -type f -print查找比newer.txt旧的文件 [root@zcwyou ~]# find ! -newer "newer.log" -type f -print查找比old.txt新,比newer.txt旧的文件 [root@zcwyou ~]# find -newer 'old.txt' ! -newer 'newer.txt' -type f -print |
CopyRight 2018-2019 实验室设备网 版权所有 |