Friday, April 8, 2022

[SOLVED] What is/how does the "search" command work?

Issue

Put briefly, can I use the 'search' command to search through file contents? If so, how?

I am trying to find an easier way to search through file contents in Linux Mint than having to type

grep -rnw . -e 'my search text' 

I just noticed there is a 'search' command

$ which search
/usr/local/bin/search

However, when I look at its help text; I am presented with this:

usage: search [arguments] [options]
arguments:
     for text
     in directory

I am unable to make sense of this, any arguments I try passing in seem to inevitably lead me to the same text.

There is also no man page entry, I tried that as well.

I have tried e.g.

$ search "my search text" .
$ search . "test"
$ search . . . .

Lacking a way to get this to work, I may opt for an alias. Sadly though, 'search' is already taken..


Solution

I just did a clean install, and went to have a look at this 'search' executable. Turns out, it is in fact a shell command with the following resulting command:

find $directory -type f -exec grep -$case$verbose "$text" --color=auto -n {} \;

The four $variables above are respectively populated with the search directory, case-sensitivity, verbosity and ultimately the single word search text.

I have no idea why this facility exists, but as the manual suggests, you can only use it to search for a single word (no spaces), and in the specified following directory.

An example looking for (case insensitive) and without verbosity would thus be:

search for bar in ~/home/foo

The command was apparently added in Linux mint in 2008, as indicated in this post that also details its use.



Answered By - Joeppie
Answer Checked By - David Goodson (WPSolving Volunteer)