查找根目录下大于500M的文件,排除/proc目录

  1. find / ! -path "/proc/*" -type f -size +500M | sort -rh|xargs ls -lh | awk '{ print $9 ": " $5 }'

如果排除俩个目录

  1. 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"参数来进行过滤。不过必须注意:要忽略的路径参数要紧跟着搜索的路径之后,否则该参数无法起作用

  1. 拿一个例子来说明下:
  2. 比如查找/data/web/ssy/online路径下的的目录,并统计目录大小,以G位单位进行排序(默认为降序),并统计前10个大小的目录。命令如下:
  3. # find /data/web/ssy/online/* -maxdepth 0 -type d -exec /usr/bin/du -sh {} \;|grep '[0-9]G'|sort -rh|head -10
  4.  
  5. 查找/data/web/ssy/online路径下除tmp目录之外的目录,并统计目录大小,以G位单位进行排序(默认为降序),并统计前10个大小的目录。命令如下
  6. # 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
  7.  
  8. 注意:
  9. 1"-maxdepth 0" 表示只查找到/data/web/ssy/online下的目录。如果是"-maxdepth 1"则表示查找到/data/web/ssy/online/xxx下的目录
  10. 2find命令中的过滤、忽略、排除使用"-path 过滤的文件或目录-prune -o ",其中-prune类似于if判断,如果-prune之前的语句为真,比如找到了
  11. 前面-path指定的/data/web/ssy/online/tmp目录,就不再执行后面-o跟的语句了,如果没有找到则执行后面的语句。这样就做到了排除效果!
  12. 其中的"-o" "-or" 的意思!
  13. 3)-path要过滤掉的文件或目录路径参数一定要紧跟在要搜索的路径之后,否则过滤效果就不会实现!!也就是说上面的"-path /data/web/ssy/online/tmp"
  14. 必须紧跟着放在"/data/web/ssy/online/*"后面,否则查找时就不会过来掉/data/web/ssy/online/tmp这个目录。
  15.  
  16. ========================================================================================================================================================
  17. 示例一:
  18. 假设/opt/kevin目录下有三个目录:test1test2test3,三个目录下都有list文件
  19. [root@localhost kevin]# pwd
  20. /opt/kevin
  21. [root@localhost kevin]# ls
  22. test1 test2 test3
  23.  
  24. 现在要查找/opt/kevin路径下的list文件,并忽略掉test2目录,操作如下:
  25. [root@localhost kevin]# pwd
  26. /opt/kevin
  27. [root@localhost kevin]# find . -type f -name list
  28. ./test1/list
  29. ./test2/list
  30. ./test3/list
  31. [root@localhost kevin]# find . -type f -name list -print
  32. ./test1/list
  33. ./test2/list
  34. ./test3/list
  35.  
  36. 使用-path -prune -o实现过滤效果
  37. [root@localhost kevin]# find . -path test2 -prune -o -type f -name list -print
  38. ./test1/list
  39. ./test2/list
  40. ./test3/list
  41.  
  42. [root@localhost kevin]# find . -path ./test2/ -prune -o -type f -name list -print
  43. find: warning: -path ./test2/ will not match anything because it ends with /.
  44. ./test1/list
  45. ./test2/list
  46. ./test3/list
  47.  
  48. 当搜索路径不是全路径时,过滤目录路径必须是./test2 才能实现过滤效果!
  49. [root@localhost kevin]# find . -path ./test2 -prune -o -type f -name list -print
  50. ./test1/list
  51. ./test3/list
  52.  
  53. 要过滤的目录操作-path必须紧跟着搜索路径 才能实现过滤效果
  54. [root@localhost kevin]# find . -type f -path ./test2 -prune -o -name list -print
  55. ./test1/list
  56. ./test2/list
  57. ./test3/list
  58.  
  59. 当搜索路径时全路径时,过滤路径也要是全路径,才能实现过滤效果
  60. [root@localhost kevin]# find . -path /opt/kevin/test2 -prune -o -type f -name list -print
  61. ./test1/list
  62. ./test2/list
  63. ./test3/list
  64.  
  65. [root@localhost kevin]# find /opt/kevin/ -path /opt/kevin/test2 -prune -o -type f -name list -print
  66. /opt/kevin/test1/list
  67. /opt/kevin/test3/list
  68.  
  69. [root@localhost kevin]# find /opt/kevin/* -path /opt/kevin/test2 -prune -o -type f -name list -print
  70. /opt/kevin/test1/list
  71. /opt/kevin/test3/list
  72.  
  73. [root@localhost kevin]# find /opt/kevin -path /opt/kevin/test2 -prune -o -type f -name list -print
  74. /opt/kevin/test1/list
  75. /opt/kevin/test3/list
  76.  
  77. [root@localhost kevin]# find /opt/kevin -path /opt/kevin/test2/ -prune -o -type f -name list -print
  78. find: warning: -path /opt/kevin/test2/ will not match anything because it ends with /.
  79. /opt/kevin/test1/list
  80. /opt/kevin/test2/list
  81. /opt/kevin/test3/list
  82.  
  83. 由上面可知:
  84. 1)当要搜索的目录不是全路径时,要过滤掉的目录必须是"./test2"才能实现过滤效果。如果是"test2"或者"./test2/"都不能实现过滤效果。
  85. 2)当要搜索的目录是全路径时,要过滤掉的目录也必须是全路径才能实现过滤效果!要过滤掉的目录后面不能加"/",否则也不能实现过滤效果。
  86. 3)过滤操作"-path /opt/kevin/test2/ -prune -o"必须紧跟在要搜索路径的后面才能实现过滤效果,否则也不能实现过滤效果。
  87.  
  88. 如果要过滤两个目录,比如过滤掉test2和test3目录,则使用转义符\( -path ./test2 -o -path ./test3 -prune -o \)
  89. 注意:两个转义符前面都要有空格!!
  90.  
  91. [root@localhost kevin]# find . -path ./test2 -o -path ./test3 -prune -o -type f -name list -print
  92. ./test1/list
  93. ./test2/list
  94.  
  95. [root@localhost kevin]# find . \( -path ./test2 -o -path ./test3 \) -prune -o -type f -name list -print
  96. ./test1/list
  97.  
  98. [root@localhost kevin]# find /opt/kevin/ \( -path /opt/kevin/test2 -o -path /opt/kevin/test3 \) -prune -o -type f -name list -print
  99. /opt/kevin/test1/list
  100.  
  101. 除了上面的方法,还有一个方法如下:
  102. [root@localhost kevin]# find . -type f -name list ! -path ./test2/* ! -path ./test3/*
  103. ./test1/list

