文件查找及find命令详解

find

1、文件查找:

在文件系统上查找符合条件的文件的过程;

2、文件查找:locate, find

locate: 非实时查找工具;依赖于事先构建的索引;索引的构建是在系统较为空闲时自动进行(周期性任务);手动更新此数据库(updatedb);查找速度快;模糊查找;

find:实时查找;查找速度略慢;精确查找;

3、find命令使用方法:

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

查找路径:默认为当前路径;

查找条件:指定的查找标准,可以根据文件名、大小、属主属组、类型等进行;默认为找出指定路 径下的所有文件;

处理动作:对符合条件的文件做什么操作;默认为输出至屏幕;

4、查找条件:

  • 根据文件名进行查找:

-name "文件名称": 支持使用glob;

*, ?, []

-iname "文件名称":不区分字符大小写,支持使用glob;

-regex "PATTERN":以PATTERN匹配整个文件路径字符串,而不仅仅是文件名称;

  • 根据属主、属组查找:

-user USERNAME: 查找属主为指定用户的文件;

-group GROUPNAME:

-uid UserID: 查找文件的属主指定uid的文件;

-gid GroupID:

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

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

例子:

[[email protected] tmp]# find /tmp/ -user centos -ls
654082    0 -rw-------   1 centos   root            0 8月 25 21:08 /tmp/yum.log
[[email protected] tmp]# find /tmp/ -group centos -ls
654211    4 -rw-r--r--   1 root     centos        126 9月  1 12:46 /tmp/111
[[email protected] tmp]# find /tmp/ -user centos -a -group centos -ls
654210   20 -rw-r--r--   1 centos   centos      19697 4月 10 00:44 /tmp/functions
[[email protected] tmp]# find /tmp/ -uid 3008 -ls
654210   20 -rw-r--r--   1 3008     3008        19697 4月 10 00:44 /tmp/functions
654082    0 -rw-------   1 3008     root            0 8月 25 21:08 /tmp/yum.log
[[email protected] tmp]# find /tmp/ -nouser -ls
654210   20 -rw-r--r--   1 3008     3008        19697 4月 10 00:44 /tmp/functions
654082    0 -rw-------   1 3008     root            0 8月 25 21:08 /tmp/yum.log
[[email protected] tmp]# find /tmp/ -nouser -a -nogroup -ls
654210   20 -rw-r--r--   1 3008     3008        19697 4月 10 00:44 /tmp/functions
  • 根据文件类型进行查找:

-type TYPE

f: 普通文件

d: 目录

l: 符号链接

b: 块设备

c: 字符设备

p: 命名管道

s: 套接字

例子:

[[email protected] tmp]# find /tmp/ -type l -ls
654205    0 lrwxrwxrwx   1 root     root           22 8月 25 21:24 /tmp/grub.conf -> ../boot/grub/grub.conf
[[email protected] /]# find /tmp/ -type d -ls
915714    4 drwxr-xr-x   9 root     root         4096 9月  3 18:09 /tmp/
915721    4 drwxr-xr-x   2 root     root         4096 9月  3 18:09 /tmp/89123jk4
915715    4 drwxr-xr-x   2 root     root         4096 9月  3 18:09 /tmp/jkashd
915719    4 drwxr-xr-x   2 root     root         4096 9月  3 18:09 /tmp/sdfhk
915718    4 drwxr-xr-x   2 root     root         4096 9月  3 18:09 /tmp/1892373
915717    4 drwxr-xr-x   2 root     root         4096 9月  3 18:09 /tmp/kjashd
915720    4 drwxr-xr-x   2 root     root         4096 9月  3 18:09 /tmp/90124h3kjshdr
915716    4 drwxr-xr-x   2 root     root         4096 9月  3 18:09 /tmp/kjashdkj
  • 组合查找条件:

与条件:-a

或条件:-o

非条件:-not, !

!A -o !B = !(A -a B)

!A -a !B = !(A -o B)

例子:

