简体   繁体   English

使用find命令排除目录并在其他目录上执行脚本

[英]Exclude directories with find command and executing a script on other directories

I currently have a directory structure that I need to be able to roll through each of 100 or so directories and run a script on them individually while excluding this check on a handful of other directories. 我目前有一个目录结构,我需要能够浏览100个左右的目录中的每个目录,并分别在其中运行脚本,同时还要排除少数其他目录上的检查。

This is what I have been using in the past: 这是我过去一直在使用的:

find ./OTHER/ -maxdepth 2 -wholename '*_*/*.txt' -execdir /files/bin/other_process {} +

I would like to exclude certain directories from this check and have not found a sufficient answer to this problem. 我想从此检查中排除某些目录,并且没有找到足够的答案来解决此问题。

This has been my best attempt (or two) at the problem: 这是我对此问题的最佳尝试(或两次):

find ./OTHER/ \( -path ./OTHER/X???_EXCLUDE_THIS -prune -o -path ./OTHER/X???_IGNORE_THIS -prune -o \) -type d \(-name *_*/*.txt \) -execdir /files/bin/other_process {} +

I get: 我得到:

find: paths must precede expression  ./OTHER/A101_EXCLUDE_THIS/

This is the return that I get on nearly every variation that I have used. 这是我几乎使用过的所有变体所获得的回报。

This has been my best attempt (or two) at the problem: 这是我对此问题的最佳尝试(或两次):

 find ./OTHER/ \\( -path ./OTHER/X???_EXCLUDE_THIS -prune -o -path ./OTHER/X???_IGNORE_THIS -prune -o \\) -type d \\(-name *_*/*.txt \\) -execdir /files/bin/other_process {} + 

Errors in this attempt: 尝试中的错误:

  • \\(-name : There must be a space after \\( . \\(-name\\(后面必须有一个空格。
  • -name *_*/*.txt : -name is for base of file name; -name *_*/*.txt-name用于文件名的基础; use -path here. 在这里使用-path
  • *_*/*.txt : You should quote such patterns to prevent expansion by the shell. *_*/*.txt :您应该引用此类模式以防止被Shell扩展。
  • -o \\) : -o does not belong at the end of an expression; -o \\) :- -o不属于表达式的末尾; you mean \\) -o . 你的意思是\\) -o But you don't need parentheses here. 但是您在这里不需要括号。
  • -type d : Since you want to find regular files *.txt , you must not look for a d irectory. -type d :由于要查找常规文件 *.txt ,因此请勿查找d目录。

With those errors corrected, it works: 纠正了这些错误后,它可以工作:

find ./OTHER/ -path './OTHER/X???_EXCLUDE_THIS' -prune -o -path './OTHER/X???_IGNORE_THIS' -prune -o -path '*_*/*.txt' -execdir echo {} +

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM