博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Linux基础 - 文件管理 find

Posted on 2023-02-18 22:43  Kingdomer  阅读(29)  评论(0编辑  收藏  举报

 

find /etc /tmp /root -name passwd
find . -name "*.txt"                         # 当前路径下, 递归查找以.txt结尾的文件
find . -maxdepth 1 -name "*.txt"             # 当前路径下, 不递归查找以.txt结尾的文件夹, -maxdepth 1表示查找深度为1
find /tmp -user root -type s                 # 在/tmp下,查找属主是root的socket文件
find /tmp -atime +30 -exec rm –rf {} \;      # 删除查找到的超过30天没有访问过的文件
find /tmp -type f -size 0 -exec ls -l {} \;
find /tmp -type f -perm 644 -mtime +7 -ok rm {} \;

-name 查找时, 有无双引号的区别
[root@cl-node03 soupman]# find . -name "*.sh"
./scripts/chapter6/ex6-18.sh
./scripts/chapter9/ex9-30.sh
./scripts/chapter9/ex9-33.sh
./cc.sh
[root@cl-node03 soupman]# find . -name *.sh
./cc.sh

 

一、 查找选项

1.1 根据文件名查找
[root@centos78 ~]# find /etc -name '*del*'

-name:  按照文件名查找
-iname: 根据文件名查找,但是不区分大小写
-prune: 不在当前指定的目录中查找
-depth: 在查找文件时,首先查找当前目录中的文件,然后再在其子目录中查找

[root@centos78 ~]# ls
ab.txt ab.txt.zip anaconda-ks.cfg td2.zip td3.zip td.zip testdir
[root@centos78 ~]# find . -name "*.txt"
./testdir/ac/ac01.txt
./testdir/ab.txt
./ab.txt
[root@centos78 ~]# find . -maxdepth 1 -name "*.txt"
./ab.txt

1.2 根据文件所属用户和用户组查找 -user -group
[root@centos78 ~]# find / -user testuser01     # 查找在系统中属于testuser01用户的文件
[root@centos78 ~]# find / -group testuser01    # 查找在系统中属于testuser01组的文件
[root@centos78 ~]# find /tmp -uid 500          # 查找uid是500 的文件
[root@centos78 ~]# find /tmp -gid 1000         # 查找gid是1000的文件

1.3 根据文件时间戳属性查找
[root@centos78 ~]# find /tmp -atime -5         # 表示查找在五天内访问过的文件

  • -atime:   最近一次访问时间, 单位: 天
  • -mtime:   最近一次内容修改时间, 单位: 天
  • -ctime:   最近一次属性修改时间, 单位: 天
  • -amin:    最近一次访问时间, 单位: 分钟
  • -mmin:    最近一次内容修改时间, 单位: 分钟
  • -cmin:    最近一次属性修改时间, 单位: 分钟
  • -newer file1 ! file2:   查找更改时间比文件file1新但比文件file2旧的文件

1.4 根据文件类型查找 -type
[root@cl-server ~]# find /tmp -type s
[root@cl-server ~]# find . -type f -mmin -10   # 搜索当前目录中,过去10分钟内更新过的普通文件。
如果不加-type f参数,则搜索普通文件+特殊文件+目录。
文件类型: f-普通文件 d-目录 l-符号链接文件 c-字符设备文件  p-管道文件 b-块设备文件 s-socket文件

1.5 根据文件大小查找
-size n[c] 查找文件长度为n块的文件,带有c时表示文件长度以字节计
[root@centos78 ~]# find /tmp -size 2M          # 查找在/tmp 目录下等于2M的文件
[root@centos78 ~]# find /tmp -size +2M         # 查找在/tmp 目录下大于2M的文件
[root@centos78 ~]# find /tmp -size -2M         # 查找在/tmp 目录下小于2M的文件
[root@centos78 ~]# find . -size +1000000c      # 在当前目录下查找文件长度大于1M字节的文件
[root@centos78 ~]# find / -empty               # 查找在系统中为空的文件或者文件夹
[root@centos78 ~]# find ~ -size +1M -size -10M
[root@centos78 ~]# find ./ -size +3M -size -5M -mtime -7

1.6 根据文件权限查找 -perm
[root@centos78 ~]# find /tmp -perm 755         # 查找在/tmp目录下权限是755的文件
[root@centos78 ~]# find /tmp -perm +222        # 表示只要有一类用户(属主,属组,其他)的匹配写权限就行
[root@centos78 ~]# find /tmp -perm -222        # 表示必须所有类别用户都满足有写权限

1.7 nouser和-nogroup
-nogroup 查找所属组无效的文件,即该文件所属的组不存在
-nouser 查找属主无效的文件
[root@centos78 ~]# find / -nogroup -a -nouser  # 在整个系统中查找既没有属主又没有属组的文件

1.8 条件的与或非
-a 连接两个不同的条件(两个条件必须同时满足)
[root@cl-server ~]# find /tmp -name "*.pid" -a -user root

-o 连接两个不同的条件(两个条件满足其一即可)
[root@cl-server ~]# find /tmp -name "*.pid" -o -user root

-not 不满足指定条件
[root@cl-server ~]# find /tmp -not -user root

 

二、指定动作

对搜索结果进行特定的处理:

  • -print            # 默认情况下的动作
  • -ls               # 查找到后, 用ls显示出来
  • -ok [command]     # 查找到后, 执行命令的时候询问用户是否要执行
  • -exec [command]   # 查找到后, 执行命令的时候不询问用户, 直接执行

-ok和-exec命令将命令行上后续的参数作为他们参数的一部分,直到被\;序列终止。
魔术字符串{}是-ok和-exec命令的一个特殊类型的参数,它将被当前文件的完整路径取代。 {} 代替查找到的文件。

[root@cl-server ~]# find /tmp -atime +30 -exec rm –rf {} \;     # 删除查找到的超过30天没有访问过的文件
[root@cl-server ~]# find /tmp -name "*.old" | xargs chmod 700
[root@cl-server ~]# find . -name "config.py" -ls

[root@cl-server ~]# find ./ -size +2k -exec ls -l {} \ ;        # \与;之间不能有空格
find: 遗漏“-exec”的参数
[root@cl-server ~]# find ./ -size +2k -exec ls -l {} \;
-rw-r--r-- 1 root root 2336 7月 22 16:29 ./nginx_v1.conf
-rw-r--r-- 1 root root 2401 7月 22 17:33 ./nginx_v2.conf
-rw-r--r-- 1 root root 3598 7月 22 17:41 ./keepalived.conf
-rw-r--r-- 1 root root 2452 7月 22 17:46 ./nginx.conf.j2

 

[root@centos78 ~]# find . -type f -name "*.txt" -print0 | xargs -0 wc -l
0 ./testdir/ac/ac01.txt
0 ./testdir/ab.txt
10 ./ab.txt
10 total

  

对当前文件夹下图片进行压缩
# find ./ -regex '.*\(jpg\|JPG\|png\|PNG\|jpeg\)' -size +50k -exec convert -quality 75 {} {} \;

对格式260*345图片在50Kb以上的图片,图片质量为75进行压缩
# find ./ -regex '.*\(jpg\|JPG\|png\|PNG\|jpeg\)' -size +50k -exec convert -resize 260x345 -quality 75 {} {} \;

 

[root@centos78 ~]# find --help
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]

