find查找时排除目录及文件
查找根目录下大于500M的文件,排除/proc目录
- find / ! -path "/proc/*" -type f -size +500M | sort -rh|xargs ls -lh | awk '{ print $9 ": " $5 }'
如果排除俩个目录
- find / ! -path "/proc/*" ! -path "/home/*" -type f -size +500M | sort -rh|xargs ls -lh | awk '{ print $9 ": " $5 }'
参考
find排除目录 - raindream - 博客园
https://www.cnblogs.com/drizzlewithwind/p/5705915.html
find命令:忽略一个目录或者多个目录 - PhoenixMY - 博客园
https://www.cnblogs.com/PhoenixMY/p/5919810.html
1)find过滤目录
使用find命令在linux系统中查找文件时,有时需要忽略某些目录,可以使用"-path 过滤的目录路径 -prune -o"参数来进行过滤。不过必须注意:要忽略的路径参数要紧跟着搜索的路径之后,否则该参数无法起作用
- 拿一个例子来说明下:
- 比如查找/data/web/ssy/online路径下的的目录,并统计目录大小,以G位单位进行排序(默认为降序),并统计前10个大小的目录。命令如下:
- # find /data/web/ssy/online/* -maxdepth 0 -type d -exec /usr/bin/du -sh {} \;|grep '[0-9]G'|sort -rh|head -10
- 查找/data/web/ssy/online路径下除tmp目录之外的目录,并统计目录大小,以G位单位进行排序(默认为降序),并统计前10个大小的目录。命令如下
- # find /data/web/ssy/online/* -path /data/web/ssy/online/tmp -prune -o -maxdepth 0 -type d -exec /usr/bin/du -sh {} \;|grep '[0-9]G'|sort -rh|head -10
- 注意:
- 1)"-maxdepth 0" 表示只查找到/data/web/ssy/online下的目录。如果是"-maxdepth 1"则表示查找到/data/web/ssy/online/xxx下的目录
- 2)find命令中的过滤、忽略、排除使用"-path 过滤的文件或目录-prune -o ",其中-prune类似于if判断,如果-prune之前的语句为真,比如找到了
- 前面-path指定的/data/web/ssy/online/tmp目录,就不再执行后面-o跟的语句了,如果没有找到则执行后面的语句。这样就做到了排除效果!
- 其中的"-o" 是 "-or" 的意思!
- 3)-path要过滤掉的文件或目录路径参数一定要紧跟在要搜索的路径之后,否则过滤效果就不会实现!!也就是说上面的"-path /data/web/ssy/online/tmp"
- 必须紧跟着放在"/data/web/ssy/online/*"后面,否则查找时就不会过来掉/data/web/ssy/online/tmp这个目录。
- ========================================================================================================================================================
- 示例一:
- 假设/opt/kevin目录下有三个目录:test1,test2,test3,三个目录下都有list文件
- [root@localhost kevin]# pwd
- /opt/kevin
- [root@localhost kevin]# ls
- test1 test2 test3
- 现在要查找/opt/kevin路径下的list文件,并忽略掉test2目录,操作如下:
- [root@localhost kevin]# pwd
- /opt/kevin
- [root@localhost kevin]# find . -type f -name list
- ./test1/list
- ./test2/list
- ./test3/list
- [root@localhost kevin]# find . -type f -name list -print
- ./test1/list
- ./test2/list
- ./test3/list
- 使用-path 和 -prune -o实现过滤效果
- [root@localhost kevin]# find . -path test2 -prune -o -type f -name list -print
- ./test1/list
- ./test2/list
- ./test3/list
- [root@localhost kevin]# find . -path ./test2/ -prune -o -type f -name list -print
- find: warning: -path ./test2/ will not match anything because it ends with /.
- ./test1/list
- ./test2/list
- ./test3/list
- 当搜索路径不是全路径时,过滤目录路径必须是./test2 才能实现过滤效果!
- [root@localhost kevin]# find . -path ./test2 -prune -o -type f -name list -print
- ./test1/list
- ./test3/list
- 要过滤的目录操作-path必须紧跟着搜索路径 才能实现过滤效果
- [root@localhost kevin]# find . -type f -path ./test2 -prune -o -name list -print
- ./test1/list
- ./test2/list
- ./test3/list
- 当搜索路径时全路径时,过滤路径也要是全路径,才能实现过滤效果
- [root@localhost kevin]# find . -path /opt/kevin/test2 -prune -o -type f -name list -print
- ./test1/list
- ./test2/list
- ./test3/list
- [root@localhost kevin]# find /opt/kevin/ -path /opt/kevin/test2 -prune -o -type f -name list -print
- /opt/kevin/test1/list
- /opt/kevin/test3/list
- [root@localhost kevin]# find /opt/kevin/* -path /opt/kevin/test2 -prune -o -type f -name list -print
- /opt/kevin/test1/list
- /opt/kevin/test3/list
- [root@localhost kevin]# find /opt/kevin -path /opt/kevin/test2 -prune -o -type f -name list -print
- /opt/kevin/test1/list
- /opt/kevin/test3/list
- [root@localhost kevin]# find /opt/kevin -path /opt/kevin/test2/ -prune -o -type f -name list -print
- find: warning: -path /opt/kevin/test2/ will not match anything because it ends with /.
- /opt/kevin/test1/list
- /opt/kevin/test2/list
- /opt/kevin/test3/list
- 由上面可知:
- 1)当要搜索的目录不是全路径时,要过滤掉的目录必须是"./test2"才能实现过滤效果。如果是"test2"或者"./test2/"都不能实现过滤效果。
- 2)当要搜索的目录是全路径时,要过滤掉的目录也必须是全路径才能实现过滤效果!要过滤掉的目录后面不能加"/",否则也不能实现过滤效果。
- 3)过滤操作"-path /opt/kevin/test2/ -prune -o"必须紧跟在要搜索路径的后面才能实现过滤效果,否则也不能实现过滤效果。
- 如果要过滤两个目录,比如过滤掉test2和test3目录,则使用转义符\( -path ./test2 -o -path ./test3 -prune -o \)
- 注意:两个转义符前面都要有空格!!
- [root@localhost kevin]# find . -path ./test2 -o -path ./test3 -prune -o -type f -name list -print
- ./test1/list
- ./test2/list
- [root@localhost kevin]# find . \( -path ./test2 -o -path ./test3 \) -prune -o -type f -name list -print
- ./test1/list
- [root@localhost kevin]# find /opt/kevin/ \( -path /opt/kevin/test2 -o -path /opt/kevin/test3 \) -prune -o -type f -name list -print
- /opt/kevin/test1/list
- 除了上面的方法,还有一个方法如下:
- [root@localhost kevin]# find . -type f -name list ! -path ./test2/* ! -path ./test3/*
- ./test1/list
2)find过滤文件
先查看对应文件,然后使用"grep -v"进行过滤
- 比如只查找/opt/kevin目录下的文件(不查找/opt/kevin的二级目录下的文件),并过滤到haha2文件
- [root@localhost kevin]# pwd
- /opt/kevin
- [root@localhost kevin]# ll
- total
- -rw-r--r-- root root Nov : haha
- -rw-r--r-- root root Nov : haha1
- -rw-r--r-- root root Nov : haha2
- -rw-r--r-- root root Nov : haha3
- -rw-r--r-- root root Nov : haha4
- drwxr-xr-x root root Nov : test1
- drwxr-xr-x root root Nov : test2
- drwxr-xr-x root root Nov : test3
- [root@localhost kevin]# find . -maxdepth -type f
- ./haha
- ./haha1
- ./haha2
- ./haha3
- ./haha4
- [root@localhost kevin]# find . -maxdepth -type f |grep -v "haha2"
- ./haha
- ./haha1
- ./haha3
- ./haha4
- 过滤多个文件,就使用多个"grep -v"
- [root@localhost kevin]# find . -maxdepth -type f |grep -v "haha2"
- ./haha
- ./haha1
- ./haha3
- ./haha4
- [root@localhost kevin]# find . -maxdepth -type f |grep -v "haha2"|grep -v haha3
- ./haha
- ./haha1
- ./haha4
- [root@localhost kevin]# find . -maxdepth -type f |grep -v "haha2"|grep -v haha3|grep -v haha4
- ./haha
- ./haha1
- )find命令中的-maxdepth和-mindepth:控制搜索深度的选项
- -maxdepth :指定遍历搜索的最大深度。最大目录层级
- -mindepth: 指定开始遍历搜索的最小深度。最小目录层级
- -maxdepth :最大目录层级为0,表示只针对当前目录本身(比如/opt/kevin)进行搜索操作或du -sh 统计操作。
- -maxdepth :最大目录层级为1,表示针对/opt/kevin/ 路径进行搜索操作或du -sh 统计操作。
- -maxdepth :最大目录层级为2,表示针对/opt/kevin/xxx/ 路径进行搜索操作或du -sh 统计操作。
- [root@localhost kevin]# pwd
- /opt/kevin
- [root@localhost kevin]# ll
- total
- -rw-r--r-- root root Nov : haha
- -rw-r--r-- root root Nov : haha1
- -rw-r--r-- root root Nov : haha2
- -rw-r--r-- root root Nov : haha3
- -rw-r--r-- root root Nov : haha4
- drwxr-xr-x root root Nov : test1
- drwxr-xr-x root root Nov : test2
- drwxr-xr-x root root Nov : test3
- -maxdepth 表示最小目录层级是0,即搜索路径它本身
- [root@localhost kevin]# find . -maxdepth -type f
- 但是如果当前路径加入"*"使用"-maxdepth 0" 效果和 当前路径不加"*"使用"-maxdepth 1" 是一样的!
- [root@localhost kevin]# find ./* -maxdepth 0 -type f
- ./haha
- ./haha1
- ./haha2
- ./haha3
- ./haha4
- [root@localhost kevin]# find . -maxdepth 1 -type f
- ./haha
- ./haha1
- ./haha2
- ./haha3
- ./haha4
- [root@localhost kevin]# find /opt/kevin -maxdepth 0 -type f
- [root@localhost kevin]# find /opt/kevin/ -maxdepth 0 -type f
- [root@localhost kevin]# find /opt/kevin/* -maxdepth 0 -type f
- /opt/kevin/haha
- /opt/kevin/haha1
- /opt/kevin/haha2
- /opt/kevin/haha3
- [root@localhost kevin]# find /opt/kevin -maxdepth 1 -type f
- /opt/kevin/haha
- /opt/kevin/haha1
- /opt/kevin/haha2
- /opt/kevin/haha3
- /opt/kevin/haha4
- [root@localhost kevin]# find /opt/kevin/ -maxdepth 1 -type f
- /opt/kevin/haha
- /opt/kevin/haha1
- /opt/kevin/haha2
- /opt/kevin/haha3
- /opt/kevin/haha4
- [root@localhost kevin]# find /opt/kevin/* -maxdepth 1 -type f
- /opt/kevin/haha
- /opt/kevin/haha1
- /opt/kevin/haha2
- /opt/kevin/haha3
- /opt/kevin/haha4
- /opt/kevin/test1/list
- /opt/kevin/test2/list
- /opt/kevin/test3/list
- [root@localhost kevin]# find . -maxdepth 2 -type f
- ./test1/list
- ./test2/list
- ./test3/list
- ./haha
- ./haha1
- ./haha2
- ./haha3
- ./haha4
- 结论:
- 如果搜索路径后面加了"*",则使用"-maxdepth n"
- 和
- 不加"*"使用"-maxdepth n+1"
- 的效果是一样的!!
- 超过了实际目录级层,效果是一样的
- [root@localhost kevin]# find . -maxdepth 3 -type f
- ./test1/list
- ./test2/list
- ./test3/list
- ./haha
- ./haha1
- ./haha2
- ./haha3
- ./haha4
- [root@localhost kevin]# find . -maxdepth 4 -type f
- ./test1/list
- ./test2/list
- ./test3/list
- ./haha
- ./haha1
- ./haha2
- ./haha3
- ./haha4
- 如果仅仅只是在/opt/kevin/xxx下搜索,即这里的最小目录深度是2
- [root@localhost kevin]# pwd
- /opt/kevin
- [root@localhost kevin]# ll
- total 0
- -rw-r--r-- 1 root root 0 Nov 21 18:51 haha
- -rw-r--r-- 1 root root 0 Nov 21 18:51 haha1
- -rw-r--r-- 1 root root 0 Nov 21 18:51 haha2
- -rw-r--r-- 1 root root 0 Nov 21 18:51 haha3
- -rw-r--r-- 1 root root 0 Nov 21 18:51 haha4
- drwxr-xr-x 2 root root 18 Nov 21 18:24 test1
- drwxr-xr-x 2 root root 18 Nov 21 18:24 test2
- drwxr-xr-x 2 root root 18 Nov 21 18:24 test3
- [root@localhost kevin]# find . -mindepth 2 -type f
- ./test1/list
- ./test2/list
- ./test3/list
- 最小目录层级为0
- [root@localhost kevin]# find . -mindepth 0 -type f
- ./test1/list
- ./test2/list
- ./test3/list
- ./haha
- ./haha1
- ./haha2
- ./haha3
- ./haha4
- 最小目录层级为1
- [root@localhost kevin]# find . -mindepth 1 -type f
- ./test1/list
- ./test2/list
- ./test3/list
- ./haha
- ./haha1
- ./haha2
- ./haha3
- ./haha4
- 最小目录层级为3,即超过当前最大目录层级,则就搜索不到了!
- [root@localhost kevin]# find . -mindepth 3 -type f
- ========================================================================
- -mindepth和-maxdepth可以一起结合起来使用,用于搜索指定层级范围内的文件。
- ========================================================================
- 如果只想搜索/opt/kevin/xxx下的文件,可行的做法:
- 第一种做法:最大目录层级是1,即-maxdepth 1
- [root@localhost kevin]# find ./* -maxdepth 0 -type f
- ./haha
- ./haha1
- ./haha2
- ./haha3
- ./haha4
- [root@localhost kevin]# find . -maxdepth 1 -type f
- ./haha
- ./haha1
- ./haha2
- ./haha3
- ./haha4
- 第二种做法:最小目录层级是1,最大目录层级是1,即-mindepth 1 -maxdepth 1
- [root@localhost kevin]# find . -mindepth 1 -maxdepth 1 -type f
- ./haha
- ./haha1
- ./haha2
- ./haha3
- ./haha4
- 再来看下面的示例
- [root@localhost kevin]# echo "123456" > bo/bobo/list1
- [root@localhost kevin]# echo "123456" > bo/bobo/list2
- [root@localhost kevin]# echo "123456" > bo/bobo/ke/list3
- [root@localhost kevin]# echo "123456" > bo/bobo/ke/list4
- [root@localhost kevin]# ll bo/
- total 0
- drwxr-xr-x 3 root root 42 Nov 21 23:23 bobo
- [root@localhost kevin]# ll bo/bobo/
- total 8
- drwxr-xr-x 2 root root 32 Nov 21 23:23 ke
- -rw-r--r-- 1 root root 7 Nov 21 23:23 list1
- -rw-r--r-- 1 root root 7 Nov 21 23:23 list2
- [root@localhost kevin]# ll bo/bobo/ke/
- total 8
- -rw-r--r-- 1 root root 7 Nov 21 23:23 list3
- -rw-r--r-- 1 root root 7 Nov 21 23:23 list4
- 如果想搜索/opt/kevin/xxx/xxx下的文件,即最小目录层级是3,最大目录层级是3
- [root@localhost kevin]# find . -mindepth 3 -type f
- ./bo/bobo/ke/list3
- ./bo/bobo/ke/list4
- ./bo/bobo/list1
- ./bo/bobo/list2
- [root@localhost kevin]# find . -maxdepth 3 -type f
- ./test1/list
- ./test2/list
- ./test3/list
- ./haha
- ./haha1
- ./haha2
- ./haha3
- ./haha4
- ./bo/bobo/list1
- ./bo/bobo/list2
- [root@localhost kevin]# find . -mindepth 3 -maxdepth 3 -type f
- ./bo/bobo/list1
- ./bo/bobo/list2
- 如果想要搜索第二层级和第三层级之间的文件,如下:
- [root@localhost kevin]# find . -mindepth 2 -type f
- ./test1/list
- ./test2/list
- ./test3/list
- ./bo/bobo/ke/list3
- ./bo/bobo/ke/list4
- ./bo/bobo/list1
- ./bo/bobo/list2
- [root@localhost kevin]# find . -maxdepth 3 -type f
- ./test1/list
- ./test2/list
- ./test3/list
- ./haha
- ./haha1
- ./haha2
- ./haha3
- ./haha4
- ./bo/bobo/list1
- ./bo/bobo/list2
- [root@localhost kevin]# find . -mindepth 2 -maxdepth 3 -type f
- ./test1/list
- ./test2/list
- ./test3/list
- ./bo/bobo/list1
- ./bo/bobo/list2
转自
Find 查找命令时过滤掉某些文件或目录 以及 -maxdepth、-mindepth的用法 - 散尽浮华 - 博客园 https://www.cnblogs.com/kevingrace/p/11907695.html
find查找时排除目录及文件的更多相关文章
- Sublime Text 查找时排除指定的文件夹或文件
Sublime Text 查找时排除指定的文件夹或文件 Ctrl + Shift + F这组快捷键可以调出 Sublime Text 的查找替换窗口,里边有一栏 Where,可以做一些高级设置:d:\ ...
- [100]tar命令打包(排除目录或文件)
在linux中可以用tar打包目录以方便传输or备份,我们先来看一个例子 Linux下tar命令exclude选项排除指定文件或目录 test 文件夹有如下文件 [root@lee ~]# ll te ...
- grep时排除指定的文件和目录
参考:http://winterth.duapp.com/notes/ar03s04.htmlhttp://blog.sina.com.cn/s/blog_7169c8ce0100qkyf.html ...
- 7z压缩文件时排除指定的文件
分享一个7z压缩文件时排除指定文件类型的命令行,感觉很有用: 7z a -t7z d:\updateCRM.7z d:\updateCRM\*.* -r -x!*.log -x!*bak a:创建压缩 ...
- ZIP、tar.gz压缩时排除指定目录
1.ZIP 压缩时排除一个文件夹下所有内容zip -r sss.zip sss/ -x "sss/222/*" 压缩时排除指定多个文件夹下所有内容zip -r sss.zip ss ...
- [WinAPI] API 13 [遍历指定目录 打印文件和其他属性]
Windows API中,有一组专门的函数和结构,用于遍历目录,它们是FindFirstFile函数.FindNextFile函数和WIN32_FIND_DATA结构.使用FindFirstFile和 ...
- Find 查找命令时过滤掉某些文件或目录 以及 -maxdepth、-mindepth的用法
1)find过滤目录使用find命令在linux系统中查找文件时,有时需要忽略某些目录,可以使用"-path 过滤的目录路径 -prune -o"参数来进行过滤.不过必须注意:要忽 ...
- (转)linux下cp目录时排除一个或者多个目录的实现方法
原文链接:http://www.jb51.net/LINUXjishu/88971.html 说明:/home目录里面有data目录,data目录里面有a.b.c.d.e五个目录,现在要把data目录 ...
- linux 查找目录或文件详解
查找目录:find /(查找范围) -name '查找关键字' -type d查找文件:find /(查找范围) -name 查找关键字 -print 如果需要更进一步的了解,可以参看Linux的命令 ...
随机推荐
- php插入中文数据到MySQL乱码
事情是这样的:我在本地的测试成功了,放到服务器测试,发现服务器的数据库里的中文竟然乱码了. 我进行了以下几步基本的做法: PHP文件改为utf-8的格式. 加入header("Content ...
- 团队作业之旅游行业APP分析
随着经济的发展,不论是在工作中的男女老少,还是在校园中的童鞋,都喜欢在假期来一场说走就走的旅行,来缓解生活中的各种压力.当然,在国家面临经济转型的情况下,更多的将工业,农业转向服务型的旅游业,各个省市 ...
- Docker(一)-Docker介绍
什么就Docker? Docker是一个开源项目, 诞生于2013年初,最初是dotCloud公司内部的一个业余项目.它基于Google公司推出的Go语言实现.项目后来加入了Linux基金会,遵从了A ...
- CentOS 修改时区的方法
study from https://blog.csdn.net/skh2015java/article/details/85007624 第一种 tzselect 输入命令直接选择即可 第二种,直接 ...
- 实战基于Spring Boot 2的WebFlux和mLab搭建反应式Web
Spring Framework 5带来了新的Reactive Stack非阻塞式Web框架:Spring WebFlux.作为与Spring MVC并行使用的Web框架,Spring WebFlux ...
- clear & file input & reset & file input
clear & file input & reset & file input Clear <input type="file"> docume ...
- Java之递归遍历目录,修改指定文件的指定内容
EditProperties.java package PropertiesOperation.Edit; import java.io.File; /** * 替换指定Porpoerties文件中的 ...
- poj2135 Farm Tour(费用流)
Description When FJ's friends visit him on the farm, he likes to show them around. His farm comprise ...
- redis后台启动配置
在cmd窗口启动redis,窗口关闭后再次操作会报错. 将redis安装为服务,可使其在后台启动,无须担心误操作关闭服务窗口. 配置如下: 进入redis目录,输入如下命令执行即可: redis-se ...
- Linux Deploy Ubuntu安装MySQL
一.在Android手机安装Linux 二.Ubuntu安装Mysql 建议在root用户上操作 sudo su 输入密码 (一)安装mysql 1. sudo apt-get install mys ...