Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

search

I am currently working on a C project that contains over 50 .h and .c files. I would like to know if there is a quick way to search for certain lines of code (like ctrl+f for a window for example) without having to actually search each file one by one.

Thank you in advance

like image 722
Adam Avatar asked Mar 19 '14 13:03

Adam


People also ask

How do I search for a string in multiple files?

To search multiple files with the grep command, insert the filenames you want to search, separated with a space character. The terminal prints the name of every file that contains the matching lines, and the actual lines that include the required string of characters. You can append as many filenames as needed.

Which command can be used to display the contents of multiple files to the screen at once?

You can also use the cat command to display the contents of one or more files on your screen. Combining the cat command with the pg command allows you to read the contents of a file one full screen at a time. You can also display the contents of files by using input and output redirection.

How do you find a word in all files in a directory?

Finding text strings within files using grep -R – Read all files under each directory, recursively. Follow all symbolic links, unlike -r grep option. -n – Display line number of each matched line. -s – Suppress error messages about nonexistent or unreadable files.


2 Answers

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;
like image 130
Mike Avatar answered Sep 22 '22 09:09

Mike


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

find . -name '*\.[ch]' | xargs grep -n 'myfuncname'
like image 32
Peter - Reinstate Monica Avatar answered Sep 24 '22 09:09

Peter - Reinstate Monica