I'm pretty sure there are literally tens of (possibly easier) ways to accomplish this, but I've always used the following snippet of bash code to search for text in files (the backslash at the end of the first line indicates continuation and should not be typed in; the two lines below are really one big command, but they are split into two separate lines so it doesn't screw up the page layout on lower resolutions):
for i in *.txt; do echo $i:; cat -n $i | \
grep -i 'search_string'; done
First, "*.txt" is expanded to a list of all files ending in '.txt'. For example, "one.txt two.txt three.txt". Then, $i is set to one of the files (in the order they are listed) at each iteration of the for loop. The commands between the 'do' and 'done' are executed for each file. The name of the current file is printed. Then, the output of `cat -n $i`, which is the contents of file $i (with each line prefixed with the line number), is piped to `grep -i 'search_string'`. This searches for the given text in file $i (ignoring case).
Of course, you can change the above snippet to search for whatever you want, in whatever files you specify. For example, to search for the string 'while' in all C files (.c) in the current directory, change "*.txt" to "*.c" and 'search_string' to 'while'. Or if you want to search in both C files and header files (.h), then use "*.c *.h". You can even use the find utility, as shown here:
for i in `find /some/path -iname "*.html" -print`; \
do echo $i:; cat -n $i | grep -i 'search_string'; done
This is exactly the same as the first command, except "*.txt" was replaced with `find ...`. The back-ticks (`) are substituted with the output of the command they enclose, in this case, the find command. The find command used here prints the absolute paths of all HTML files (i.e. ending in .html) in /some/path, including those in sub-directories, sub-sub-directories, ..etc. These files are then searched for 'search_string'.
Feel free ask questions or post what you personally use when searching for text in files.
Recent Comments