Is there any way to prevent find from digging down recursively into subdirectories?

寵の児 提交于 2019-12-22 10:33:22

问题


When I do:

$ find / 

It searches the entire system.
How do I prevent that?

(This question comes from an "answer" to another question.)


回答1:


G'day,

Just wanted to expand on the suggestion from Jon to use -prune. It isn't the easiest of find options to use, for example to just search in the current directory the find command looks like:

find . \( -type d ! -name . -prune \) -o \( <the bit you want to look for> \)

this will stop find from descending into sub-directories within this directory.

Basically, it says, "prune anything that is a directory, whose name isn't ".", i.e. current dir."

The find command evals left to right for each item found in the current directory so after completion of the first element, i.e. the prune segment, it will then continue on with the matched item in your second -o (OR'd) expression.

HTH.

cheers, Rob




回答2:


Consider:

-maxdepth n
             True if the depth of the current file into the tree is less than
             or equal to n.

-mindepth n
             True if the depth of the current file into the tree is greater
             than or equal to n.



回答3:


You might be better off using wildcards. For instance, if you want to find all ksh scripts in the current directory:

$ ls *.ksh



回答4:


Use the -prune option.




回答5:


You may even do

echo /specific/dir/*.jpg

as it's your shell that expands the wildcard. Typing

ls *.jpg

is equivalent to typing

ls foo.jpg bar.jpg

given foo.jpg and bar.jpg are all the files that end with ".jpg" in the current directory.



来源:https://stackoverflow.com/questions/27077/is-there-any-way-to-prevent-find-from-digging-down-recursively-into-subdirectori

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!