linux之find

作用

  根据文件的名称或属性查找文件

格式

  find [查找范围] [参数]

参数

  1、按照文件的名字查找

      -name

      -iname (忽略大小写)

      可配合的

        *  :通配符

1、[root@localhost ~]# find /etc/ -name 'hosts'
   /etc/hosts
2、find /etc/ -name '*hosts*'

  2、按照文件的大小查询文件

    -size

    可以配合的

        +      表示大于

        -      表示小于

       没有符号  就是等于

1、[root@localhost ~]# find /root -size +1M
2、[root@localhost ~]# find /root -size -1M
3、[root@localhost ~]# find /root -size 1000M

  扩展:快速生成一个指定大小的文件

[root@localhost ~]# dd if=/dev/zero of=4.txt bs=10M count=100
dd : 生成文件
    if         :从什么地方读
    of         : 写入到什么文件
    bs         : 每次写入多少内容
    count     : 写入多少次

  3、按照时间来查找

    -mtime  :按照修改文件的时间来查找

    -ctime  :按照文件创建的时间来查找

    -atime  :按照访问时间来查找

    可配合的:

        +:查询某个时间段之前的数据

        -:查询某个时间段之内的数据

1、[root@localhost ~]# find /root -mtime +2
2、[root@localhost ~]# find /root -mtime -1

  4、按照文件的权限位来查找

    -user  :按照用户的属主来查找

    -group     :按照用户的属组来查找

1、[root@localhost ~]# find /root -user root
2、[root@localhost ~]# find /root -group test01

  5、按照文件的类型查询

    -type

    文件类型有

d : 文件夹
l : 链接文件
s : 套接字文件
p : 管道文件
c : 字符文件
b :磁盘文件
f : 普通文件
[root@localhost ~]# find /root -type f

  6、按照文件的权限查找

    -perm(权限是按照数字来查找的)

[root@localhost ~]# find /root -perm 644

  7、按照index node号码来查询

    -inum

[root@localhost ~]# find /root -inum 134946661

  8、配合参数(不可以单独使用)

    -a  :并且

    -o  :或许

    -maxdepth  :查询的目录深度(必须放置与第一个参数位)

    -exec : 将find处理好的结果交给其他命令继续处理。

 案例

要求把/etc目录下,所有的普通文件打包压缩到/tmp目录
[root@localhost /tmp]# tar -czPf /tmp/etcv2.tar.gz `find /etc/ -type f | xargs`

  知识扩展

    |  :前面一个命令的结果交给后面一个命令处理

    xargs :把处理的文本变成以空格分割的一行

    ` ` :提前执行命令,然后讲结果交给其他命令来处理

 

posted @ 2021-12-20 20:12  那就凑个整吧  阅读(68)  评论(0编辑  收藏  举报