I want to search for all occurences of <searchterm> on my machine. Now when I perform a search like this

find / <searchterm>

I can see all the paths the search is running through. How can I limit the results to my searchterm?

Abstract example

I have a folder, let's call it my-own-folder. Now when I want to know if there are any other folders with the same name and run a search for

find / my-own-folder

the output shows a couple of hundred lines like this


followed by another thousands of lines (~380k lines) that contain /usr, /lib, /proc, /var and so on, examples:


And the lines don't contain my-own-folder. I highly doubt, that they're all connected to that folder. So why are they outputted?

Precise example

I want to apply a patch for openldap but don't know where I can find the folder in order to run the patch. So when I search

find / openldap
find / slapd

it seems to output all locations where the search is performed, and not only the occurences of openldap or slapd.


My fault was not to use -name in my search like this

find / -name "<searchterm>"

To limit the amount of output you can use either head or tail to read either first or last x number of terms. For example,

$ find . -iname "*test*" | head -n 10
find: `./.cache/dconf'./xterm-297/tektests
: Permission denied./file2.test

You can also use less to view the output conveniently with page-up / down buttons.

find / -type d -name "foldername" | less

You can also use ! -path to tell find to ignore some specific folder

find . ! -path /somedirectory/anotherdirectory -iname "searchterm"

of use grep -v termyoudontwant. -v flag allows to ignore stuff what you don't want to see in the output

find . -name "searchterm" | grep -v idontwantthatstuff `

Find allows specifying what type do you want to search for. If you want to search for folders, type is d. To search for all folders on the system named test, run this:

find / -type d -name "test"

Here's a concrete example to search for exact directory on my system:

$ sudo find / -type d -name "bin"                                              

If I want to ignore the /usr/ entries, I run this:

$ sudo find /   -type d -name "bin" | grep -v '\/usr\/*'                       

To ignore the directories /usr, /lib, /proc, /var , change grep like this

grep -v '\/usr\/*\|\/lib\/*\|\/proc\/*\|\/var\/*'

  • Even I didn't downvote, but pleas how could you see the output if you redirect both output and error? 2>&1 I think you shouldn't redirect the output also! I think your way of answering make some confusion. Please edit your answer and just answer the needed – Maythux Jun 17 '15 at 6:53
  • Maythux, I've removed that part. Thought OP didn't want any output from the command and just wanted to check the exit status $?, but I guess I was wrong. I looked through OP's edit, so it's more clear now – Sergiy Kolodyazhnyy Jun 17 '15 at 6:57
  • @Serg Not really making me happy, I still get a lot of output unrelated to my searchterm... – Gottlieb Notschnabel Jun 17 '15 at 7:03
  • @GottliebNotschnabel see my edit. I've added searching for directories only and there's also less command to make viewing long output convenient – Sergiy Kolodyazhnyy Jun 17 '15 at 7:09
  • Didn't use -name "" (see my question update). I accepted this answer as it's most helpful for searching in general. Thanks! – Gottlieb Notschnabel Jun 17 '15 at 7:19

You can use redirection:

find / <searchterm> 2>/dev/null

This redirect all errors to the null device /dev/null.

for example:

 # find / -name StewieGriffin\*
/root: Permission denied
/home/peterg: Permission denied

Would be converted to

# find / -name StewieGriffin\* 2>/dev/null

Please take a more look on this link.

  • Still shows me hundrets of lines that don't seem to be related to my search (see added example in my question) – Gottlieb Notschnabel Jun 17 '15 at 6:54
  • Didn't use -name "" (see my question update), thanks for your help! – Gottlieb Notschnabel Jun 17 '15 at 7:19

As you discovered, -name is the flag to use as it is built in to find. As a more general purpose solution which will cover you when other apps don't have a tool to rationalise search results (apt-get for example) you can use grep.

find / <searchterm> | grep <searchterm>

Don't use a blankspace between / and my-own-folder. Set full path to the folder like this:

# find /var/www/my-own-folder

This will only output the content of my-own-folder.

  • Uh, I thought the first statement after find is the place to start the search from? – Gottlieb Notschnabel Jun 17 '15 at 6:57
  • @gottliebNotschnabel technically, if you want to search just one particular folder, that's what you specify in the path. So if I want to search everything in /home/myusername/bin folder , I'd run find /home/myusername/bin -name "searchterm" Like that. Telling find to search from / means searching the whole system, all the folders – Sergiy Kolodyazhnyy Jun 17 '15 at 6:59
  • Uh okay, misunderstanding: I'm not looking for content of that path, but for all occurences of my-own-folder on the system. – Gottlieb Notschnabel Jun 17 '15 at 7:02
  • @GottliebNotschnabel so let me get this clear. You just wanna know all the occurrences of your folder. Only folder, no files, nothing else. Right ? What other things you *don't * want in the search ? I'm gonna update my answer in a second, please see – Sergiy Kolodyazhnyy Jun 17 '15 at 7:04
  • Basically, I want to find all occurences of searchterm, not only folders. I aedded a precise example to my question in order to specify – Gottlieb Notschnabel Jun 17 '15 at 7:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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