2010-10-13 37 views
164

是否有可能以某种方式使用find命令,它不会递归到子目录中?例如,找不到递归

DirsRoot 
    |-->SubDir1 
    | |-OtherFile1 
    |-->SubDir2 
    | |-OtherFile2 
    |-File1 
    |-File2 

和类似find DirsRoot --donotrecuourse -type f结果将是只File1, File2

回答

243

我想你会得到你想要的-maxdepth 1选项,根据你当前的命令结构。如果没有,您可以尝试查看man pagefind

相关条目(为方便起见):

-maxdepth levels 
      Descend at most levels (a non-negative integer) levels of direc- 
      tories below the command line arguments. `-maxdepth 0' means 
      only apply the tests and actions to the command line arguments. 

您的选项基本上都是:

find DirsRoot/* -maxdepth 0 -type f #This does not show hidden files 

或者:

find DirsRoot/ -maxdepth 1 -type f #This does show hidden files 
+0

对于OP的例子,我认为这需要'-maxdepth 1'? – 2010-10-13 15:42:48

+0

@ Paul R:实际上,这种取决于他想如何处理隐藏的文件,但我已经修改了我的答案。对于他的例子'1'可能是他想要的。 – eldarerathis 2010-10-13 16:00:31

+0

对我而言,'-maxdepth 0'没有显示*任何*文件,而是'-maxdepth 1'按预期工作,同时显示隐藏文件。 – 2016-03-02 20:11:40

21

我相信你正在寻找-maxdepth 1

+1

对于OP的例子,我认为这需要'-maxdepth 1'? – 2010-10-13 15:41:35

+0

是的,如果他完全按照他的例子使用命令,那么它会是1.我的错误。 – 2010-10-13 15:54:25

13

如果你看看POSIX兼容的解决方案:

cd DirsRoot && find . -type f -print -o -name . -o -prune

-maxdepth不符合POSIX选项。

+0

感谢这个解决方案,但不能简化为'find DirsRoot/* -type f -prune'? – dokaspar 2017-05-05 12:31:29

+0

@dokaspar真的很棒的问题! (你忘记在'-prune' btw前插入“-o”) 答案是否定的,它不能。为了完全理解为什么它不能被简化,只需在发出'find DirsRoot/* -type f -o -prune'之前发出'set -x'命令,你就会立即看到它。 根本原因是'DirsRoot/*'表达式的shell扩展的局限性。 – sqrt163 2017-05-05 21:47:52