Operating System - HP-UX
1834772 Members
3275 Online
110070 Solutions
New Discussion

Re: Want to find files which contains a string from current directory

 
Ezhilarasan
Occasional Advisor

Want to find files which contains a string from current directory

Hi,

I want to find files which contains a string from current directory.
What is the command for this ?

Thanks
R.Ezhil
7 REPLIES 7
Ramkumar Devanathan
Honored Contributor

Re: Want to find files which contains a string from current directory

$ find . | xargs grep ""

$ find . -exec grep "" {} \;

Script -

#!/usr/bin/ksh

for file in `find .`
do
if [[ `file $file` = "$file: ascii text" ]]; then
grep "" $file
fi
done

#EOF

Use -ok instead of -exec to prompt you before searching a file.
HPE Software Rocks!
Ravi_8
Honored Contributor

Re: Want to find files which contains a string from current directory

Hi,

$grep "string to be found" *

list all the files in the current directory containing the string "string to be found"
never give up
Michael Tully
Honored Contributor

Re: Want to find files which contains a string from current directory

Hi,

I'm assuming your attempting to find all files that have the text string 'test':

$ grep -i string *

If this i not what you looking for, please give us some additional information.

Regards
Michael
Anyone for a Mutiny ?
Bill Douglass
Esteemed Contributor

Re: Want to find files which contains a string from current directory

For plain text files (shell scripts, perl or c code, config files)

grep "string" *

should do fine.

If you have binary files (compiled code, libraries, etc) then you could do

grep -l "string" file

would list the file with matching textm without dumping long lines of binary output on your terminal screen.

You could also set up a loop and process files using the strings command:

for i in `ls`; do if strings $i | grep -q "string"; then printf "$i: "; strings $i | grep "string"; fi; done

This will print the name of each file and the natching text ("string") in your current directory.

Explanation:

for i in `ls`; do -
get a list of files in the current directory and assign to $i variable for each pass though the loop.

if strings $i | grep -q "string" -
Test for presence of string "string" in file. Use -q options on grep to set return code without printing any output.

printf "$i: "; strings $i | grep "string" -
Print file name, and any strings found that match "string"
Ramkumar Devanathan
Honored Contributor

Re: Want to find files which contains a string from current directory

Ezhil, if you want the filenames alone then use this -

$ find . | xargs grep "" | cut -d: -f1 | sort | uniq

- ramd.
HPE Software Rocks!
Ramkumar Devanathan
Honored Contributor

Re: Want to find files which contains a string from current directory

Ezhil,

It's a good practice to assign points (part of itrc forums etiquette) to responses.

- ramd.
HPE Software Rocks!
Tim Sanko
Trusted Contributor

Re: Want to find files which contains a string from current directory

I am so lazy I don't care about fooling with executables.

If you are looking for something in a compiled program here is my quick and dirty.

find . -exec strings {} \;| grep |

Tim