Linux文件查找find命令用法

本文涉及的产品
运维安全中心(堡垒机),企业双擎版 50资产 7天
运维安全中心(堡垒机),免费版 6个月
简介:

一、含义

find是linux命令,可以将系统内符合expression的文件列出来。指定文件按名称、类别、时间、大小、权限等不同属性组合时,只有完全相符的才会被列出来。

相对于locate查找命令,find具有精确匹配、实时查找的优势,但是同时由于需要遍历指定路径下的文件,所以查找速度慢,比较耗费系统资源。


二、find命令

find [option] [查找路径] [查找条件] [处理动作]

查找路径:默认当前目录
查找条件:默认为查找指定路径下的所有文件
处理动作:默认为显示


查找条件:

-name "文件名称":根据文件名称进行查找,支持文件名通配(globbing)

1
2
3
4
#查找/etc/下文件名为network的文件
[Linux] #find /etc/ -name "network"
/etc/sysconfig/network
/etc/rc.d/init.d/network

-iname "文件名称":查找时忽略大小写

1
2
3
[Linux] #find /tmp/ -iname abc
/ tmp / Abc
/ tmp / abc

-u username:根据文件属主查找

1
2
3
4
5
[Linux] #find /tmp/ -user hadoop
/ tmp / abc.tar.xz
/ tmp / etc
/ tmp / etc / netconfig
[Linux] #

-g groupname:根据文件属组查找

1
2
3
4
5
6
[Linux] #find /tmp/ -group hadoop
/ tmp / Abc
/ tmp / abc.tar.xz
/ tmp / etc
/ tmp / etc / netconfig
[Linux] #

-uid UID:根据文件UID查找

1
2
3
4
[Linux] #find /tmp/ -uid 500
/ tmp / 1M .txt
/ tmp / 1.5M .txt
[Linux] #

-gid GID:根据文件GID查找

1
2
3
4
5
[Linux] #find /tmp/ -gid 501
/ tmp / 2M .txt
/ tmp / 1.5M .txt
/ tmp / 2.1M .txt
[Linux] #

-nouser:查找没有属主的文件

1
2
3
4
5
6
[Linux] #find /tmp/ -nouser
/ tmp / myMBR
/ tmp / 3M .txt
/ tmp / abc
/ tmp / 512K .txt
[Linux] #

-nogroup:查找没有属组的文件

1
2
3
4
[Linux] #find /tmp/ -nogroup
/ tmp / abc
/ tmp / 512K .txt
[Linux] #



对查找条件可以组合一起使用:

-a:与;同时满足

1
2
3
4
#查找用户为root,且组为hadoop的文件
[Linux] #find /tmp/ -user root -a -group hadoop
/ tmp / Abc
[Linux] #

-o:或;至少一个满足

1
2
3
4
5
6
[Linux] #find /tmp/ -nouser -o -user stu7
/ tmp / myMBR
/ tmp / 3M .txt
/ tmp / abc
/ tmp / 512K .txt
[Linux] #

-not|!:非;取反

1
2
3
4
5
6
7
8
9
10
11
[Linux] #find /tmp/ -not -user root
/ tmp / myMBR
/ tmp / 1M .txt
/ tmp / 1.5M .txt
/ tmp / abc.tar.xz
/ tmp / 3M .txt
/ tmp / etc
/ tmp / etc / netconfig
/ tmp / abc
/ tmp / 512K .txt
[Linux] #


type:根据文件类型查找文件

f:普通文件

d:目录文件

b:块设备

c:字符设备

l:符号链接

1
2
3
4
5
6
7
8
[Linux] #find /dev/ -type l
/ dev / myvg / mylv
/ dev / myvg / snap - mylv
/ dev / dvd
/ dev / cdrom
/ dev / md / station73.magelinux.com: 5
/ dev / MAKEDEV
/ dev / core

s:套接字

1
2
3
[Linux] #find /dev/ -type s
/ dev / log
[Linux] #

p:管道



-size:根据文件大小查找

-size [+|-]#Unit{K|M|G}

#Unit:精确匹配到#到#-1个单位内的文件

