Skip to content

Using grep to search text in the files from a folder

April 9, 2009
tags:

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
Advertisements
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: