user138126
user138126

Reputation: 1003

how to find a function from a folder on linux

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!

Upvotes: 11

Views: 22002

Answers (3)

William
William

Reputation: 371

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.

Upvotes: 2

Gilles Quénot
Gilles Quénot

Reputation: 185035

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)

Upvotes: 17

Scott Mermelstein
Scott Mermelstein

Reputation: 15397

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

Upvotes: 0

Related Questions