1
2
3
4
[Linux] #find /tmp/ -size 2M
/ tmp / 2M .txt
/ tmp / 1.5M .txt
[Linux] #

+#Unit:匹配大于#个单位的文件

1
2
3
4
[Linux] #find /tmp/ -size +2M
/ tmp / 3M .txt
/ tmp / 2.1M .txt
[Linux] #

-#Unit:匹配大小为#-1个单位之外的文件

1
2
3
4
5
6
[Linux] #find /tmp/ -size -2M
/ tmp /
/ tmp / myMBR
/ tmp / 1M .txt
/ tmp / scripttest
/ tmp / rc.sysinit


根据时间戳查找:

以天为单位(time):支持[+|-]

-atime:访问时间

+#:表示(#+1)天之外访问过的文件

1
2
3
4
5
6
7
[Linux] #find /tmp/ -atime +2
/ tmp / myMBR
/ tmp / rc.sysinit
/ tmp / abc.tar.xz
/ tmp / etc / netconfig
/ tmp / script
[Linux] #

-mtime:修改时间

-#:表示#天之内被修改过的文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[Linux] #find /tmp/ -mtime -2
/ tmp /
/ tmp / 2M .txt
/ tmp / 1M .txt
/ tmp / 1.5M .txt
/ tmp / Abc
/ tmp / 3M .txt
/ tmp / 2.1M .txt
/ tmp / network
/ tmp / abc
/ tmp / 512K .txt
/ tmp / NetworkManager
/ tmp / .ICE - unix
[Linux] #

-ctime:改变时间

#:表示大于#+1> # >=#天内改变的文件

1
2
3
4
[Linux] #find /tmp/ -ctime 1
/ tmp / network
/ tmp / NetworkManager
[Linux] #


以分钟为单位(min):支持[+|-]

用法与以天为单位是一样的;就不再举例说明

-amin [+|-]#

-mmin [+|-]#

-cmin [+|-]#


根据权限查找:

-perm [+|-]MODE

MODE:精确匹配

1
2
3
4
5
6
#匹配权限精确为444的文件
[Linux] #find /tmp/ -perm 444
/ tmp / 1M .txt
/ tmp / 1.5M .txt
/ tmp / 3M .txt
[Linux] #

+MODE:任何一类用户的任何一位权限匹配;通常查找某类用户的某特定权限是否存在

1
2
3
4
5
6
7
#查找其他用户是否有写权限,0代表不查找
[Linux] #find /tmp/ -perm +002
/ tmp /
/ tmp / Abc
/ tmp / abc
/ tmp / .ICE - unix
[Linux] #

-MODE:每类用户的指定要检查的权限位都匹配

1
2
3
4
5
#查找每类用户都有写权限的
[Linux] #find /tmp/ -perm -222
/ tmp /
/ tmp / .ICE - unix
[Linux] #


处理动作:

-print:打印在标准输出上

-ls:以长格式输出匹配文件的信息

1
2
3
4
[Linux] #find /tmp/ -perm -222 -ls
262145     4  drwxrwxrwt    6  root     root          4096  Mar   1  14 : 23  / tmp /
262146     4  drwxrwxrwt    2  root     root          4096  Mar   1  13 : 48  / tmp / .ICE - unix
[Linux] #

-exec COMMAND {} \;:对匹配到的文件执行指定的命令

1
2
3
4
5
6
7
8
9
10
[Linux] #find /tmp/ -iname "*.txt" -exec mv {} {}x \;
[Linux] #find /tmp/ -iname "*.txtx"
/ tmp / 512K .txtx
/ tmp / 1M .txtx
/ tmp / 3M .txtx
/ tmp / ccc.txtx
/ tmp / 1.5M .txtx
/ tmp / 2.1M .txtx
/ tmp / 2M .txtx
[Linux] #

-ok COMMAND {} \;:同于-exec,但是是交互式操作

find ...| xargs COMMAND

