Linux文件查找find和locate

1 locate文件查找    1

1.1 概述    1

1.2 locate文件查找的特性    1

2 文件查找概述    1

3    1

3.1 文件名查找    1

3.2 文件大小查找    1

3.3 时间戳查找    1

3.4 文件从属关系查找    1

3.5 文件类型查找    1

3.6 权限查找    1

3.7 组合查找    1

3.8 处理动作    1

 

  1. locate文件查找

  2. 概述

安装

    yum install -y mlocate

locate

    -c:符合条件的文件数量

    -b:基名查找(按文件名称关键字查找,而不是文件路径关键字)

locate依赖于事先构建好的索引库

系统自动实现(周期性)

手动更新数据库(updatedb)

  1. locate文件查找的特性

1、查找速度快

2、模糊查找

3、不是实时更新

  1. 文件查找概述

Linux系统中的find命令在查找文件是非常有用而且方便

他可以根据不同条件来进行查找文件:例如权限,拥有者,修改时间,文件大小等等。同时find是linux下必须掌握的。

find命令的基本语法如下:

命令

选项

路径

表达式

动作

find

[options]

[path]

[expression]

[action]

 

  1. 文件名查找

-name:区分大小写

-iname:忽略大小写

    通配符:*和?    *:匹配多个任意字符 ?:匹配一个任意字符

-regex:基本正则表达式模糊查找文件,匹配是整个路径,而非其名;

区分大小写

[root@oldboy pubilc]# find /pubilc/ -name "ifcfg-ens33"

/pubilc/ifcfg-ens33

不区分大小写

[root@oldboy pubilc]# find /pubilc/ -iname "ifcfg-ens33"

/pubilc/ifcfg-ens33

/pubilc/IFCFG-ENS33

  1. 文件大小查找

-size[+|-] #UNIT

    常用单位:k、m、g

查找大于10m的文件

[root@oldboy pubilc]# find /pubilc/ -size +10M -ls

33587283 15360 -rw-r--r-- 1 root root 15728640 8月 23 16:22 /pubilc/file3.txt

33587284 12288 -rw-r--r-- 1 root root 12582912 8月 23 16:23 /pubilc/file2.txt

查找等于10M的文件

[root@oldboy pubilc]# find /pubilc/ -size 10M -ls

33587282 10240 -rw-r--r-- 1 root root 10485760 8月 23 16:21 /pubilc/file1.txt

查找小于10M的文件

[root@oldboy pubilc]# find /pubilc/ -size -10M -ls

33587264 0 drwxr-xr-x 4 root root 111 8月 23 16:22 /pubilc/

51095307 0 drwxr-xr-x 2 root root 6 8月 23 16:11 /pubilc/ifcfg-ens33

619630 0 drwxr-xr-x 2 root root 6 8月 23 16:11 /pubilc/IFCFG-ENS33

33587281 6144 -rw-r--r-- 1 root root 6291456 8月 23 16:21 /pubilc/file.txt

  1. 时间戳查找

以"天"为单位

    -atime[-|+]#

        #:之前第#天的文件

        -#:七天内的文件

        +#:七天之前的文件

以分钟为单位:

    -amin:

    -mmin

    -cmin:

创建文件测试文件

[root@oldboy pubilc]# for i in {01..31};do date -s 201808$i && touch file-$i;done

查找20以前的文件

[root@oldboy pubilc]# find /pubilc/ -atime +20 -ls

33587281 0 -rw-r--r-- 1 root root 0 8月 1 00:00 /pubilc/file-01

33587282 0 -rw-r--r-- 1 root root 0 8月 2 00:00 /pubilc/file-02

33587283 0 -rw-r--r-- 1 root root 0 8月 3 00:00 /pubilc/file-03

33587284 0 -rw-r--r-- 1 root root 0 8月 4 00:00 /pubilc/file-04

查找5天以内的文件

[root@oldboy pubilc]# find /pubilc/ -atime -5 -ls

33587264 4 drwxr-xr-x 2 root root 4096 8月 31 00:00 /pubilc/

