find命令的简单使用

Find命令


格式:find [option] [Path] [筛选条件] [处理动作]
Path:默认当前目录
筛选条件:对文件/目录设置筛选条件
处理动作:默认显示所有文件
  筛选条件:
    -name Name文件或目录名称,区分大小写
    -iname Name不区分大小写
    -user User_Name利用文件属主筛选
    -group Group_Name利用文件属组筛选
    -uid UID利用UID进行筛选,当用户被删除时,只有用户的UID留在文件/目录上,没有用户名显示
    -gid GID利用GID进行筛选,当组被删除时,只用GID留在文件/目录上,没有组名显示
    -nouser被删除用户所留下的文件
    -nogroup被删除的组所留下的文件
    -size [+|-] n [unit]
      +|-
        -size n [unit]n-1至n个单位的文件
        -size +n [unit]大于n+1个单位的文件
        -size -n [unit]小于n-1个单位的文件
      unit
        c比特  kKB  MMB  GGB
    以文件类型为筛选条件-type TYPE
      TYPE:f普通文件  d目录  b块设备  c字符设备  l符号链接  p命令管道  s套接字
    以时间戳为筛选条件
      以天计:--------------(now-n-1)--------------(now-n)--------------now---->
        -atime [+|-] n时分秒单位依然有效
          -atime n(now-n-1)至(now-n)
          -atime -n(now-n)至(now)内被访问到
          -atime +n(now-n-1)以前
        -mtime [+|-] n同atime
        -ctime [+|-] n同atime

[root@ZYB test_dir1]# date
Wed Apr 25 09:05:23 CST 2018
[root@ZYB test_dir2]# ls
test_file00  test_file01  test_file10  test_file11  test_file20  test_file21
[root@ZYB test_dir1]# touch -mt "04220700.00" test_file00
[root@ZYB test_dir1]# touch -mt "04221300.00" test_file01
[root@ZYB test_dir1]# touch -mt "04230700.00" test_file10
[root@ZYB test_dir1]# touch -mt "04231300.00" test_file11
[root@ZYB test_dir1]# touch -mt "04240700.00" test_file20
[root@ZYB test_dir1]# touch -mt "04241300.00" test_file21
[root@ZYB test_dir1]# stat test_file* | grep "Mod"
Modify: 2018-04-22 07:00:00.000000000 +0800
Modify: 2018-04-22 13:00:00.000000000 +0800
Modify: 2018-04-23 07:00:00.000000000 +0800
Modify: 2018-04-23 13:00:00.000000000 +0800
Modify: 2018-04-24 07:00:00.000000000 +0800
Modify: 2018-04-24 13:00:00.000000000 +0800
[root@ZYB test_dir1]# find -mtime 1
./test_file20
./test_file11
[root@ZYB test_dir1]# find -mtime -1
.
./test_file21
[root@ZYB test_dir1]# find -mtime -2
.
./test_file20
./test_file21
./test_file11
[root@ZYB test_dir1]# find -mtime +2
./test_file00

      以分钟计:--------------(now-x)--------------(now-x+1)--------------now---->
        -amin [+|-] n秒单位依然有效
          -amin x(now-x)至(now-x+1)
          -amin -x(now-x)至(now)
          -amin +x(now-x)以前
        -mmin [+|-] x同amin
        -cmin [+|-] x同amin

[root@ZYB test_dir2]# ls
test_file00  test_file03  test_file10  test_file13  test_file20  test_file23
test_file01  test_file04  test_file11  test_file14  test_file21  test_file24
test_file02  test_file05  test_file12  test_file15  test_file22  test_file25
[root@ZYB test_dir2]# stat test_file0? | grep "Modify"
Modify: 2018-04-25 08:29:05.000000000 +0800
Modify: 2018-04-25 08:29:15.000000000 +0800
Modify: 2018-04-25 08:29:25.000000000 +0800
Modify: 2018-04-25 08:29:35.000000000 +0800
Modify: 2018-04-25 08:29:45.000000000 +0800
Modify: 2018-04-25 08:29:55.000000000 +0800
[root@ZYB test_dir2]# stat test_file1? | grep "Modify"
Modify: 2018-04-25 08:30:05.000000000 +0800
Modify: 2018-04-25 08:30:15.000000000 +0800
Modify: 2018-04-25 08:30:25.000000000 +0800
Modify: 2018-04-25 08:30:35.000000000 +0800
Modify: 2018-04-25 08:30:45.000000000 +0800
Modify: 2018-04-25 08:30:55.000000000 +0800
[root@ZYB test_dir2]# stat test_file2? | grep "Modify"
Modify: 2018-04-25 08:31:05.000000000 +0800
Modify: 2018-04-25 08:31:15.000000000 +0800
Modify: 2018-04-25 08:31:25.000000000 +0800
Modify: 2018-04-25 08:31:35.000000000 +0800
Modify: 2018-04-25 08:31:45.000000000 +0800
Modify: 2018-04-25 08:31:55.000000000 +0800
[root@ZYB test_dir2]# date
Wed Apr 25 08:48:19 CST 2018
[root@ZYB test_dir2]# find -mmin 18
./test_file20
./test_file21
./test_file15
./test_file13
./test_file14
./test_file12
[root@ZYB test_dir2]# date
Wed Apr 25 08:53:31 CST 2018
[root@ZYB test_dir2]# find -mmin -19
.
./test_file20
./test_file21
./test_file15
./test_file24
./test_file13
./test_file14
./test_file23
./test_file25
./test_file22
[root@ZYB test_dir2]# date
Wed Apr 25 08:49:33 CST 2018
[root@ZYB test_dir2]# find -mmin +23
./test_file02
./test_file11
./test_file00
./test_file04
./test_file10
./test_file12
./test_file01
./test_file05
./test_file03

    以权限为筛选条件-perm [+|-] MODE
      -perm MODE精确匹配
      -perm +MODE任意用户类型的任意指定权限位,-要被/所取代
      -perm -MODE指定用户类型指定权限位
  筛选条件组合时,括号需转义

[root@ZYB ~]# find /usr/ -not -user root -not -user bin -ls
2102377    0 -rw-r--r--   1 zyb      zyb             0 Apr 24 20:18 /usr/haha
[root@ZYB ~]# find /usr/ -not ( -user root -o -user bin ) -ls 
2102377    0 -rw-r--r--   1 zyb      zyb             0 Apr 24 20:18 /usr/haha
# -not空格(空格-user空格root空格-o空格-user空格bin空格)	空格不能省略

  处理动作:
    -print打印到标准输出上
    -ls以长格式形式显示
    -exec COMMAND {} ;使用于对查找到的内容进行处理,反斜号为转义字符,花括号与反斜线之间用空格

[root@ZYB test_dir1]# ls
test_file1  test_file2
[root@ZYB test_dir1]# find -name "tes*" -exec mv {} {}_haha ;

    -ok COMMAND {} ;交互模式,同-exec,但在进行操作之前需用户确认所执行的命令
    -xargs COMMAND对查找内容进行操作

原文地址:https://www.cnblogs.com/hesper/p/8794339.html