Linux模糊查找文件应该用什么命令比较好?

系统 Linux
本文主要讲解如何在Linux系统下使用命令行工具模糊查找文件,本文的工具包括find命令的用法也介绍grep命令的使用方法,同时也有find与grep两者结合,能更精确根据条件查找文件。

 1. 前言

本文主要讲解如何在Linux系统下使用命令行工具模糊查找文件,本文的工具包括find命令的用法也介绍grep命令的使用方法,同时也有find与grep两者结合,能更精确根据条件查找文件。

[[275183]]

2. 根据文件名模糊查找文件

例如:

在目录/var/zcwyou里,找到.txt后缀的文件,文件名大小写敏感。即只能匹配.txt后缀的

  1. [root@zcwyou ~]# find /var/zcwyou -name '*.txt' 

在目录/var/zcwyou里,找到.txt后缀的文件,忽略大小定。即可以匹配.txt后缀,也可以匹配.Txt后缀,也可以匹配.TXT后缀的文件。

  1. [root@zcwyou ~]# find /var/zcwyou -iname '*.txt' 

3. 根据文件名和文件内容模糊查找文件

使用方法:

结合find命令与xargs和grep -i ‘目标匹配关键字’

案例1:

在当前目录搜索文件内容含有某字符串(大小写敏感)的文件:

语法:

find /etc/ -type f | xargs grep '目标关键词'

案例2:

在/etc/目录下,查找带zcwyou关键词的文件

  1. [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进行搜索和过滤。

  1. [root@zcwyou ~]#find . -type f -name ‘*.c’ | xargs grep -i '目标搜索关键词' 

4. 使用`find`命令模糊查找linux文件

在当前目录下,模糊查找在系统中最后10分钟访问的文件

  1. [root@zcwyou ~]# find . -amin -10 

在当前目录下,模糊查找在系统中最后48小时访问的文件

  1. [root@zcwyou ~]# find . -atime -2 

在当前目录下,查找在系统中为空的文件或者文件夹

  1. [root@zcwyou ~]# find . -empty 

在当前目录下,查找在系统中属于group cat的文件

  1. [root@zcwyou ~]# find . -group cat 

模糊查找在系统中最后5分钟里修改过的文件

  1. [root@zcwyou ~]# find . -mmin -5 

查找在系统中最后24小时里修改过的文件

  1. [root@zcwyou ~]# find . -mtime -1

5. 根据用户属性模糊查找Linux文件

查找在系统中属于作废用户的文件

  1. [root@zcwyou ~]# find . -nouser 

查找在系统中属于zcwyou这个用户的文件

  1. [root@zcwyou ~]# find . -user zcwyou 

6. 通过文件大小模糊查找文件

查找根目录下超过1M的文件,并打印

  1. [root@zcwyou ~]# find / -size +1M -type f -print 

查找当前目录下等于500字节的文件,包括子目录,并打印结果

  1. [root@zcwyou ~]# find -size 500c -print 

查找home目录下小于600k的文件

  1. [root@zcwyou ~]# find /home -size -600k -print 

7. 通过文件新旧查找文件

查找比old.txt新的文件

  1. [root@zcwyou ~]# find -newer "old.txt" -type f -print 

查找比newer.txt旧的文件

  1. [root@zcwyou ~]# find ! -newer "newer.log" -type f -print 

查找比old.txt新,比newer.txt旧的文件

  1. [root@zcwyou ~]# find -newer 'old.txt' ! -newer 'newer.txt' -type f -print 

 

责任编辑:华轩 来源: 今日头条
相关推荐

2021-08-05 08:32:45

TypeScript InterfaceType

2014-06-05 15:16:49

Linux开源Flash

2018-04-28 09:12:42

Linux

2022-05-25 08:41:48

Linuxfd 命令文件

2021-12-12 09:19:16

find命令Linux

2009-09-15 09:24:42

思科认证考试思科认证

2020-07-28 10:40:26

大数据专业技术

2020-09-23 16:53:46

Python编辑器工具

2020-11-18 09:26:52

@property装饰器代码

2020-12-08 15:54:15

编程语言Python

2018-07-10 16:05:05

2019-07-03 11:16:51

Linux数据库Line

2019-09-12 08:21:22

Linuxfind命令查找

2022-05-06 08:00:00

APIBallerina编程语言

2018-01-29 14:57:16

Linux文件权限find命令

2023-11-04 15:56:02

2021-11-11 07:02:33

类型函数调用

2021-11-30 23:01:51

编程语言数据Python

2022-10-17 09:34:18

2021-03-15 14:09:49

电脑软件安全
点赞
收藏

51CTO技术栈公众号