33588261 0 -rw-r--r-- 1 root root 0 8月 27 00:00 /pubilc/file-27

33588262 0 -rw-r--r-- 1 root root 0 8月 28 00:00 /pubilc/file-28

33588263 0 -rw-r--r-- 1 root root 0 8月 29 00:00 /pubilc/file-29

33588264 0 -rw-r--r-- 1 root root 0 8月 30 00:00 /pubilc/file-30

33588265 0 -rw-r--r-- 1 root root 0 8月 31 00:00 /pubilc/file-31

查找前5天,当天的文件

[root@oldboy pubilc]# find /pubilc/ -atime 5 -ls

33588260 0 -rw-r--r-- 1 root root 0 8月 26 00:00 /pubilc/file-26

  1. 文件从属关系查找

-user:查找属主指定用户的所有文件;

-group:查找属组指定组的所有文件;

-uid:查找属主指定UID的所有文件

-gid:查找属组指定的GID的所有文件

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

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

查找属主是oldboy的文件

[root@oldboy pubilc]# find / -user oldboy 2>/dev/null

28341 4 -rw-rw-r-- 1 oldboy oldboy 445 8月 22 21:11 /home/oldboy/2.txt

619618 0 -rw-rw-r-- 1 oldboy oldboy 0 8月 22 21:21 /home/oldboy/ab

451578 4 -rw-rw-r-- 1 oldboy oldboy 93 8月 22 21:32 /home/oldboy/ping.sh

451575 4 -rw------- 1 oldboy oldboy 2427 8月 22 21:32 /home/oldboy/.viminfo

查找属组是oldboy的文件

[root@oldboy pubilc]# find / -group oldboy -ls

28318 12 -rw------- 1 oldboy oldboy 12288 8月 22 10:01 /home/oldboy/.123.swp

28319 0 -rw-rw-r-- 1 oldboy oldboy 0 8月 22 21:21 /home/oldboy/1.txt

28341 4 -rw-rw-r-- 1 oldboy oldboy 445 8月 22 21:11 /home/oldboy/2.txt

查找UID是的文件

[root@oldboy pubilc]# find / -uid 1000 -ls 2>/dev/null

25716 4 -rw-r--r-- 1 oldboy oldboy 231 4月 11 08:53 /home/oldboy/.bashrc

25719 4 -rw------- 1 oldboy oldboy 1115 8月 22 21:50 /home/oldboy/.bash_histo

查找GID是的文件

[root@oldboy pubilc]# find / -gid 1000 -ls 2>/dev/null

25714 4 -rw-r--r-- 1 oldboy oldboy 18 4月 11 08:53 /home/oldboy/.bash_logout

25715 4 -rw-r--r-- 1 oldboy oldboy 193 4月 11 08:53 /home/oldboy/.bash_profi

查找没有属主的文件

[root@oldboy pubilc]# find / -nouser 2>/dev/null -ls

619630 0 -rw-rw---- 1 1002 mail 0 8月 31 00:08 /var/spool/mail/aaa

17563463 0 drwx------ 2 1002 1002 62 8月 31 00:08 /home/aaa

17563476 4 -rw-r--r-- 1 1002 1002 18 4月 11 08:53 /home/aaa/.bash_logout

17563511 4 -rw-r--r-- 1 1002 1002 193 4月 11 08:53 /home/aaa/.bash_profile

17563512 4 -rw-r--r-- 1 1002 1002 231 4月 11 08:53 /home/aaa/.bashrc

33587290 0 -rw-r--r-- 1 1002 1002 0 8月 10 00:00 /pubilc/file-10

33574983 0 -rw-r--r-- 1 1002 1002 0 8月 16 00:00 /pubilc/file-16

查找没有属组的文件

[root@oldboy pubilc]# find / -nogroup 2>/dev/null -ls

17563463 0 drwx------ 2 1002 1002 62 8月 31 00:08 /home/aaa

17563476 4 -rw-r--r-- 1 1002 1002 18 4月 11 08:53 /home/aaa/.bash_logout

