282

Is it possible to use the find command in some way that it will not recurse into the sub-directories? For example,

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

And the result of something like find DirsRoot --do-not-recurse -type f will be only File1, File2?

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
filippo
  • 5,583
  • 13
  • 50
  • 72

4 Answers4

435

I think you'll get what you want with the -maxdepth 1 option, based on your current command structure. If not, you can try looking at the man page for find.

Relevant entry (for convenience's sake):

-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.

Your options basically are:

# Do NOT show hidden files (beginning with ".", i.e., .*):
find DirsRoot/* -maxdepth 0 -type f

Or:

#  DO show hidden files:
find DirsRoot/ -maxdepth 1 -type f
Rob Bednark
  • 25,981
  • 23
  • 80
  • 125
eldarerathis
  • 35,455
  • 10
  • 90
  • 93
  • 1
    For the OP's example I think this needs to be `-maxdepth 1` ? – Paul R Oct 13 '10 at 15:42
  • @Paul R: Actually, that sort of depends on how he wants to handle hidden files, but I've amended my answer nonetheless. For his example `1` is probably what he wants. – eldarerathis Oct 13 '10 at 16:00
  • 3
    For me,`-maxdepth 0` isn't showing *any* file but `-maxdepth 1` is working as intended, with hidden files being displayed as well. – Srini Mar 02 '16 at 20:11
  • 3
    @BruceWayne note the `*` in `find DirsRoot/* -maxdepth 0 -type f`. If you leave that out, it won't show any files. – mapeters Jul 14 '16 at 15:43
  • @mook, Thanks, but I can't remember the original context in which I had this issue, haha. – Srini Jul 14 '16 at 16:17
  • Using '*' to omit hidden files displays an error if nothing is found. I guess you can redirect stedrr to dev/null but this could obscure other issues. Not sure yet if this is truly useful as a one line solution. – Bill Gale Sep 06 '22 at 12:31
37

I believe you are looking for -maxdepth 1.

waffle paradox
  • 2,755
  • 18
  • 19
17

If you look for POSIX compliant solution:

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

-maxdepth is not POSIX compliant option.

sqr163
  • 1,074
  • 13
  • 24
  • thanks for this solution, but can't this be simplified to `find DirsRoot/* -type f -prune`? – dokaspar May 05 '17 at 12:31
  • @dokaspar Really great question! (you forgot to insert "-o" before `-prune` btw) The answer is no, it cannot. To fully understand WHY it cannot be simplified, just emit `set -x` command before emitting the `find DirsRoot/* -type f -o -prune` and you will immediately see it yourself. The root cause is the limitations of the shell expansion of `DirsRoot/*` expression. – sqr163 May 05 '17 at 21:47
  • not working on centos, still doing full recursion at output!!!! The proper command is `find . -name . -o -prune` – Reishin Jul 06 '18 at 14:25
  • on Solaris anyway, keeping with DirsRoot as the desired path, you do not need cd ; instead, you can: find DirsRoot/. -type f -print -o -name . -o -prune – spioter Dec 04 '19 at 17:21
5

Yes it is possible by using -maxdepth option in find command

find /DirsRoot/* -maxdepth 1 -type f

From the manual

man find

-maxdepth levels

Descend at most levels (a non-negative integer) levels of directories below the starting-points.

-maxdepth 0

means only apply the tests and actions to the starting-points themselves.

Javeed Shakeel
  • 2,926
  • 2
  • 31
  • 40