11

I downloaded some source codes of a program and put it in a folder I want to find a function declaration from the folder on terminal, how to do it with shell command? thanks!

2
  • 2
    What do function-declarations look like in this source-code? Commented Mar 28, 2013 at 23:08
  • I just want to look for the string from a folder, in the folder there are many files, sub-folders Commented Mar 28, 2013 at 23:11

3 Answers 3

17

Try doing this

using :

grep -Hri function_name .

if you want only the path :

grep -ril function_name .

Explanations

  • the trailing . stands for current directory
  • -i : case-insensitive
  • -r : recursive
  • -H : Print the file name for each match
  • -l : Suppress normal output; instead print the name of each input file from which output would normally have been printed.

See man grep

Last but not least

An interesting tool is ack, it will avoid searching in .svn, .cvs, .git dirs and such... It is designed to search code.

Example :

$ cd /usr/share/perl5
$ ack -r 'Larry\s+Wall'
site_perl/Inline/C.pm
370:# bindings to. This code is mostly hacked out of Larry Wall's xsubpp program.

core_perl/overload/numbers.pm
5:#    Copyright (C) 2008 by Larry Wall and others

core_perl/CPAN.pm
876:#       From: Larry Wall <[email protected]>

or just file path :

$ ack -rl 'Larry\s+Wall'
vendor_perl/LWP.pm
site_perl/Inline/C.pm
core_perl/overload/numbers.pm
core_perl/CPAN.pm
core_perl/SelfLoader.pm
core_perl/AutoLoader.pm
core_perl/AutoSplit.pm
core_perl/Test/Harness.pm
core_perl/XSLoader.pm
core_perl/DB.pm

No need the ending . with ack (compared to grep)

Sign up to request clarification or add additional context in comments.

1 Comment

If you want to refine your search to specific file types and still use grep, you can use --include=*.<file_postfix>.
2

Try using CScope. You also need to have vi/vim installed.

After you install it, just run the following command in the folder you want to find the function:

cscope -R

After that, a menu will appear, showing many options to find all symbols, the function definition, functions which calls the function, etc.

Comments

0

This will go through all the files in the directory you opened and any subdirectories (though it messes up if there are spaces in a file or directory name), will search all of them for the FUNCTION that you put, and will output the path to the file and show the matching line. Run it by cding to the directory you opened, (and replace FUNCTION with the function you're looking for).

find . -type f | xargs grep FUNCTION

2 Comments

why there is a - in between?
The - is necessary, but should be attached to the word type. I'll fix it, thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.