Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I prevent find from returning the directory I use as the root to start searching from in the results? e.g.:

$ find targetDir -name 'target*'

instead of:

$ find targetDir -name 'target*'
share|improve this question
up vote 6 down vote accepted

Use mindepth:

$ find targetDir -mindepth 1 -name 'target*'

From man find:

   -mindepth levels
          Do  not apply any tests or actions at levels less than levels (a
          non-negative integer).  -mindepth  1  means  process  all  files
          except the command line arguments.
share|improve this answer

With GNU, BSD or BusyBox find (i.e. on Linux, *BSD or Cygwin):

find targetDir -mindepth -name 'target*'

If you only have POSIX find (e.g. on Solaris or MINIX), the easy way is to change to the toplevel directory first. Then the location of the search has the unique name . which you can exclude.

( cd targetDir && find . -name . -o -name 'target*' -print )

If you really need to run from another directory, you can exclude directories called targetDir, but that will also exclude things like targetDir/something/targetDir.

Alternatively, you can make the shell do the work for you. Use a set of patterns to match all files in a directory. A downside is that you'll get an error message from find if one of the patterns doesn't match.

find targetDir/..?* targetDir/.[!.]* targetDir/* -name 'target*'

If you know that there are no dot files in the directory and that the directory is not empty, you can go with just *.

find targetDir/* -name 'target*'
share|improve this answer

A simple trick is to do like this:

$ find targetDir/* -name 'target*'

This will effectively exclude the root dir (targetDir) from the search.

share|improve this answer
Be aware that this will ignore all hidden files/directories in targetDir. – michas Jan 3 '14 at 13:29

It might be easier to specify what you really want instead of trying to ignore things you don't want. For example -type f will return only regular files, and will therefore not output the start directory.

But this really depends on what you are after, and why you want to get rid of the base dir.

share|improve this answer

yet another way: [just for the sake of providing one, in the "timtowtdi" idea]

( cd targetDir && find . -name 'target*' ) | sed -e 's#^./#targetDir/#'

of as a (more flexible)function:

findin () { 
    targetdir="$1" ; target="$2" 
    ( cd "${targetdir}" && find . -name "${target}" ) | sed -e "s#^./#${targetdir}/#"

findin "targetDir" 'target*'
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.