1
2
3
4
5
6
7
8
9
[Linux] #find /tmp/ -iname "*.txtx" | xargs ls -lh
- r - - r - - r - -  1  centos gentoo  1.6M  Mar   1  11 : 45  / tmp / 1.5M .txtx
- r - - r - - r - -  1  centos root    1.0M  Mar   1  11 : 43  / tmp / 1M .txtx
- rw - r - - r - -  1  root   gentoo  2.1M  Mar   1  11 : 44  / tmp / 2.1M .txtx
- rw - r - - r - -  1  root   gentoo  2.0M  Mar   1  11 : 43  / tmp / 2M .txtx
- r - - r - - r - -  1  stu7   stu7    3.0M  Mar   1  11 : 43  / tmp / 3M .txtx
- rw - r - - r - -  1     514     515  512K  Mar   1  11 : 44  / tmp / 512K .txtx
- rw - r - - r - -  1  root   root       0  Mar   1  14 : 23  / tmp / ccc.txtx
[Linux] #


三、具体实例

1、查找/var/目录属主为root且属组为mail的所有文件;

1
2
3
[Linux] #find /var/ -user root -a -group mail
/ var / spool / mail
[Linux] #

2、查找/usr目录下不属于root、bin或hadoop的所用文件;

1
2
3
4
5
6
[Linux] #find /usr/ -not \( -user root -o -user bin -o -user hadoop \) |less
/ usr / libexec / abrt - action - install - debuginfo - to - abrt - cache
/ usr / local / apache / cgi - bin / test - cgi
/ usr / local / apache / cgi - bin / printenv.wsf
/ usr / local / apache / cgi - bin / printenv.vbs
/ usr / local / apache / cgi - bin / printenv

3、查找/etc/目录下最近一周内其内容修改过的,且不属于root或hadoop的文件;

1
2
3
[Linux] #find /etc/ -atime -7 -not \( -user root -o -user hadoop \)
/ etc / keystone / policy.json
[Linux] #

4、查找当前系统上没有属主或属组,且最近1个月内曾被访问过的文件;

1
2
3
[Linux] #find /tmp/ -atime -30 -a \( -nouser -o -nogroup \)
/ tmp / abc
[Linux] #

5、查找/etc/目录下大于1M且类型为普通文件的所有文件;

1
2
3
4
5
[Linux] #find /etc/ -size +1M -a -type f
/ etc / selinux / targeted / modules / active / policy.kern
/ etc / selinux / targeted / policy / policy. 24
/ etc / gconf / gconf.xml.defaults / % gconf - tree.xml
[Linux] #

6、查找/etc/目录所有用户都没有写权限的文件;

1
2
3
4
5
6
7
8
9
10
11
12
13
[Linux] #find /etc/ -not -perm +222
/ etc / pam.d / cups
/ etc / openldap / certs / password
/ etc / shadow
/ etc / dbus - 1 / system.d / cups.conf
/ etc / shadow -
/ etc / ld.so.conf.d / kernel - 2.6 . 32 - 358.el6 .x86_64.conf
/ etc / gshadow
/ etc / rc.d / init.d / lvm2 - monitor
/ etc / rc.d / init.d / blk - availability
/ etc / rc.d / init.d / lvm2 - lvmetad
/ etc / sudoers
[Linux] #

7、查找/etc/目录下至少有一类用户没有写权限;

1
2
3
4
5
6
7
8
9
10
[Linux] #find /etc/ -not -perm -222 | less
/ etc / pam.d
/ etc / pam.d / system - auth - ac
/ etc / pam.d / config - util
/ etc / pam.d / runuser - l
/ etc / pam.d / remote
/ etc / pam.d / setup
/ etc / pam.d / gdm - fingerprint
/ etc / pam.d / system - config - users
/ etc / pam.d / password - auth - ac

8、查找/etc/init.d/目录下,所有用户都有执行权限且其它用户有写权限的文件;

1
2
[Linux] #find /etc/init.d/ -perm -113
[Linux] #



find相对较占用系统资源,所以建议不要经常使用,不过当别人不小心进了你的服务器,这时查找就会方便很多的。


本文转自Mr_陈 51CTO博客,原文链接:http://blog.51cto.com/chenpipi/1365559,如需转载请自行联系原作者

