linux

Linux--find命令

2019-08-20  本文已影响0人  李小李的路

概述

find   path  -option  [-print]   [-exec   -ok   |xargs  |grep ] [ command  {}  \ ;  ]

find 命令的参数

 ~ 表示$HOME目录
 . 表示当前目录
 / 表示根目录 
注意:可以指定路径,采用${path}的方式
形式为command {} \;,注意{}与\;之间有空格 
-name   filename               #查找名为filename的文件
-iname  filename              #不区分名称字母大小写
-regex PATTERN              # 基于正则表达式的模式查找,匹配的是整个路径,而非单个文件名。
-perm                                #按执行权限来查找
-user    username             #按文件属主来查找
-group groupname            #按组来查找
-mtime   -n +n                   #按文件更改时间来查找文件,-n指n天以内,+n指n天以前
-atime    -n +n                   #按文件访问时间来查找文件,-n指n天以内,+n指n天以前
-ctime    -n +n                  #按文件创建时间来查找文件,-n指n天以内,+n指n天以前
-mmin  -n +n                   #按文件更改时间来查找文件,-n指n分钟以内,+n指n分钟以前
-amin    -n +n                   #按文件访问时间来查找文件,-n指n分钟以内,+n指n分钟以前
-cmin    -n +n                  #按文件创建时间来查找文件,-n指n分钟以内,+n指n分钟以前
-nogroup                          #查无有效属组的文件,即文件的属组在/etc/groups中不存在
-nouser                            #查无有效属主的文件,即文件的属主在/etc/passwd中不存
-type    b/d/c/p/l/f             #查是块设备、目录、字符设备、管道、符号链接、普通文件
-size      n[c]                    #查长度为n块[或n字节]的文件
-mount                            #查文件时不跨越文件系统mount点
-follow                            #如果遇到符号链接文件,就跟踪链接所指的文件
-prune                            #忽略某个目录

常用的find命令demo

查找名称/文件

[liyahui@172 00]$ find 1566061193689590_depth_price_py.txt
1566061193689590_depth_price_py.txt
[liyahui@172 00]$ find /home -name test.txt                                                                                       
find: ‘/home/centos’: Permission denied       
[liyahui@172 13]$ find . -iname 1565762393_depth_price.txt
./1565762393_depth_price.txt
./1565762393_depth_PRICE.txt
[liyahui@172 20190814]$ find . -type d -name 20
./20
[liyahui@172 13]$ find -type f -name 1565762393_depth_price.txt
./1565762393_depth_price.txt
[liyahui@172 20190814]$ find . -type f -name "*.txt" | wc -l
1078

根据权限查找

[liyahui@172 13]$ find -type f -perm 777
./1565762134_depth_price.txt
./1565762161_depth_price.txt
./1565762189_depth_price.txt
[liyahui@172 13]$ find -type f  ! -perm 777
./1565762217_depth_price.txt
./1565762246_depth_price.txt
./1565762276_depth_price.txt
./1565762306_depth_price.txt
./1565762335_depth_price.txt
./1565762364_depth_price.txt
./1565762393_depth_price.txt
./1565762393_depth_PRICE.txt
[liyahui@172 13]$ find -type f  ! -perm 777 -exec chmod 755 {} \;
[liyahui@172 13]$ ll
total 748
-rwxrwxrwx 1 liyahui liyahui 67627 Aug 14 13:55 1565762134_depth_price.txt
-rwxrwxrwx 1 liyahui liyahui 67592 Aug 14 13:56 1565762161_depth_price.txt
-rwxrwxrwx 1 liyahui liyahui 67593 Aug 14 13:56 1565762189_depth_price.txt
-rwxr-xr-x 1 liyahui liyahui 67637 Aug 14 13:56 1565762217_depth_price.txt
-rwxr-xr-x 1 liyahui liyahui 67622 Aug 14 13:57 1565762246_depth_price.txt
-rwxr-xr-x 1 liyahui liyahui 67640 Aug 14 13:57 1565762276_depth_price.txt
-rwxr-xr-x 1 liyahui liyahui 67617 Aug 14 13:58 1565762306_depth_price.txt
-rwxr-xr-x 1 liyahui liyahui 67636 Aug 14 13:58 1565762335_depth_price.txt
-rwxr-xr-x 1 liyahui liyahui 67611 Aug 14 13:59 1565762364_depth_price.txt
-rwxr-xr-x 1 liyahui liyahui 67603 Aug 14 13:59 1565762393_depth_price.txt
-rwxr-xr-x 1 liyahui liyahui 67603 Aug 20 09:48 1565762393_depth_PRICE.txt
[liyahui@172 13]$ find -type f -perm /u=r
./1565762134_depth_price.txt
./1565762161_depth_price.txt
./1565762189_depth_price.txt
./1565762217_depth_price.txt
./1565762246_depth_price.txt
./1565762276_depth_price.txt
./1565762306_depth_price.txt
./1565762335_depth_price.txt
./1565762364_depth_price.txt
./1565762393_depth_price.txt
./1565762393_depth_PRICE.txt
[liyahui@172 13]$ find -type f -name "1565762306_depth_price.txt" -exec rm -rf {} \;
[liyahui@172 13]$ find -type f -name "1565762306_depth_price.txt" -exec rm -rf {} \;
[liyahui@172 13]$ 
[liyahui@172 13]$ 
[liyahui@172 13]$ 
[liyahui@172 13]$ find -type f -name "15657623*.txt" -exec rm -rf {} \;
[liyahui@172 13]$ ll
total 408
-rwxrwxrwx 1 liyahui liyahui 67627 Aug 14 13:55 1565762134_depth_price.txt
-rwxrwxrwx 1 liyahui liyahui 67592 Aug 14 13:56 1565762161_depth_price.txt
-rwxrwxrwx 1 liyahui liyahui 67593 Aug 14 13:56 1565762189_depth_price.txt
-rwxr-xr-x 1 liyahui liyahui 67637 Aug 14 13:56 1565762217_depth_price.txt
-rwxr-xr-x 1 liyahui liyahui 67622 Aug 14 13:57 1565762246_depth_price.txt
-rwxr-xr-x 1 liyahui liyahui 67640 Aug 14 13:57 1565762276_depth_price.txt
drwxrwxr-x 2 liyahui liyahui 8192 Aug 14 23:59 23
[liyahui@172 20190814]$ find . -type d -empty
./13

根据日期和时间查找

[liyahui@172 20190814]$ find . -type f -atime 5 | wc -l
1067
[liyahui@172 20190814]$ find . -mtime +5 -mtime -10 | wc -l
0
[liyahui@172 20190814]$ find . -type f -cmin 60 | wc -l
0
[liyahui@172 20190814]$ find . -type f -amin 60 | wc -l
0

根据大小查找文件和目录

[liyahui@172 20190814]$ find -size +60k | wc -l
1067
[liyahui@172 20190814]$ find -size +64k -size -80k | wc -l
1067

上一篇下一篇

猜你喜欢

热点阅读