[[email protected] tmp]# find /tmp/ -type f -a -nouser -ls
915724    4 -rw-------   1 3012     hadoop        827 8月 30 15:46 /tmp/grub.conf
[[email protected] tmp]# find /tmp/ \( -nouser -o -user lisir \) -ls
915724    4 -rw-------   1 3012     hadoop        827 8月 30 15:46 /tmp/grub.conf
915723    4 -rw-r--r--   1 lisir    lisir01       957 8月 25 13:35 /tmp/fstab
915722   20 -rw-r--r--   1 lisir    3012        19697 4月 10 00:44 /tmp/functions
[[email protected] tmp]# find /tmp/ \( -nouser -a -group hadoop \) -ls
915724    4 -rw-------   1 3012     hadoop        827 8月 30 15:46 /tmp/grub.conf
[[email protected] tmp]# find /etc ! -name "*.conf" -a -not -type f -ls 
392449   12 drwxr-xr-x 117 root     root        12288 9月  3 18:14 /etc
393096    4 drwxr-xr-x   2 root     root         4096 8月 25 21:12 /etc/purple
393295    0 lrwxrwxrwx   1 root     root           15 8月 25 21:13 /etc/rc.sysinit -> rc.d/rc.sysinit
394019    4 drwxr-xr-x   2 root     root         4096 8月 25 21:16 /etc/pcmcia
393055    4 drwxr-xr-x   2 root     root         4096 8月 25 21:12 /etc/obex-data-server
393034    4 drwxr-xr-x   3 root     root         4096 8月 25 21:11 /etc/ghostscript
393040    4 drwxr-xr-x   2 root     root         4096 8月 25 21:11 /etc/ghostscript/8.70
392886    4 drwxr-xr-x   2 root     root         4096 8月 25 21:11 /etc/wpa_supplicant
392787    4 drwxr-xr-x   2 root     root         4096 8月 25 21:11 /etc/ssl
  • 根据文件大小来查找:

-size [+|-]#UNIT

单位:k, M, G

+#UNIT: (#,+oo)

-#UNIT:[0,#-1]

例子:

[[email protected] /]# find /etc/ -size 3k -exec ls -lh {} \;
-rw-r--r--. 1 root root 2.9K 8月  21 2010 /etc/pinforc
-rwxr-xr-x. 1 root root 2.6K 4月  10 00:44 /etc/sysconfig/network-scripts/ifup-tunnel
-rwxr-xr-x. 1 root root 2.4K 4月  10 00:44 /etc/sysconfig/network-scripts/ifup-post
-rw-r--r--. 1 root root 2.2K 7月  24 16:22 /etc/sysconfig/nfs
-rw-r--r--. 1 root root 2.6K 7月  24 19:32 /etc/sysconfig/cpuspeed
-rw-r--r--. 1 root root 2.6K 5月  20 22:18 /etc/sysconfig/raid-check
-rw-------. 1 root root 2.3K 8月  25 21:25 /etc/lvm/backup/VolGroup
-r--r--r--. 1 root root 2.2K 7月  24 12:04 /etc/lvm/profile/command_profile_template.profile
  • 根据时间戳:

(1)以“天”为单位

-atime [+|-]#

