英文:
Using Unix "find" to traverse a specific folder path
问题
我有以下目录结构
/顶层
/顶层/1,/顶层/2,... /顶层/nn
/顶层/xx/dir1,/顶层/xx/dir2,/顶层/xx/dir3
只有每个编号分支中的 dir3 包含感兴趣的文件。理想情况下,我希望 find 仅查看 dir3,并忽略该级别的任何其他目录(可能有超过2个要忽略的)。目前,我正在使用以下行,它可以工作,但执行时间似乎表明 find 进入 dir1 和 dir2 并检查每个文件是否与 -path 匹配,这是非常耗时且无意义的:
find /顶层 -path '/顶层/[0-9]*/dir3/*.log' -type f -mtime +1 -print
我试图找到一个“条件”,只允许 dir3“通过”,但找不到只在“目录级别”上工作的任何内容。我还尝试找到一种使用 -prune 的方法,而不必拥有一个列出的文件夹,无需搜索,但未能找到有效的语法。欢迎任何了解的想法。
英文:
I have a directory structure as follows
/top-level
/top-level/1, /top-level/2, ... /top-level/nn
/top-level/xx/dir1, /top-level/xx/dir2, /top-level/xx/dir3
Only dir3 in each numbered branch holds files of interest. Ideally, I would like find to ONLY look at dir3 and ignore any other directories at that level (there may be more than 2 to ignore). Presently, I'm using this line which works, but the execution time seems to indicate find is going into dir1 and dir2 and checking every file against -path, which is very time-consuming and pointless:
find /top-level -path '/top-level/[0-9]*/dir3/*.log' -type f -mtime +1 -print
I was trying to find a "conditional" that would only allow dir3 to "pass", but couldn't find anything that only worked on the "directory level". I also tried finding a way to use -prune without having to have an enumerated list of folders NOT to search, but have not been able to come up with a syntax that works.
Any INFORMED thoughts are appreciated.
答案1
得分: 1
使用-prune来避免进入非folder3目录。
find /top-level \( ! -name folder3 -type d -prune \) -type f -mtime +1 -print
英文:
Use -prune to avoid descending into the non-folder3 directories.
find /top-level \( ! -name folder3 -type d -prune \) -type f -mtime +1 -print
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论