17563511 4 -rw-r--r-- 1 1002 1002 193 4月 11 08:53 /home/aaa/.bash_profile

17563512 4 -rw-r--r-- 1 1002 1002 231 4月 11 08:53 /home/aaa/.bashrc

33587290 0 -rw-r--r-- 1 1002 1002 0 8月 10 00:00 /pubilc/file-10

33574983 0 -rw-r--r-- 1 1002 1002 0 8月 16 00:00 /pubilc/file-16

  1. 文件类型查找

-type

    f:普通文件

    d:目录文件

    l:链接文件

    c:字符设备文件

    p:管道文件

    b:块设备文件

普通文件

find /public -type f

目录

find /public -type d

链接文件

find /public -type l

管道文件

find /run -type p

块设备

find /dev -type b

字符设备

find /dev -type c

套接字文件

find /dev -type s

  1. 权限查找

-perm [/|-] mode

    mode:精确权限匹配;

    /mode:任何一类用户(g、u、o)的权限中的任何一位(r、w、x)符合条件即满足;

        9位权限位之间存在"或"关系;

    -mode:每一类用户(g、u、o)的权限中的每一位(r、w、x)同时符合条件即满足;

        9位权限位之间存在"与"关系;

 

完全匹配444权限

拥有者至少有r— 组至少有r—其他人至少有r--

[root@oldboy pubilc]# find /pubilc/ -perm -444 -ls

  1. 组合查找

与:-a(默认)

或:-o

非:-not,!

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

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

在/public目录下查找属于属于oldboy用户并且属于oldgirl组的文件

[root@oldboy pubilc]# find /pubilc/ -user oldboy -a -group oldgirl -ls

33587284 0 -rw-r--r-- 1 oldboy oldgirl 0 8月 4 00:00 /pubilc/file-04

33587285 0 -rw-r--r-- 1 oldboy oldgirl 0 8月 5 00:00 /pubilc/file-05

33587286 0 -rw-r--r-- 1 oldboy oldgirl 0 8月 6 00:00 /pubilc/file-06

33587287 0 -rw-r--r-- 1 oldboy oldgirl 0 8月 7 00:00 /pubilc/file-07

在/public目录下查找属于oldboy或者属于oldgirl的文件

[root@oldboy pubilc]# find /pubilc/ -user oldboy -o -user oldgirl -ls

33587288 0 -rw-r--r-- 1 oldgirl oldboy 0 8月 8 00:00 /pubilc/file-08

  1. 处理动作

当查找到一个文件后,需要对文件进行处理

    -print:输出到标准输出,默认的动作;

    -ls:类似于对查找的文件执行"ls -l"命令,输出文件的详细信息;

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

    -fls:/PATH/TO/SOMETILE:把查找到的所有文件的长格式信息保存至指定文件中;

    -ok COMMAND {} \; :对查找的每个文件执行有COMMAND表示的 命令,每次操作都由用户进行确认;

    -exec COMMAND {} \; :对查找的每个文件执行由COMMAND表示的命令,不需要确认;

打印查询到的文件

[root@oldboy pubilc]# find /pubilc/ -name "file-*"

/pubilc/file-01

/pubilc/file-02

/pubilc/file-03

[root@oldboy pubilc]# find /pubilc/ -name "file-*" -print

/pubilc/file-01

/pubilc/file-02

/pubilc/file-03

拷贝文件

[root@oldboy pubilc]# find /etc/sysconfig/ -name "ifcfg-ens33" -exec cp {} ./ \;

[root@oldboy pubilc]# find /etc/sysconfig/ -name "*ens33" -ok cp {} ./ \;

< cp ... /etc/sysconfig/network-scripts/ifcfg-ens33 > ? y

删除文件

[root@oldboy pubilc]# find /pubilc/ -name "file-01" -exec rm {} \;

本地文件保留最近7天的备份,备份服务器保留3个月

[root@oldboy pubilc]# find /pubilc/ -mtime +7 -delete

[root@oldboy pubilc]# find /pubilc/ -mtime +30 -delete

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/majinhai/p/9534103.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值