Using grep to search text in the files from a folder
2009 April 9
I received some Java projects, including webservices (J2EE) and clients to work with. These projects were created using Netbeans, so their build scripts were not exactly working from the command line and I had to do some manual search and replace operations. To do this, I used the command line tool grep. It is one of the most useful command line tools that the Linux userland offers.
To search for text in files from the current folder (or directory, to follow Linux nomenclature), one can write:
grep OperatorType -R . -n --include=*.java --color
The parameters are as follows:
- text
- the actual text to search for
- -R
- Recurse to subdirectories
- .
- start from the current directory
- –include=*.java
- specifies a regular expression that searchable files must match (in my case, search only files with the “java” extension)
- –color
- makes the output pretty
- -n
- shows the lines numbers of the lines that matched your search text