Linux文件查找find命令用法

简介:

一、含义

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,如需转载请自行联系原作者

相关文章
|
1天前
|
Linux 数据安全/隐私保护
Linux常用命令实例带注释
Linux常用命令实例带注释
5 0
|
1天前
|
Linux 开发工具 数据安全/隐私保护
Linux(19)常用解压命令记录
Linux(19)常用解压命令记录
6 0
|
2天前
|
机器学习/深度学习 缓存 监控
linux查看CPU、内存、网络、磁盘IO命令
`Linux`系统中,使用`top`命令查看CPU状态,要查看CPU详细信息,可利用`cat /proc/cpuinfo`相关命令。`free`命令用于查看内存使用情况。网络相关命令包括`ifconfig`(查看网卡状态)、`ifdown/ifup`(禁用/启用网卡)、`netstat`(列出网络连接,如`-tuln`组合)以及`nslookup`、`ping`、`telnet`、`traceroute`等。磁盘IO方面,`iostat`(如`-k -p ALL`)显示磁盘IO统计,`iotop`(如`-o -d 1`)则用于查看磁盘IO瓶颈。
|
2天前
|
Linux Perl
Linux系统替换字符串常用命令
请注意,`sed`命令可以非常强大,可以根据不同的需求使用不同的选项和正则表达式来进行更复杂的字符串替换操作。
16 0
|
5天前
|
安全 Linux 开发工具
Linux中可引起文件时间戳改变的相关命令
【4月更文挑战第12天】Linux中可引起文件时间戳改变的相关命令
12 0
|
6天前
|
域名解析 网络协议 Linux
Linux 中的 Nslookup 命令怎么使用?
【4月更文挑战第12天】
25 6
Linux 中的 Nslookup 命令怎么使用?
|
7天前
|
Linux Shell 开发工具
Linux文件常用操作
Linux文件常用操作(几乎覆盖所有日常使用)
61 0
|
7天前
|
运维 网络协议 Unix
18.系统知识-Linux常用命令
18.系统知识-Linux常用命令
|
8天前
|
网络协议 Ubuntu Linux
Linux 下 TFTP 服务搭建及 U-Boot 中使用 tftp 命令实现文件下载
Linux 下 TFTP 服务搭建及 U-Boot 中使用 tftp 命令实现文件下载
|
15天前
|
Web App开发 Linux 网络安全
工作中常用到的Linux命令
工作中常用到的Linux命令