10

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

/var/www/my-own-folder/index.php
/var/www/my-own-folder/lib/1.php
/var/www/my-own-folder/templates/2.php
/var/www/my-own-folder/log/3.log

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

/etc/gshadow
/etc/fonts
/dev/ram7
/media/user
/run/udev/watch
/bin/gzexe
/vmlinuz
/lost+found

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.

Update

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

find / -name "<searchterm>"
dialogik
  • 273

4 Answers4

8

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
/home/stewie/StewieGriffin-resume.doc

Would be converted to

# find / -name StewieGriffin\* 2>/dev/null
/home/stewie/StewieGriffin-resume.doc

Please take a more look on this link.

Maythux
  • 84,289
2

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
./xterm-297/tektests/imtesth.tek
./xterm-297/tektests/fotest.tek
./xterm-297/tektests/aitest.tek
./xterm-297/tektests/imtest.tek
./xterm-297/testxmc.c
./xterm-297/Tests
./xterm-297/vttests
./test.c
: 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"                                              
/home/xieerqi/bin
/usr/lib/initramfs-tools/bin
/usr/lib/klibc/bin
/usr/lib/pm-utils/bin
/usr/lib/2013.com.canonical.certification:checkbox/bin
/usr/lib/i386-linux-gnu/qt4/bin
/usr/lib/ure/bin
/usr/lib/2013.com.canonical.certification:plainbox-resources/bin
/usr/lib/python3/dist-packages/plainbox/impl/providers/stubbox/bin
/usr/lib/guile-2.0/bin
/usr/share/qt4/bin
/usr/share/libreoffice/bin
/usr/src/linux-headers-3.16.0-30/tools/testing/selftests/rcutorture/bin
/usr/local/bin
/usr/bin
/bin

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

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

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

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

Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
  • 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 06: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 06:57
  • @Serg Not really making me happy, I still get a lot of output unrelated to my searchterm... – dialogik Jun 17 '15 at 07: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 07:09
  • Didn't use -name "" (see my question update). I accepted this answer as it's most helpful for searching in general. Thanks! – dialogik Jun 17 '15 at 07:19
  • @GottliebNotschnabel Well that was easy , right ? :D Glad I could help ! – Sergiy Kolodyazhnyy Jun 17 '15 at 07:21
1

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>
elmato
  • 111
0

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.

krt
  • 1,956
  • 15
  • 12
  • Uh, I thought the first statement after find is the place to start the search from? – dialogik Jun 17 '15 at 06: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 06:59
  • Uh okay, misunderstanding: I'm not looking for content of that path, but for all occurences of my-own-folder on the system. – dialogik Jun 17 '15 at 07: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 07: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 – dialogik Jun 17 '15 at 07:10
  • @GottliebNotschnabel see my addditions to the answer – Sergiy Kolodyazhnyy Jun 17 '15 at 07:17