Archive for the tag 'text'

SBDavid

Find text in a large number of files

Find text in a large number of files

If you need to find a string in a file, you would typically use:

grep -H “string” file-name.ext

However, grep doesn’t handle a large number of files well. If you specify grep “string” * or even grep “string” `find ./`you may find yourself facing this error:

bash: /bin/grep: Argument list too long

Simple bash script to do the searching.

In this sample, We will be looking for a string “welcome” in a directory named “./Document/”:

for i in `find ./Document/`; do grep -H “welcome” $i; done

This uses the find command to do the searching. It actually returns a list of filenames, which we can then grep one-by-one. The -H option tells grep to let us know the filename it found the string in so we can go right into that file to find the location of it.