A quick way to search for certain lines of code through many files in a project

On Linux/Unix there's a command line tool called grep you can use it to search multiple files for a string. For examples if I wanted to search for strcpy in all files:

~/sandbox$ grep -rs "strcpy"*
test.c:    strcpy(OSDMenu.name,"OSD MENU");

-r gives searches recursivly so you get all the files in all directories (from the current one) searched. -s ignores warnings, in case you run into non-readable files.

Now if you wanted to search for something custom, and you can't remember the case there's options like -i to allow for case insenstive searches.

~/sandbox$ grep -rsi "myint" *
test.c:    int myInt = 5;
test.c:    int MYINT = 10; 

You can also use regular expressions in case you forgot exactly what you were looking for was called (indeed the name, 'grep' comes from the sed command g/re/p -- global/regular expression/print:

~/sandbox$ grep -rsi "my.*" *
test.c:    int myInt = 5;
test.c:    int MYINT = 10;
test.c:    float myfloat = 10.9;

install cygwin if you aren't using *nix and use find/grep, e.g.

find . -name '*\.[ch]' | xargs grep -n 'myfuncname'

You can use grep to search through the files using the terminal/command line.

grep -R "string_to_search" .

-R to be recursive, search in all sub directories too

Then string you want

Then is the location, . for the current directory

Tags:

Linux

Search