Tuesday, October 26, 2021

[SOLVED] how to search for a line match from all files recursively found in a directory

Issue

Using linux commands, is there any way to know which file has the given line of code from a given project directory?

All directories are traced recursively for all files in subdirectories as well.

say your project is in php and is in directory /var/www/projectDir/

and the line to be searched is public function getProjectData( to know where (in which file lying in which subdirectory ) getProjectData php function is defined?


Solution

use the command

 find /var/www/projectDir/ -name '*.php'|xargs grep 'public function getProjectData('

the first portion

find /var/www/projectDir/ -name '*.php'

will search all the files which are having extension php and within directory /var/www/projectDir

the second portion

xargs grep 'public function getProjectData('

will search all the files found in first portion for public function getProjectData(.

xargs is used to consider the output of first portion as a standard input for second portion.

The symbol | named pipe will pipe the output to second portion

output

/var/www/projectDir/sub/directory/somephpfile.php:    public function getProjectData($param1, $param2) {

now you can open the file and search for which line the content is defined using

(ctrl + F for gedit) or ( ctrl + W for nano)

or use any of your favorite editor.



Answered By - veer7