How to find a function from a folder on linux

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

+7
source share
3 answers

Try to do it

using :

grep -Hri function_name . 

if you want only the path:

 grep -ril function_name . 

Explanation

  • trailing . indicates current directory
  • -i : case insensitive
  • -r : recursive
  • -H : print file name for each match
  • -l : suppresses normal output; instead, type the name of each input file from which output is usually printed.

See man grep

And last but not least

An interesting tool is ack , it will avoid searching in .svn , .cvs , .git dirs, etc. It is designed to search for 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 xsubpp program. core_perl/overload/numbers.pm 5:# Copyright (C) 2008 by Larry Wall and others core_perl/CPAN.pm 876:# From: Larry Wall < larry@wall.org > 

or just the 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 to complete . using ack (compared to grep )

+12
source

This will go through all the files in the directory that you opened, and in any subdirectories (although it gets confused if there are spaces in the file or directory name), it will look for all of them for the FUNCTION that you put, and will output the file path and show the corresponding line. Run it cd in the directory you opened (and replace FUNCTION with the function you need).

 find . -type f | xargs grep FUNCTION 
0
source

Try using cscope . You also need to install vi / vim.

After installing it, simply run the following command in the folder in which you want to find the function:

 cscope -R 

After that, a menu will appear showing a lot of options for finding all characters, defining a function, functions that call the function, etc.

0
source

All Articles