M HYPE SPLASH
// general

How to use find quietly?

By John Campbell

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>"
7

4 Answers

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.

4

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\/*'

6

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.

6

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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