文本查找工具find命令

在Linux系统上操作时,有时会忘记一个文件放在哪个目录下,这是可以用到文件查找工具去查找到相应的文件,文件查找工具有locate、find两个工具

文件查找:

1.locate:   locate命令是通过linux上构建的一个索引数据库来查询的,遍历整个数据库去搜索所匹配的文件,而且这个索索引数据库是Linux系统自己维护和更新的,索引的构建是在系统比较空闲的时候自动进行的周期性任务。

虽然数据库的更新是系统自动更新的,但也可以通过手动updatedb更新,但是更新的时候需要遍历整个根目录(/),所以极消耗系统资源。

locate的特点:查找速度快,模糊查询,非实时查找

[[email protected] nginxlog]# locate passwd

/etc/passwd

/etc/passwd-

/etc/pam.d/passwd

.....

locate命令虽然查找快,但是如果你创建了一个文件,索引数据还没更新,文件是查找不到的。

[[email protected] nginxlog]# touch mageedu

[[email protected] nginxlog]# ls

mageedu  nginx-1.4.7.tar.gz  test

[[email protected] nginxlog]# locate mageedu

[[email protected] nginxlog]#

注意:有的centos6没有locate命令,可以yum -y install mlocate,然后updatedb下。

2、find:   find是实时查找工具,通过遍历指定路径下的文件系统完成文件查找。

特点:查找速度略慢,实时查找,精确查找

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

查找路径:指定具体的目标路径,默认为当前当前路径

查找条件:可以查找文件名,大小,类型,权限,文件修改时间等

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

2.1、查找条件:

根据文件名查找:-name,支持用global(通配符);-iname表示不区分大小写

[[email protected] nginxlog]# find /etc -name "*passwd"

根据属主、属组查找:-user,-group,-uid,-gid,-nouser,-nogroup

[[email protected] nginxlog]# find /etc -user root

根据文件类型去查找:

-type TYPE: -d,-f,-l,-s(套接字文件) ,-p(管道文件),-c,b

[[email protected] nginxlog]# find /etc -type f

根据文件大小去查找:

-size [+|-]#unit:  #表示大小

[[email protected] nginxlog]# find /etc -size +2M

注意:+2M表示大于2M的文件;2M表示是1M-2M的文件;-2M表示小于2M的文件,即0M-1M;

根据时间去查找:

-atime [+|-]#:  #表示天数       注意:这里天数是按24小时计算!

[[email protected] ~]# find /etc -atime 3 -type f

例子中3表示查找以前“第三天”的文件,如果+3表示查找“第三天“以前的文件,如果-3表示查找

“第三天”到现在的时间;

已分钟为单位:

-amin、mmin、cmin

根据权限查找:

-perm [-|/] mode

mode:表示精确匹配查找,必须按给定的权限查找文件。

/mode:表示只要ugo当中有一个符合就可以

-mode:ugo同时都必须拥有为其指定的权限,ugo中只要权限位包含所指定的权限就可以。                                        例如:

[[email protected] tmp]# find /tmp -perm 755 -ls

101319180    0 -rwxr-xr-x   1 root     root            0 Mar 12 18:01 /tmp/test.txt

[[email protected] tmp]# find /tmp -perm /755 -ls

100663426    0 -rw-------   1 root     root             0 Mar 11 15:49 /tmp/yum.log

101319179    0 -rwxrwxrwx   1 root     root            0 Mar 12 17:57 /tmp/test.xtx

101319180    0 -rwxr-xr-x   1 root     root            0 Mar 12 18:01 /tmp/test.txt

这里出现rm-也符合是因为0可以表示-或者r或者w或者x。

[[email protected] tmp]# find /tmp -perm -755 -ls

101319179    0 -rwxrwxrwx   1 root     root            0 Mar 12 17:57 /tmp/test.xtx

101319180    0 -rwxr-xr-x   1 root     root            0 Mar 12 18:01 /tmp/test.txt

r-x是包含在rwx当中的

举例:

找出/etc目录下都没有写权限的文件或目录;

[[email protected] tmp]# find /etc -not -perm /222 -ls

33852189    4 -r--r--r--   1 root     root           33 Mar 11 15:52 /etc/machine-id

34203544    4 ----------   1 root     root          370 Mar 11 15:58 /etc/gshadow

34203344    4 ----------   1 root     root          717 Mar 11 15:58 /etc/shadow

只要把ugo当中任何一个有写权限的都找出来,然后执行取反操作。

找出/etc目录下至少有一部分没有执行权限的文件或目录;

[[email protected] tmp]# find /etc -not -perm -111 -ls

33554562    4 -rw-r--r--   1 root     root          465 Mar 11 15:49 /etc/fstab

33554563    0 -rw-------   1 root     root            0 Mar 11 15:49 /etc/crypttab

