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).