2)find过滤文件
先查看对应文件,然后使用"grep -v"进行过滤

  1. 比如只查找/opt/kevin目录下的文件(不查找/opt/kevin的二级目录下的文件),并过滤到haha2文件
  2. [root@localhost kevin]# pwd
  3. /opt/kevin
  4. [root@localhost kevin]# ll
  5. total
  6. -rw-r--r-- root root Nov : haha
  7. -rw-r--r-- root root Nov : haha1
  8. -rw-r--r-- root root Nov : haha2
  9. -rw-r--r-- root root Nov : haha3
  10. -rw-r--r-- root root Nov : haha4
  11. drwxr-xr-x root root Nov : test1
  12. drwxr-xr-x root root Nov : test2
  13. drwxr-xr-x root root Nov : test3
  14.  
  15. [root@localhost kevin]# find . -maxdepth -type f
  16. ./haha
  17. ./haha1
  18. ./haha2
  19. ./haha3
  20. ./haha4
  21.  
  22. [root@localhost kevin]# find . -maxdepth -type f |grep -v "haha2"
  23. ./haha
  24. ./haha1
  25. ./haha3
  26. ./haha4
  27.  
  28. 过滤多个文件,就使用多个"grep -v"
  29. [root@localhost kevin]# find . -maxdepth -type f |grep -v "haha2"
  30. ./haha
  31. ./haha1
  32. ./haha3
  33. ./haha4
  34.  
  35. [root@localhost kevin]# find . -maxdepth -type f |grep -v "haha2"|grep -v haha3
  36. ./haha
  37. ./haha1
  38. ./haha4
  39.  
  40. [root@localhost kevin]# find . -maxdepth -type f |grep -v "haha2"|grep -v haha3|grep -v haha4
  41. ./haha
  42. ./haha1
  43.  
  44. find命令中的-maxdepth和-mindepth:控制搜索深度的选项
  45. -maxdepth :指定遍历搜索的最大深度。最大目录层级
  46. -mindepth 指定开始遍历搜索的最小深度。最小目录层级
  47.  
  48. -maxdepth :最大目录层级为0,表示只针对当前目录本身(比如/opt/kevin)进行搜索操作或du -sh 统计操作。
  49. -maxdepth :最大目录层级为1,表示针对/opt/kevin/ 路径进行搜索操作或du -sh 统计操作。
  50. -maxdepth :最大目录层级为2,表示针对/opt/kevin/xxx/ 路径进行搜索操作或du -sh 统计操作。
  51.  
  52. [root@localhost kevin]# pwd
  53. /opt/kevin
  54. [root@localhost kevin]# ll
  55. total
  56. -rw-r--r-- root root Nov : haha
  57. -rw-r--r-- root root Nov : haha1
  58. -rw-r--r-- root root Nov : haha2
  59. -rw-r--r-- root root Nov : haha3
  60. -rw-r--r-- root root Nov : haha4
  61. drwxr-xr-x root root Nov : test1
  62. drwxr-xr-x root root Nov : test2
  63. drwxr-xr-x root root Nov : test3
  64.  
  65. -maxdepth 表示最小目录层级是0,即搜索路径它本身
  66. [root@localhost kevin]# find . -maxdepth -type f
  67.  
  68. 但是如果当前路径加入"*"使用"-maxdepth 0" 效果和 当前路径不加"*"使用"-maxdepth 1" 是一样的!
  69. [root@localhost kevin]# find ./* -maxdepth 0 -type f
  70. ./haha
  71. ./haha1
  72. ./haha2
  73. ./haha3
  74. ./haha4
  75.  
  76. [root@localhost kevin]# find . -maxdepth 1 -type f
  77. ./haha
  78. ./haha1
  79. ./haha2
  80. ./haha3
  81. ./haha4
  82.  
  83. [root@localhost kevin]# find /opt/kevin -maxdepth 0 -type f
  84. [root@localhost kevin]# find /opt/kevin/ -maxdepth 0 -type f
  85. [root@localhost kevin]# find /opt/kevin/* -maxdepth 0 -type f
  86. /opt/kevin/haha
  87. /opt/kevin/haha1
  88. /opt/kevin/haha2
  89. /opt/kevin/haha3
  90.  
  91. [root@localhost kevin]# find /opt/kevin -maxdepth 1 -type f
  92. /opt/kevin/haha
  93. /opt/kevin/haha1
  94. /opt/kevin/haha2
  95. /opt/kevin/haha3
  96. /opt/kevin/haha4
  97. [root@localhost kevin]# find /opt/kevin/ -maxdepth 1 -type f
  98. /opt/kevin/haha
  99. /opt/kevin/haha1
  100. /opt/kevin/haha2
  101. /opt/kevin/haha3
  102. /opt/kevin/haha4
  103.  
  104. [root@localhost kevin]# find /opt/kevin/* -maxdepth 1 -type f
  105. /opt/kevin/haha
  106. /opt/kevin/haha1
  107. /opt/kevin/haha2
  108. /opt/kevin/haha3
  109. /opt/kevin/haha4
  110. /opt/kevin/test1/list
  111. /opt/kevin/test2/list
  112. /opt/kevin/test3/list
  113.  
  114. [root@localhost kevin]# find . -maxdepth 2 -type f
  115. ./test1/list
  116. ./test2/list
  117. ./test3/list
  118. ./haha
  119. ./haha1
  120. ./haha2
  121. ./haha3
  122. ./haha4
  123.  
  124. 结论:
  125. 如果搜索路径后面加了"*",则使用"-maxdepth n"

  126. 不加"*"使用"-maxdepth n+1"
  127. 的效果是一样的!!
  128.  
  129. 超过了实际目录级层,效果是一样的
  130. [root@localhost kevin]# find . -maxdepth 3 -type f
  131. ./test1/list
  132. ./test2/list
  133. ./test3/list
  134. ./haha
  135. ./haha1
  136. ./haha2
  137. ./haha3
  138. ./haha4
  139.  
  140. [root@localhost kevin]# find . -maxdepth 4 -type f
  141. ./test1/list
  142. ./test2/list
  143. ./test3/list
  144. ./haha
  145. ./haha1
  146. ./haha2
  147. ./haha3
  148. ./haha4
  149.  
  150. 如果仅仅只是在/opt/kevin/xxx下搜索,即这里的最小目录深度是2
  151. [root@localhost kevin]# pwd
  152. /opt/kevin
  153. [root@localhost kevin]# ll
  154. total 0
  155. -rw-r--r-- 1 root root 0 Nov 21 18:51 haha
  156. -rw-r--r-- 1 root root 0 Nov 21 18:51 haha1
  157. -rw-r--r-- 1 root root 0 Nov 21 18:51 haha2
  158. -rw-r--r-- 1 root root 0 Nov 21 18:51 haha3
  159. -rw-r--r-- 1 root root 0 Nov 21 18:51 haha4
  160. drwxr-xr-x 2 root root 18 Nov 21 18:24 test1
  161. drwxr-xr-x 2 root root 18 Nov 21 18:24 test2
  162. drwxr-xr-x 2 root root 18 Nov 21 18:24 test3
  163.  
  164. [root@localhost kevin]# find . -mindepth 2 -type f
  165. ./test1/list
  166. ./test2/list
  167. ./test3/list
  168.  
  169. 最小目录层级为0
  170. [root@localhost kevin]# find . -mindepth 0 -type f
  171. ./test1/list
  172. ./test2/list
  173. ./test3/list
  174. ./haha
  175. ./haha1
  176. ./haha2
  177. ./haha3
  178. ./haha4
  179.  
  180. 最小目录层级为1
  181. [root@localhost kevin]# find . -mindepth 1 -type f
  182. ./test1/list
  183. ./test2/list
  184. ./test3/list
  185. ./haha
  186. ./haha1
  187. ./haha2
  188. ./haha3
  189. ./haha4
  190.  
  191. 最小目录层级为3,即超过当前最大目录层级,则就搜索不到了!
  192. [root@localhost kevin]# find . -mindepth 3 -type f
  193.  
  194. ========================================================================
  195. -mindepth和-maxdepth可以一起结合起来使用,用于搜索指定层级范围内的文件。
  196. ========================================================================
  197. 如果只想搜索/opt/kevin/xxx下的文件,可行的做法:
  198. 第一种做法:最大目录层级是1,即-maxdepth 1
  199. [root@localhost kevin]# find ./* -maxdepth 0 -type f
  200. ./haha
  201. ./haha1
  202. ./haha2
  203. ./haha3
  204. ./haha4
  205.  
  206. [root@localhost kevin]# find . -maxdepth 1 -type f
  207. ./haha
  208. ./haha1
  209. ./haha2
  210. ./haha3
  211. ./haha4
  212.  
  213. 第二种做法:最小目录层级是1,最大目录层级是1,即-mindepth 1 -maxdepth 1
  214. [root@localhost kevin]# find . -mindepth 1 -maxdepth 1 -type f
  215. ./haha
  216. ./haha1
  217. ./haha2
  218. ./haha3
  219. ./haha4
  220.  
  221. 再来看下面的示例
  222. [root@localhost kevin]# echo "123456" > bo/bobo/list1
  223. [root@localhost kevin]# echo "123456" > bo/bobo/list2
  224. [root@localhost kevin]# echo "123456" > bo/bobo/ke/list3
  225. [root@localhost kevin]# echo "123456" > bo/bobo/ke/list4
  226. [root@localhost kevin]# ll bo/
  227. total 0
  228. drwxr-xr-x 3 root root 42 Nov 21 23:23 bobo
  229. [root@localhost kevin]# ll bo/bobo/
  230. total 8
  231. drwxr-xr-x 2 root root 32 Nov 21 23:23 ke
  232. -rw-r--r-- 1 root root 7 Nov 21 23:23 list1
  233. -rw-r--r-- 1 root root 7 Nov 21 23:23 list2
  234. [root@localhost kevin]# ll bo/bobo/ke/
  235. total 8
  236. -rw-r--r-- 1 root root 7 Nov 21 23:23 list3
  237. -rw-r--r-- 1 root root 7 Nov 21 23:23 list4
  238.  
  239. 如果想搜索/opt/kevin/xxx/xxx下的文件,即最小目录层级是3,最大目录层级是3
  240. [root@localhost kevin]# find . -mindepth 3 -type f
  241. ./bo/bobo/ke/list3
  242. ./bo/bobo/ke/list4
  243. ./bo/bobo/list1
  244. ./bo/bobo/list2
  245.  
  246. [root@localhost kevin]# find . -maxdepth 3 -type f
  247. ./test1/list
  248. ./test2/list
  249. ./test3/list
  250. ./haha
  251. ./haha1
  252. ./haha2
  253. ./haha3
  254. ./haha4
  255. ./bo/bobo/list1
  256. ./bo/bobo/list2
  257.  
  258. [root@localhost kevin]# find . -mindepth 3 -maxdepth 3 -type f
  259. ./bo/bobo/list1
  260. ./bo/bobo/list2
  261.  
  262. 如果想要搜索第二层级和第三层级之间的文件,如下:
  263. [root@localhost kevin]# find . -mindepth 2 -type f
  264. ./test1/list
  265. ./test2/list
  266. ./test3/list
  267. ./bo/bobo/ke/list3
  268. ./bo/bobo/ke/list4
  269. ./bo/bobo/list1
  270. ./bo/bobo/list2
  271.  
  272. [root@localhost kevin]# find . -maxdepth 3 -type f
  273. ./test1/list
  274. ./test2/list
  275. ./test3/list
  276. ./haha
  277. ./haha1
  278. ./haha2
  279. ./haha3
  280. ./haha4
  281. ./bo/bobo/list1
  282. ./bo/bobo/list2
  283.  
  284. [root@localhost kevin]# find . -mindepth 2 -maxdepth 3 -type f
  285. ./test1/list
  286. ./test2/list
  287. ./test3/list
  288. ./bo/bobo/list1
  289. ./bo/bobo/list2