:[#,#+1)

+#: [#+1,oo]

-#: [0,#)

#:

-mtime

-ctime

+3大于三天 -3三天以内  3大于等三4天以内

(2)以“分钟”为单位

-amin

-mmin

-cmin

注:a表示最近访问文件的时间、m表示最近修改文件内容的时间、c表示最近文件有所改变的状态 ,如文件修改,属性\属主 改变 ,节点 ,链接变化等

例子:

[[email protected] /]# touch -m -t 201509031828 /tmp/lisir
[[email protected] /]# stat /tmp/lisir
  File: "/tmp/lisir"
  Size: 0               Blocks: 0          IO Block: 4096   普通空文件
Device: fd00h/64768d    Inode: 915725      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2015-09-03 18:28:23.640000731 +0800
Modify: 2015-09-03 18:28:00.000000000 +0800
Change: 2015-09-03 18:28:23.640000731 +0800
[[email protected] /]# find /tmp/ -mtime -3 -ls
915725    0 -rw-r--r--   1 root     root            0 9月  3 18:28 /tmp/lisir
  • 根据权限:

-perm [/|-]MODE

MODE: 精确权限匹配

/MODE或+MODE:任何一类对象(u,g,o)的任何一位权限符合条件即可;隐含或条件;

/444或+444:表示只要符合u,g,o任何一个权限有读取即可

-MODE:为每一类对象指定的每一位权限都必须同时存在方为符合条件;隐含与条件;

-444:表示必须符合u,g,o每一位权限都有读即可

例子:

[[email protected] soft]# find /soft/ -perm /222 -ls
2354690    4 drwxr-xr-x   2 root     root         4096 9月  3 18:38 /soft/
2354691    0 -rwx------   1 root     root            0 9月  3 18:38 /soft/a
2354692    0 -rwxrwx---   1 root     root            0 9月  3 18:38 /soft/b
2354693    0 -rwxrwxrwx   1 root     root            0 9月  3 18:38 /soft/c
2354694    0 -rw-r--r--   1 root     root            0 9月  3 18:38 /soft/d
2354695    0 -rw-r--r--   1 root     root            0 9月  3 18:38 /soft/e
[[email protected] soft]# find /soft/ -perm -222 -ls
2354693    0 -rwxrwxrwx   1 root     root            0 9月  3 18:38 /soft/c
[[email protected] soft]# find /soft/ ! -perm -222 -ls
2354690    4 drwxr-xr-x   2 root     root         4096 9月  3 18:38 /soft/
2354691    0 -rwx------   1 root     root            0 9月  3 18:38 /soft/a
2354692    0 -rwxrwx---   1 root     root            0 9月  3 18:38 /soft/b
2354694    0 -rw-r--r--   1 root     root            0 9月  3 18:38 /soft/d
2354695    0 -rw-r--r--   1 root     root            0 9月  3 18:38 /soft/e

5、处理动作:

-print: 默认处理动作;

-ls:类似于对查找到的每个文件做"ls -l"的操作;

-delete: 删除查找到的文件;

-fls /path/to/somefile:查找到的文件的详细路径信息保存至指定文件中;

-ok COMMAND {} \;

对每个文件执行指定的命令之前需要用户事先确认;

-exec COMMAND {} \;

无需用户确认;

例子:

[[email protected] soft]# find . -perm -222 -exec mv {} {}.a \;
[[email protected] soft]# ll
总用量 0
-rwx------. 1 root root 0 9月   3 18:43 a
-rwxrwx---. 1 root root 0 9月   3 18:43 b
-rwxrwxrwx. 1 root root 0 9月   3 18:43 c.a
[[email protected] soft]# find . -perm -222 -exec chmod 777 {} \;
[[email protected] soft]# ll
总用量 0
-rwx------. 1 root root 0 9月   3 18:43 a
-rwxrwx---. 1 root root 0 9月   3 18:43 b
-rwxrwxrwx. 1 root root 0 9月   3 18:43 c.a

练习:

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

答:find /var -user root -group mail -ls

2、查找/usr目录下不属于root、bin或hadoop的所有文件;
    答:find /usr -not -user root -a -not -user bin -a -not -user hadoop -ls

3、查找/etc目录下最近一周内其内容修改过,且属主不为root或hadoop的所有文件;
     答:find /etc -ctime -7 -a -not -user root -not -user hadoop -ls

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

答:find / \( -nouser -o -nogroup \) -a -atime -7 -ls

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

答:find /etc -type f -a -size +20k -exec ls -lh {} \;

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

答:find /etc -not -perm /222 -ls

7、查找/etc目录下至少有一类用户没有执行权限的文件;

答:find /etc -not -perm -111 -ls

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

答:find /etc/init.d/ -perm /111 -a -perm /002 -ls

时间: 2024-08-05 14:08:31

文件查找及find命令详解的相关文章

【linux_笔记】Linux_文件查找(find)详解&&特殊权限

学习资源来自:www.magedu.com 学习记录过程中难免出现错误,如有发现,还望大神们指出. 示例操作部分有的与历史操作有关,如果先前的示例操作没有执行过的话,可能会有部分示例的操作无法执行.示例仅供参考(练习题在附录). 文件查找: locate(不常用):非实时,模糊匹配,根据全系统文件数据库进行查找,速度快:# updatedb, 手动生成文件数据库(非常耗时) find:实时,精确,支持众多查找标准,遍历指定目录中的所有文件完成查找,速度慢: 命令格式:find 查找路径 查找标准

Protobuf 文件生成工具 Prototool 命令详解

Protobuf 文件生成工具 Prototool 命令详解 简介 Prototool 是 Protobuf 文件的生成工具, 目前支持go, php, java, c#, object c 五种语言包的生成. 详情参考Github: https://github.com/uber/prototool docker 方式使用 prototool 工具 使用方式 // prototool 的使用 docker run --rm -v $(pwd):/work "uber/prototool&quo

BAT批处理之文件与文件夹(附xcopy命令详解)

在批处理脚本中,操作文件与文件夹的方法,包括文件的复制.删除.移动等操作,并附有xcopy命令的详细用法.原文地址:http://www.jbxue.com/article/14535.html 批处理中的文件.文件夹操作,xcopy命令的用法. 一,建bat文件自动执行复制,删除命令.例1:复制cd.dll文件至windows\system32的bat文件内容:copy cd.dll %windir%\system32例2:卸载windows\system32目录中的cd.dll,即把上面复制

[转] - JAR文件包及jar命令详解 ( MANIFEST.MF的用法 )

常常在网上看到有人询问:如何把 java 程序编译成 .exe 文件.通常回答只有两种,一种是制作一个可执行的 JAR 文件包,然后就可以像. chm 文档一样双击运行了:而另一种是使用 JET 来进行 编译.但是 JET 是要用钱买的,而且据说 JET 也不是能把所有的 Java 程序 都编译成执行文件,性能也要打些折扣.所以,使用制作可执行 JAR 文件包的方法就是最佳选择了,何况它还能保持 Java 的跨平台特性. 下面就来看看什么是 JAR 文件包吧: 1. JAR 文件包 JAR 文件

文本查找工具 find命令详解

本文旨在学习find命令常用用法 find命令语法 find [option] [查找路径] [查找条件][处理动作] 查找路径:指定具体目标路径:默认为当前目录. 查找条件:指定的查找标准,可以是文件名.大小.类型.权限等:默认会找出指                   定路径下的所有文件. 处理动作:对符合条件的文件做操作,默认输出至屏幕 find查找条件详解 根据文件名和inode查找: -name <文件名称>:支持使用glob *, ?, [], [^] -iname <文件

文件搜索者--find命令详解

1. 文件查找: 在linux系统中由于文件的众多,往往需要在众多的文件当中查找某一个文件,如果时间一长,很难记得文件存放至何处,不过,这一点,你不比担心,因为开发人员为我们提供了强大的文件搜索工具,下面将介绍两款常用的文件查找工具locate,和find,这两款查找工具只能是从文件系统中查找不能在内存当中查找,因为内存是没有文件系统的. 2. locate命令: locate命令是基于数据库来查找文件的,命令查找速度快,一般查找跟新慢的,名字变化少的,如:配置文件,可以用locate查找,因为

在Linux里,防止文件被删除(chattr命令详解)

1. 概述 linux中键入"man chattr",其描述为"chattr changes the file attributes on a Linux file system."从这我们很容易得出:在Linux文件系统里,chattr是可以改变文件的属性. 2. 具体的语法格式 chattr [ -RVf ] [ -v version ] [ mode ] files...[1] 注:中括号是可选选项,而 files 表明其为参数,必须要有.如果您加上版本号(-

搜索系统中所有以.repo结尾的文件并删除(find命令详解及xargs命令详解)

find / -name *.repo | xargs rm –rf //搜索以.repo结尾的文件并删除 find命令详解 精细查找文件或目录d find [ 查找范围 ] [ 查找条件表达式 ] 常用查找条件: 按名称查找:"-name",支持通配符"*"."?" 按文件大小查找:"-size",可使用"+"."-"作为容量条件 按文件属主查找:"-user" 按

BAT批处理之文件与文件夹操作代码(附xcopy命令详解)

批处理中的文件.文件夹操作,xcopy命令的用法. 一,建bat文件自动执行复制,删除命令. 例1:复制cd.dll文件至windows\system32的bat文件内容: copy cd.dll %windir%\system32 例2:卸载windows\system32目录中的cd.dll,即把上面复制的文件删除: del %windir%\system32\cd.dll 例3:删除download文件夹中的文件,例子如下: del C:\DOWNLOAD\*.* 注意,以处的del命令只