工作中常用的Linux命令:find命令

时间:2023-03-08 22:20:02
工作中常用的Linux命令:find命令

本文链接:http://www.cnblogs.com/MartinChentf/p/6056571.html (转载请注明出处)

1.命令格式

  find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]

2. 命令功能

  在文件目录层级中查找文件并做相应的处理

3. 命令选项

  -name finename

    按照文件名查找文件,文件名可使用通配符

  -perm mode

    按照文件权限查找文件

  -type c

    按照文件类型查找文件,文件类型如下:

    b - 块设备文件
    c - 字符设备文件
    d - 目录文件
    p - 管道文件
    f  - 普通文件
    l  - 符号链接文件
    s - 套接字文件

  -size [+/-]n[cwbkMG]

    按照文件大小查询文件,+n表示文件大小大于n,-n表示文件大小小于n

    文件大小单位如下:

     'b'  512-byte 的块

     'c'  字节单位

     'w'  字单位

     'k'  KB(210 bytes)

     'M'  MB(220 bytes)

     'G'  GB(230 bytes)

  -gid n

    按照文件属组ID查找文件

  -group gname

    按照文件属组名查找文件,也可用文件属组ID

  -uid n

    按照文件所有者ID查找文件

  -user uname

    按照文件所有者名查找文件,也可用文件所有者ID

  -amin n

    查找最后n分钟被访问过的文件

  -atime n

    查找最后n*24小时被访问过的文件

  -cmin n

    查找最后n分钟文件状态被改变的文件

  -ctime n

    查找最后n*24小时文件状态被改变的文件

  -mmin n

    查找最后n分钟文件数据被修改过得文件  

  -mtime n

    查找最后n*24小时文件数据被修改过得文件

  -newer file

    查找文件修改时间比文件file新的文件

  -anewer file

    查找文件访问时间比文件file新的文件

  -cnewer file

    查找文件状态改变的时间比文件file新的文件

  查找到文件后的相应处理动作:

  -delete

    删除查找到的文件

  -exec command {} \;

    对查找到的文件执行指定命令。{}和\;之间必须有空格,'\'为转义字符

  -ok command {} \;

    用法同exec,但在执行命令前会进行确认

  -print

    将文件输出到标准输出

  -printf format

    将查找到的文件按照format格式输出到标准输出

4. 实例

  实例1:在当前目录查找指定文件

[martin@localhost perl]$ find . -name "*.pl"
./ex3/ex3-.pl
./ex3/ex3-.pl
./ex3/ex3-.pl
./ex4/ex4-.pl

  实例2:查找当前目录下的普通文件

[martin@localhost perl]$ find . -type f
./ex3/-.txt
./ex3/-.txt
./ex3/ex3-.pl
./ex3/ex3-.pl
./ex3/ex3-.pl
./ex3/-.txt

  实例3:查找文件权限为775,并且文件大小超过4000byte的文件

[martin@localhost change]$ find . -perm  -size +4000c  
./dos2unix.pl
./test.pl
./space2tab.pl

  实例4:备份查找到的文件

[martin@localhost data]$ ll
total
-rw-rw-r--. martin martin Aug : in4_G_002_224001_12345667789.s
[martin@localhost data]$ find . -name "*.s" -exec cp {} {}.old \;
[martin@localhost data]$ ll
total
-rw-rw-r--. martin martin Aug : in4_G_002_224001_12345667789.s
-rw-rw-r--. martin martin Nov : in4_G_002_224001_12345667789.s.old

  实例5:删除查找到的文件前进行确认

[martin@localhost data]$ find . -name "*.old" -ok rm {} \;
< rm ... ./in4_G_002_224001_12345667789.s.old > ? y