21 de mayo de 2012

Find what you are looking for with grep + find + xargs

Sometimes (very often) you are looking for this particular word of string that you think is in one file of a given project... but you don't know which file, and the project has a zillion files. And you feel like crying.

Well, don't, that's what this post is for :)

Let's go for the command:

$ find -iname | xargs grep -in

Where:
[1] a folder name: for instance, to look from the current folder, use ".".
[2] for instance, for files ending in .py, use this: "*.py"
[3] a string to look for. Case insensitively.

Example: Let's look for the string "hello", case insensitively, between the files of the current folder and its subfolders, inside files with filename ending in .py:

$ find . -iname '*.py' | xargs grep -in 'hello'

The output will say the file(s) and line numbers in those files where 'hello' was found.

1 comentario:

humitos dijo...

Está bueno esto. Es una buena alternativa a "git grep" cuando no estás usando un sistema de control de versiones GIT :)