34323086    4 -rw-r--r--   1 root     root           70 Mar 12 17:13 /etc/resolv.conf

只要把ugo当中都有执行权限的用户找出,然后执行取反操作。

找出/tmp目录下都有执行权限,且其它用户有写权限的文件或目录;

[[email protected] tmp]# find   -perm -113 -ls

101319179    0 -rwxrwxrwx   1 root     root            0 Mar 12 17:57 /tmp/test.xtx

101319180    0 -rwxr-xrwx   1 root     root            0 Mar 12 18:01 /tmp/test.txt

find命令中的组合用法:

与:-a

或:-o

非:-not, !            注意:优先级是或和与>非的,-o和-a>-not

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

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

找出/tmp目录下,属主不是root,且文件名不是fstab的文件;两种写法:

[[email protected] tmp]#  find /tmp \( -not -user root -a -not -name ‘fstab‘ \) -ls

[[email protected] tmp]#  find /tmp -not \( -user root -o -name ‘fstab‘ \) -ls

find的处理动作:

-print:默认的处理动作,显示至屏幕;

-ls:类似于对查找到的文件执行“ls -l”命令;

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

-fls /path/to/somefile:查找到的所有文件的长格式信息保存至指定文件中;

-ok COMMAND {} \; 对查找到的每个文件执行由COMMAND指定的命令;

对于每个文件执行命令之前,都会交互式要求用户确认;

-exec COMMAND {} \; 对查找到的每个文件执行由COMMAND指定的命令;

{}: 用于引用查找到的文件名称自身;

注意:find传递查找到的文件至后面指定的命令时,查找到所有符合条件的文件一次性传递给后面的命令;

有些命令不能接受过多参数,此时命令执行可能会失败;另一种方式可规避此问题:

find |xargs COMMADN

例子:

1、[[email protected] tmp]# find /tmp   -name ‘test*‘ -print

/tmp/test.xtx

/tmp/test.txt

2、 [[email protected] tmp]# find /tmp   -name ‘test*‘ -ls

101319179    0 -rwxrwxrwx   1 root     root            0 Mar 12 17:57 /tmp/test.xtx

101319180    0 -rwxr-xrwx   1 lanxt    lanxt           0 Mar 12 18:01 /tmp/test.tx

3、 [[email protected] tmp]# find /tmp   -name ‘test*‘ -delete

[[email protected] tmp]# ll

total 4

-rwx------. 1 root root 813 Mar 11 15:59 ks-script-_JF4cv

-rw-------. 1 root root   0 Mar 11 15:49 yum.log

4、[[email protected] ~]# find /tmp -name ‘t*‘ -fls /root/rm.log

[[email protected] ~]# cat /root/rm.log

101319179    0 -rw-r--r--   1 root     root            0 Mar 12 19:14 /tmp/test

101319180    0 -rw-r--r--   1 root     root            0 Mar 12 19:14 /tmp/txt

5、[[email protected] ~]# find /tmp -name ‘t*‘ -ok rm {}  \;

< {} ... /tmp/test > ?

6、[[email protected] tmp]# find /tmp -name ‘t*‘ -type f  -exec rm {}   \;

[[email protected] tmp]# ll

-rwx------. 1 root root 813 Mar 11 15:59 ks-script-_JF4cv

-rw-------. 1 root root   0 Mar 11 15:49 yum.log

7、 [[email protected] tmp]# find /tmp -name "test*" -o -user root -o -user lanxt -ls

[[email protected] tmp]#

[[email protected] tmp]# find /tmp -name "test*" -o -user root -o -user lanxt |xargs ls -l

-rw-r--r--. 1 root  root    0 Mar 12 19:26 /tmp/test

-rw-r--r--. 1 lanxt root    0 Mar 12 19:26 /tmp/test1

-rw-r--r--. 1 root  root    0 Mar 12 19:26 /tmp/test2

练习:用脚本实现,每天晚上4点半,找出/nginxlog下的格式为“nginx_testXX.log”的日志,如果该日志大于2M,则将其删除,删除后,把删除的文件名记录到/tmp/rm.log。

每天晚上4点半:可以用crontab定时计划实现

30 4 * * *

找出大于2M的日志记录到/tmp/rm.log下:

find /nginxlog -name  "nginx*.log" -size +2M  &>/tmp/rm.log  或者

find /nginxlog -name  ‘nginx*.log‘ -size +2M  -fls /tmp/rm.log  或者

find /nginxlog -name  ‘nginx*.log‘ -size +2M  |tee /tmp/rm.log

然后将其删除:

cat /tmp/rm.log |xargs rm

cat /tmp/rm.log |grep -o ‘/[^[:space:]]\+‘ |xargs rm

find /nginxlog -name  ‘nginx*.log‘ -size +2M  |tee /tmp/rm.log |xargs rm

