2010-10-13 42 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