I am trying to list all of the files including a function, e.g., matrixCal.

How to do that in linux. Thanks.

Go to your root directory:

# sudo find . | xargs grep -i 'NameOfFunction'

grep is the tool to use.

If possible, can you give a short explanation on how does this linux command

sudo find . | xargs grep -i 'NameOfFunction'

work? Thanks.

Go to your root directory:

# sudo find . | xargs grep -i 'NameOfFunction'

grep is the tool to use.

From anywhere on your system:

find / -iname "matrixcal"

If needed you can use jokers, e.g. "matrixcal*".
If you want to search case sensitive, use -name option instead.
By using the path "/", you can search from anywhere, find will always start from the root.

If possible, can you give a short explanation on how does this linux command

sudo find . | xargs grep -i 'NameOfFunction'

work? Thanks.

It simply uses the pipe operator and xargs to pass output from the find command into the grep command:

1. sudo runs the command as root (after prompting you for a password) allowing the find command to list system files owned by root and files owned by other users

2. find . will cause the find command to list all files it finds starting from the current directory

3. The pipe operator "|" passes/redirects the output from the find command to grep via the xargs command.

4. grep -i 'matrixCal' will search the listed files and output any which contain the text 'matrixCal'.
The "-i" switch causes grep to ignore the case of the search string.

That's basically how Nonshatters suggested command works. But running that will soon fill your terminal with results and error messages from grep and will take a long time to run as it will search every file in the file-system.

One way of speeding up searches is to use a built-in update script to update the database that find uses. Running this from time to time can aid in speedier searches.
I tend to run this at least once a month, if not once a week.
I usually do it manually, but you could set up a cron job to run it automatically if you like.
Anyway, to run find's update script manually just type:

sudo updatedb

And that will rebuild the database and should make file-searches a bit faster.

Going back to Nonshatters command:
If you use / instead of . your search will always begin at the root of the filesystem, so you won't have to cd to / before running the command (as pointed out by FireWolf).
Also if you add the '-type f' option to the find command, it might make the search a little faster as it would only search ordinary plain files, rather than symbolic links and other special file-types.
e.g.

sudo find / -type f | xargs grep -i 'matrixCal'

Also if you know that the function is only going to appear in certain file-types, for example .h and .cpp files, you could speed up your search even further by doing this:

sudo find / -name '*.cpp' -o -name '*.h' -type f | xargs grep -i 'matrixCal'

The -o option is an OR, so the find part of the above command says:
'find any normal files where the name is *.cpp or *.h'

Or if you wanted you could use a regular expression to do the same thing. By default find uses the EMACS regex type, but you can change it to use another regex format.
Anyway, using an EMACS regex to search inside .h and .cpp files for the function:

sudo find / -regex '.*\.\(h\|cpp\)' -type f | xargs grep -i 'matrixCal'

Note: In the EMACs regex format the backslashes 'escape' the character that follows it. It looks a little wierd, but otherwise things like the parenthesis in the regex would be taken as literals. whereas with the backslashes, they indicate that we're performing a conditional operation in the regex!

Obviously if you're using another programming language, you can substitute in any other file-types that you might be interested in!

Now, lets tidy the output up a bit.
For example, we could redirect the output of grep to be written to a text file on your desktop, which you can view at your leisure. And we can redirect any error messages from grep to /dev/null. Effectively error messages will be ignored, deleted, removed...However you choose to interpret it!

/dev/null (AKA the bit bucket, the black hole, the void etc. ) is a special file which discards all data written to it, but which reports that the write operation succeeded. So it's a great place to redirect output that you don't want to see!

Anyway, to do all this would yield the following command:

sudo find / -regex '.*\.\(h\|cpp\)' -type f | xargs grep -i 'matrixCal' > ~/Desktop/results.txt  2> /dev/null

The '>' operator redirects the stdout of grep to the text file.
The '2>' operator redirects grep's stderr to /dev/null, effectively hiding the irrelevant error messages. Alternatively, you could redirect the errors to a separate text file if you wanted to keep them for future reference.

Leaving that to run will give you a text-file full of results and won't fill your screen with error messages about files that it didn't have permission to access, or which couldn't be found (thanks to xargs tokenisation of filenames with spaces in!)

Which is a point. I failed to mention this, but the commands shown here (including NonShatter's) will not like file-names that contain spaces. File-names with spaces will be split up as separate tokens by xargs and will cause grep to throw an error (as it won't be able to find the file!).

In Linux you can perform very complex operations simply by piping several commands and redirecting their input/output. The commands shown here are prime examples of this!

Hope this has helped!

From anywhere on your system:

find / -iname "matrixcal"

If needed you can use jokers, e.g. "matrixcal*".
If you want to search case sensitive, use -name option instead.
By using the path "/", you can search from anywhere, find will always start from the root.

This will only list files called 'matrixcal' or in your additional blurb, files where the filename starts with 'matrixcal'. But the OP was looking for a way of listing all files which contain calls to a function called matrixCal. So your suggestion will not work! But you are correct about using / rather than .!

Well noted JasonHippy. You have provided a very complete description!

This is was most helpful...
Lot's of explanation, precisely what I needed.

Thanks U guys!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.