Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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*'
targetDir/target1
targetDir/target2
targetDir/subDir/target3

instead of:

$ find targetDir -name 'target*'
targetDir
targetDir/target1
targetDir/target2
targetDir/subDir/target3
share|improve this question
add comment

5 Answers

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
add comment

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
add comment

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
1  
Be aware that this will ignore all hidden files/directories in targetDir. –  michas Jan 3 at 13:29
add comment

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
add comment

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
add comment

Your Answer

 
discard

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.