LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Search for string in entire filesystem? (https://www.linuxquestions.org/questions/linux-newbie-8/search-for-string-in-entire-filesystem-549717/)

sirius56 04-28-2007 10:08 AM

Search for string in entire filesystem?
 
Is there a way to search for a string in my entire filesystem? Not only filenames, but (non binary) text within files.

I tried

grep string /
grep string *
grep string

but these do not seem to work.

Thank you.

kaz2100 04-28-2007 10:18 AM

Hya,

I understand what you want to do. However, I do not recommend to proceed. While you check /dev /proc files, your Penguin may choke.

If you want to proceed, do at your risk. Look for -r option in grep.

Happy Penguins!

PS. Change your mind! Specify the directory you want to check. and If you want to exclude binary files, read manual for "find." It would be something like
Code:

find /your/search/path -exec grep string {} \; -print
Modify commands as necessary.

sirius56 04-28-2007 03:43 PM

Also went to root and tried

grep -r 'string' * (i.e., recursive)


Not quite sure if this worked because the search never ended.

Dark_Helmet 04-28-2007 04:31 PM

Quote:

Originally Posted by sirius56
grep -r 'string' * (i.e., recursive)


Not quite sure if this worked because the search never ended.

That's because the command recurses into the /dev directory and likely tries to read from your current TTY, but never gets an EOF (because you never type it in at the keyboard). This is the problem (or something similar) that kaz2100 was referring to.

Wanting to search for a string within files and in the filename itself will require either:
1) Two searches or
2) A shell script

For the two search method, I would do something like:
Code:

$ find / -type f \
        -path /dev -prune -o \
        -path /proc -prune -o \
        -path /srv -prune -o \
        -path /sys -prune -o \
        -print | grep "string_to_look_for"

$ find / -type f \
        -path /dev -prune -o \
        -path /proc -prune -o \
        -path /srv -prune -o \
        -path /sys -prune -o \
        -exec grep -l "string_to_look_for" {} \;

The first find command locates all files (-type f) on your system that are not in /dev, /proc, /srv, or /sys, prints their filenames, and then grep looks for a match to the string you are searching for. The second command does something similar, but it checks the contents of all your files for the same string. It will print out the filename of any file that contains a match.

To accomplish the same task with a single find command, you need a shell script. Something like this:
Code:

#!/bin/bash

if [ -z "${1}" ] ; then
  echo "This script expects a filename argument!"
  exit 1
fi

if [ -n "$( echo "${1}" | grep "string_to_look_for" )" ] ; then
  filename_match=1
else
  filename_match=0
fi

if [ -n "$( grep "string_to_look_for" "${1}" )" ] ; then
  file_content_match=1
else
  file_content_match=0
fi

if [ \( ${filename_match} -eq 1 \) -o \( ${file_content_match} -eq 1 \) ] ; then
  echo "${1}"
fi

exit 0

Make the script executable, and then issue this command:
Code:

$ find / -type f \
        -path /dev -prune -o \
        -path /proc -prune -o \
        -path /srv -prune -o \
        -path /sys -prune -o \
        -exec /path/to/new/scriptname {} \;

The script is not very pretty and I don't guarantee it will work without tinkering (because I have not tested it).

kaz2100 04-30-2007 07:10 PM

Hya,

I simply cannot believe it.

Happy Penguins!


All times are GMT -5. The time now is 02:53 AM.