default path is the current directory; default expression is -print
expression may consist of: operators, options, tests, and actions:

operators (decreasing precedence; -and is implicit where no others are given):
      ( EXPR )   ! EXPR   -not EXPR   EXPR1 -a EXPR2   EXPR1 -and EXPR2
      EXPR1 -o EXPR2   EXPR1 -or EXPR2   EXPR1 , EXPR2

positional options (always true): -daystart -follow -regextype

normal options (always true, specified before other expressions):
      -depth --help -maxdepth LEVELS -mindepth LEVELS -mount -noleaf
      --version -xautofs -xdev -ignore_readdir_race -noignore_readdir_race

tests (N can be +N or -N or N): -amin N -anewer FILE -atime N -cmin N
      -cnewer FILE -ctime N -empty -false -fstype TYPE -gid N -group NAME
      -ilname PATTERN -iname PATTERN -inum N -iwholename PATTERN -iregex PATTERN
      -links N -lname PATTERN -mmin N -mtime N -name PATTERN -newer FILE
      -nouser -nogroup -path PATTERN -perm [-/]MODE -regex PATTERN
      -readable -writable -executable
      -wholename PATTERN -size N[bcwkMG] -true -type [bcdpflsD] -uid N
      -used N -user NAME -xtype [bcdpfls]
      -context CONTEXT

actions: -delete -print0 -printf FORMAT -fprintf FILE FORMAT -print
      -fprint0 FILE -fprint FILE -ls -fls FILE -prune -quit
      -exec COMMAND ; -exec COMMAND {} + -ok COMMAND ;
      -execdir COMMAND ; -execdir COMMAND {} + -okdir COMMAND ;