bash 使用 find 排除子目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13460482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 22:59:09  来源:igfitidea点击:

Exclude a sub-directory using find

bashunix

提问by Ravi

I have directory structure like this

我有这样的目录结构

data
|___
   |
   abc
    |____incoming
   def
    |____incoming
    |____processed
   123
    |___incoming
   456
    |___incoming
    |___processed

There is an incoming sub-folder in all of the folders inside Data directory. I want to get all files from all the folders and sub-folders except the def/incoming and 456/incoming dirs. I tried out with following command

Data 目录中的所有文件夹中都有一个传入子文件夹。我想从除 def/incoming 和 456/incoming 目录之外的所有文件夹和子文件夹中获取所有文件。我尝试使用以下命令

 find /home/feeds/data -type d \( -name 'def/incoming' -o -name '456/incoming' -o -name arkona \) -prune -o -name '*.*' -print

but it is not working as expected.

但它没有按预期工作。

Ravi

拉维

回答by sampson-chen

This works:

这有效:

find /home/feeds/data -type f -not -path "*def/incoming*" -not -path "*456/incoming*"

Explanation:

解释:

  • find /home/feeds/data: start finding recursively from specified path
  • -type f: find files only
  • -not -path "*def/incoming*": don't include anything with def/incomingas part of its path
  • -not -path "*456/incoming*": don't include anything with 456/incomingas part of its path
  • find /home/feeds/data: 从指定路径开始递归查找
  • -type f: 只查找文件
  • -not -path "*def/incoming*":不要def/incoming在其路径中包含任何内容
  • -not -path "*456/incoming*":不要456/incoming在其路径中包含任何内容

回答by peter_the_oak

Just for the sake of documentation: You might have to dig deeper as there are many search'n'skip constellations (like I had to). It might turn out that pruneis your friend while -not -pathwon't do what you expect.

只是为了文档:您可能需要更深入地挖掘,因为有许多 search'n'skip 星座(就像我不得不这样做)。事实证明,那prune是你的朋友,但-not -path不会做你期望的事情。

So this is a valuable example of 15 find examples that exclude directories:

因此,这是 15 个排除目录的查找示例的宝贵示例:

http://www.theunixschool.com/2012/07/find-command-15-examples-to-exclude.html

http://www.theunixschool.com/2012/07/find-command-15-examples-to-exclude.html

To link to the initial question, excluding finally worked for me like this:

要链接到最初的问题,排除最终对我有用,如下所示:

find . -regex-type posix-extended -regex ".*def/incoming.*|.*456/incoming.*" -prune -o -print 

Then, if you wish to find one file and still exclude pathes, just add | grep myFile.txt.

然后,如果您希望找到一个文件并仍然排除路径,只需添加| grep myFile.txt.

It may depend also on your find version. I see:

它也可能取决于您的查找版本。我懂了:

$ find -version
GNU find version 4.2.27
Features enabled: D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION SELINUX

回答by Brian Campbell

-nameonly matches the filename, not the whole path. You want to use -pathinstead, for the parts in which you are pruning the directories like def/incoming.

-name只匹配文件名,而不匹配整个路径。您想-path改用,用于您修剪目录的部分,如def/incoming.

回答by Vishal Pathak

find $(INP_PATH} -type f -ls |grep -v "${INP_PATH}/.*/"

回答by alper

By following this How to exclude a directory in find . command:

按照这个How to exclude a directory in find 。命令

find . \( -name ".git" -o -name "node_modules" \) -prune -o -print