转自

Find 查找命令时过滤掉某些文件或目录 以及 -maxdepth、-mindepth的用法 - 散尽浮华 - 博客园 https://www.cnblogs.com/kevingrace/p/11907695.html

find查找时排除目录及文件的更多相关文章

  1. Sublime Text 查找时排除指定的文件夹或文件

    Sublime Text 查找时排除指定的文件夹或文件 Ctrl + Shift + F这组快捷键可以调出 Sublime Text 的查找替换窗口,里边有一栏 Where,可以做一些高级设置:d:\ ...

  2. [100]tar命令打包(排除目录或文件)

    在linux中可以用tar打包目录以方便传输or备份,我们先来看一个例子 Linux下tar命令exclude选项排除指定文件或目录 test 文件夹有如下文件 [root@lee ~]# ll te ...

  3. grep时排除指定的文件和目录

    参考:http://winterth.duapp.com/notes/ar03s04.htmlhttp://blog.sina.com.cn/s/blog_7169c8ce0100qkyf.html ...

  4. 7z压缩文件时排除指定的文件

    分享一个7z压缩文件时排除指定文件类型的命令行,感觉很有用: 7z a -t7z d:\updateCRM.7z d:\updateCRM\*.* -r -x!*.log -x!*bak a:创建压缩 ...

  5. ZIP、tar.gz压缩时排除指定目录

    1.ZIP 压缩时排除一个文件夹下所有内容zip -r sss.zip sss/ -x "sss/222/*" 压缩时排除指定多个文件夹下所有内容zip -r sss.zip ss ...

  6. [WinAPI] API 13 [遍历指定目录 打印文件和其他属性]

    Windows API中,有一组专门的函数和结构,用于遍历目录,它们是FindFirstFile函数.FindNextFile函数和WIN32_FIND_DATA结构.使用FindFirstFile和 ...

  7. Find 查找命令时过滤掉某些文件或目录 以及 -maxdepth、-mindepth的用法

    1)find过滤目录使用find命令在linux系统中查找文件时,有时需要忽略某些目录,可以使用"-path 过滤的目录路径 -prune -o"参数来进行过滤.不过必须注意:要忽 ...

  8. (转)linux下cp目录时排除一个或者多个目录的实现方法

    原文链接:http://www.jb51.net/LINUXjishu/88971.html 说明:/home目录里面有data目录,data目录里面有a.b.c.d.e五个目录,现在要把data目录 ...

  9. linux 查找目录或文件详解

    查找目录:find /(查找范围) -name '查找关键字' -type d查找文件:find /(查找范围) -name 查找关键字 -print 如果需要更进一步的了解,可以参看Linux的命令 ...