相关文章
|
3月前
|
Linux 应用服务中间件 Shell
二、Linux文本处理与文件操作核心命令
熟悉了Linux的基本“行走”后,就该拿起真正的“工具”干活了。用grep这个“放大镜”在文件里搜索内容,用find这个“探测器”在系统中寻找文件,再用tar把东西打包带走。最关键的是要学会使用管道符|,它像一条流水线,能把这些命令串联起来,让简单工具组合出强大的功能,比如 ps -ef | grep 'nginx' 就能快速找出nginx进程。
二、Linux文本处理与文件操作核心命令
|
3月前
|
Linux
linux命令—stat
`stat` 是 Linux 系统中用于查看文件或文件系统详细状态信息的命令。相比 `ls -l`,它提供更全面的信息,包括文件大小、权限、所有者、时间戳(最后访问、修改、状态变更时间)、inode 号、设备信息等。其常用选项包括 `-f` 查看文件系统状态、`-t` 以简洁格式输出、`-L` 跟踪符号链接,以及 `-c` 或 `--format` 自定义输出格式。通过这些选项,用户可以灵活获取所需信息,适用于系统调试、权限检查、磁盘管理等场景。
296 137
|
3月前
|
安全 Ubuntu Unix
一、初识 Linux 与基本命令
玩转Linux命令行,就像探索一座新城市。首先要熟悉它的“地图”,也就是/根目录下/etc(放配置)、/home(住家)这些核心区域。然后掌握几个“生存口令”:用ls看周围,cd去别处,mkdir建新房,cp/mv搬东西,再用cat或tail看文件内容。最后,别忘了随时按Tab键,它能帮你自动补全命令和路径,是提高效率的第一神器。
688 57
|
2月前
|
存储 安全 Linux
Linux卡在emergency mode怎么办?xfs_repair 命令轻松解决
Linux虚拟机遇紧急模式?别慌!多因磁盘挂载失败。本文教你通过日志定位问题,用`xfs_repair`等工具修复文件系统,三步快速恢复。掌握查日志、修磁盘、验重启,轻松应对紧急模式,保障系统稳定运行。
425 2
|
3月前
|
缓存 监控 Linux
Linux内存问题排查命令详解
Linux服务器卡顿?可能是内存问题。掌握free、vmstat、sar三大命令,快速排查内存使用情况。free查看实时内存,vmstat诊断系统整体性能瓶颈,sar实现长期监控,三者结合,高效定位并解决内存问题。
288 0
Linux内存问题排查命令详解
|
3月前
|
Unix Linux 程序员
Linux文本搜索工具grep命令使用指南
以上就是对Linux环境下强大工具 `grep` 的基础到进阶功能介绍。它不仅能够执行简单文字查询任务还能够处理复杂文字处理任务,并且支持强大而灵活地正则表达规范来增加查询精度与效率。无论您是程序员、数据分析师还是系统管理员,在日常工作中熟练运用该命令都将极大提升您处理和分析数据效率。
280 16
|
4月前
|
Linux 网络安全 开发工具
技术栈:这50条最常用的 Linux 命令你一定要会!
建议多在终端中实践,遇到不懂的命令就用 man 或 --help 了解详情!
526 0
|
Linux Go 数据安全/隐私保护
linux常用命令手册 用户管理useradd 文件权限管理chmod 搜索管理find grep
linux常用命令手册 用户管理useradd 文件权限管理chmod 搜索管理find grep
219 2
|
12月前
|
Linux
linux查看目录下的文件夹命令,find查找某个目录,但是不包括这个目录本身?
通过本文的介绍,您应该对如何在 Linux 系统中查看目录下的文件夹以及使用 `find` 命令查找特定目录内容并排除该目录本身有了清晰的理解。掌握这些命令和技巧,可以大大提高日常文件管理和查找操作的效率。 在实际应用中,灵活使用这些命令和参数,可以帮助您快速定位和管理文件和目录,满足各种复杂的文件系统操作需求。
1225 8
|
机器学习/深度学习 存储 Linux
linux中强大且常用命令:find、xargs、grep
linux中强大且常用命令:find、xargs、grep
917 9