Hi,
I would like to know how to get a list of files that contain a specific string inside them.
Thanks
if u know what is the string u r looking for,just mention it in ur ls command..
ls ur string
I think that the OP wants to list the files (not file name) that contains a string.
That's a job for grep.
# grep 'string' /path/to/files/*
all these tools can find 'strings' in your files
sed, awk, grep
for recursive search do
grep -R yourstring /path/to/dir
if grep -R is not available, use
find /path/to/dir -type f -print -exec grep yourstring {} \;
find /path/to/dir -type f -print -exec grep -l yourstring {} /dev/null \;
find /path/to/dir -type f -exec grep -l yourstring {} \;
if you use grep -l
you must use find without -print
whats the /dev/null
for? do you mean 2>/dev/nul
l??
Sorry, I meant to take out the print, good catch.
The /dev/null is just a trick to force grep to output the file name, it will not always do so if only one file is listed as happens in find. If I run the command you posted I get, as I expected, a list of all the files (matching the pattern or not ) , and the output from grep for each file where a match occurs.
find /searchdir -type f -exec grep -l "some string" {} /dev/null \;
or
find /searchdir -type f | xargs grep -l "some string"
give me a list of files in which the match occurs, in a well behaved grep I should not need the /dev/null, unless I want to use the grep without -l and print the matched lines and the files in which they occur.
nice trick, your solution is much easier to read, thanks for that
you can use..awk command
awk '/serch_string/' file_name
What if i dont know the filename?
if the user wants to search for all files which contain a specific string inside the file?
for example, i want to search for all the files which have the text "woodstock" ?
This was already posted in the same thread
find . -name '*.*' -print | xargs grep -il 'pattern'
This helped me with what I was looking for - thanks!