注意:如果是在工作中,必须先记录,在执行操作

   
				
时间: 2024-08-06 03:45:23

文本查找工具find命令的相关文章

文本查找工具 find命令详解

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

grep 文本查找工具

简介 grep一种文本字符查找工具,会以行为单位查找可以匹配字符的行,并且默认情况下显示整行. 我们所要做的就是:书写特定的字符,来匹配我们想要的内容. 字符分为两类: 一就是普通的字符,二是一种我们称之为'元字符'的具有特殊意义的高上大字符. 而这种由普通字符或元字符所组合起来的,用于表达.匹配特定字符的字符串, 我们就称之为 "正则表达式""正则表示法"等等...  正则表达有很多规范,这里只说grep用的, 因为别的也不会. 对了要注意一点: 元字符跟通配符是

Linux中文本搜索工具“grep”命令详解

人生中应该有两次冲动,一次是轰轰烈烈的爱情,一次是说走就走的旅行.我属于第三种,轰轰烈烈的辞职然后马不停蹄的参加了马哥的linux面授班,两周时间内对linux有了些初步的了解,与此同时,两周内八天课程中的各种定义.概念.原理.命令由于量大.琐碎,也着实给我们这些小白一个下马威,即使每天11点撤退也觉得时间不够用.鉴于对自己的负责和马哥的作业要求,特对文本搜索工具grep写些使用指南,其中可能会有描述不确切或有偏颇之处,望及时指正. 开始--. 首先,要说一下grep是什么,用在什么地方. gr

centos 7中的文本处理工具sed命令的使用

sed是一个强大而简答的文本解析转换工具,可以读取文本,并根据指定的条件对文本内容进行编辑(删除.替换.添加.移动等),最后输出所有行或者仅输出处理的某些行.sed也可以在无交互的情况下实现相当复杂的文本处理操作,被广泛应用于shell脚本中,用以完成各种自动化处理任务.sed的工作流程主要包括读取.执行和显示三个过程: 读取:sed从输入流(文件.管道.标准输入)中读取一行内容并存储到临时的缓冲区中(也称为模式空间). 执行:默认情况下,所有的sed命令都在模式空间中顺序地执行,除非指定了行的

Linux下文本搜索工具grep命令使用入门

grep命令入门 如果想通过使用grep命令来实现理想化的文本搜索,对正则表达式的了解是比不可少的.文献1对正则表达式语法做了一个简单的介绍,文献2提供了一个简单的入门.码农也可以自己google一下其他的参考资料.下面就grep命令的使用做个入门级的介绍. 1.1 grep命令的变种 linux下除了grep命令可以完成文本搜索外,还存在egrep,fgrep,rgrep三个命令.这三个命令都是由grep加上一些控制参数演变而来,如egrep=grep -E, fgrep=grep -F, r

linux命令:find文件查找工具 locate命令查找

     find命令简介: 实时.精确.支持众多查找标准 遍历指定目录中的所有文件完成查找,速度慢: 1.命令格式: locate filename 直接跟需要查找的文件  eg locate passwd  系统文件数据库中所有 包含passwd的文件. find 查找路径 查找标准 查找到以后的处理动作  查找路径:默认为当前目录  查找标准:默认为指定路径下的所有文件  处理动作:默认为显示到屏幕上 eg:find /etc -name passwd  表示在etc目录下查找名字为pas

文本查找工具find

which:查找二进制或执行命令路径     [[email protected] ~]# which free    结果: /usr/bin/free locate:非实时的 模糊查询 每日更新 (通过一个内置数据库,系统文件在数据库中有一个映射,瞬间查找前一天夜里自动更新的目标文件) -b 匹配基名 -c 统计有多少符合条件 updatedb 手动更新 find:实时.文件遍历.精确匹配.速度略慢 -a :与 -o :或 -not:非 优先级最低,结合使用要使用分组 \( \) -fls

文本处理工具printf命令

使用过c语言的程序员,应该很熟悉printf命令,printf命令模仿了c语言的printf( )函数 printf命令的作用是按照我们指定的格式输出文本 输出文本命令echo和printf有什么区别看到echo 命令是正常输出一个文本进行换行,而printf是输出文本进行不换行 [[email protected] ~]# echo beijing beijing [[email protected] ~]# printf beijing beijing[[email protected] ~

文件查找工具locate与find

写在前面: 博客书写牢记5W1H法则:What,Why,When,Where,Who,How. 本篇主要内容: ● locate用法 ● 使用find基于文件属性进行查找 locate与find简介: locate与find都是文件查找工具(区别与grep等文本查找工具),尤其是find,可根据文件的诸多属性(如文件大小.属主属组.权限信息.修改时间等),对文件进行实时查找 locate: 1.查找默认数据库文件/var/lib/mlocate/mlocate.db,所以查找速度快,非实时,每天