随机推荐

  1. php插入中文数据到MySQL乱码

    事情是这样的:我在本地的测试成功了,放到服务器测试,发现服务器的数据库里的中文竟然乱码了. 我进行了以下几步基本的做法: PHP文件改为utf-8的格式. 加入header("Content ...

  2. 团队作业之旅游行业APP分析

    随着经济的发展,不论是在工作中的男女老少,还是在校园中的童鞋,都喜欢在假期来一场说走就走的旅行,来缓解生活中的各种压力.当然,在国家面临经济转型的情况下,更多的将工业,农业转向服务型的旅游业,各个省市 ...

  3. Docker(一)-Docker介绍

    什么就Docker? Docker是一个开源项目, 诞生于2013年初,最初是dotCloud公司内部的一个业余项目.它基于Google公司推出的Go语言实现.项目后来加入了Linux基金会,遵从了A ...

  4. CentOS 修改时区的方法

    study from https://blog.csdn.net/skh2015java/article/details/85007624 第一种 tzselect 输入命令直接选择即可 第二种,直接 ...

  5. 实战基于Spring Boot 2的WebFlux和mLab搭建反应式Web

    Spring Framework 5带来了新的Reactive Stack非阻塞式Web框架:Spring WebFlux.作为与Spring MVC并行使用的Web框架,Spring WebFlux ...

  6. clear & file input & reset & file input

    clear & file input & reset & file input Clear <input type="file"> docume ...

  7. Java之递归遍历目录,修改指定文件的指定内容

    EditProperties.java package PropertiesOperation.Edit; import java.io.File; /** * 替换指定Porpoerties文件中的 ...

  8. poj2135 Farm Tour(费用流)

    Description When FJ's friends visit him on the farm, he likes to show them around. His farm comprise ...

  9. redis后台启动配置

    在cmd窗口启动redis,窗口关闭后再次操作会报错. 将redis安装为服务,可使其在后台启动,无须担心误操作关闭服务窗口. 配置如下: 进入redis目录,输入如下命令执行即可: redis-se ...

  10. Linux Deploy Ubuntu安装MySQL

    一.在Android手机安装Linux 二.Ubuntu安装Mysql 建议在root用户上操作 sudo su 输入密码 (一)安装mysql 1. sudo apt-get install mys ...