Web lists-archives.com

Re: An appropriate directory search tool?




On Fri, Oct 19, 2018 at 10:00:25AM -0500, Richard Owlett wrote:
> The "MATE Search Tool" comes close.
> 
> It can:
>   Select a starting directory.
>   Search for a specific extension.
>   Search for a keyword in file content.
> 
> It cannot:
>    Search ONLY the specified directory.
>    Return files that DO NOT contain a keyword.
> 
> I suspect what I want would most likely be what I'm looking for.
> "ls" can search by extension and stay in specified directory.
> It cannot include/exclude keywords.
> 
> My immediate problem involves only a couple dozen files so manual
> search is feasible.
> 
> Suggestions?

This would be 'find' (ok, with some little help from 'grep'). Hands down.

The man page can be a bit... intimidating, so I'd suggest to begin with
little examples and return to the man page from time to time until you
got the knack. Then it is worth it. Every bit.

To help you get started, I cooked up something implementing your "It
cannot" above. Here's the (commented) shell session:

  # Set lab rats up:
  tomas@trotzki:~$ mkdir d1 d1/d2
  tomas@trotzki:~$ cd d1
  tomas@trotzki:~/d1$ for f in f1 f2 f4 f6 d2/f1 d2/f2 ; do echo "hello world" > $f ; done
  tomas@trotzki:~/d1$ for f in f3 f5 d2/f3 ; do echo "wello horld" > $f ; done
  # Find all files containing the string "hell", limiting search
  # to one level of subdirectories (i.e. current dir, i.e. "d1"):
  tomas@trotzki:~/d1$ find . -maxdepth 1 -type f \( -exec grep -q "hell" {} \; -o -print \)
  ./f5
  ./f3
  # Same, but dropping the limit to one level:
  tomas@trotzki:~/d1$ find . -type f \( -exec grep -q "hell" {} \; -o -print \)
  ./d2/f3
  ./f5
  ./f3

Hope that tickled your curiosity :-)

Cheers
-- t

Attachment: signature.asc
Description: Digital signature