find without recursion

UnixFindShell

Unix Problem Overview


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?

Unix Solutions


Solution 1 - Unix

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

Solution 2 - Unix

I believe you are looking for -maxdepth 1.

Solution 3 - Unix

If you look for POSIX compliant solution:

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

-maxdepth is not POSIX compliant option.

Solution 4 - Unix

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionfilippoView Question on Stackoverflow
Solution 1 - UnixeldarerathisView Answer on Stackoverflow
Solution 2 - Unixwaffle paradoxView Answer on Stackoverflow
Solution 3 - Unixsqr163View Answer on Stackoverflow
Solution 4 - UnixJaveed ShakeelView Answer on Stackoverflow