Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

10.4: Search current directory using an mdfind alias UNIX
Tiger only hintThe Unix mdfind utility lets you search files using the Spotlight index from the Terminal. This is a great tool, but it doesn't quite replace find, which makes it very easy to search the current directory.

So here's a shell alias for a new spot command line tool which performs a Spotlight search only within the current directory:
alias spot='mdfind -onlyin `pwd`'
Example:
/Users/alf/Documents# spot run
/Users/alf/Documents/iStumbler/Bug Reports/istumbler-runaway-20050731.txt
/Users/alf/Documents...etc.
Add the alias to your .profile (or .bash_profile) file to automatically add it to every new shell.

[robg adds: If you're using tcsh, the format will be slightly different: alias spot 'mdfind -onlyin `pwd`'. I tested this one, and it works quite nicely.]
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[10,246 views]  

10.4: Search current directory using an mdfind alias | 6 comments | Create New Account
Click here to return to the '10.4: Search current directory using an mdfind alias' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.4: Search current directory using an mdfind alias
Authored by: bdm on Aug 25, '05 09:58:38AM
The -onlyin parameter of mdfind does not obey the usual conventions for Unix commands. For example, you can't write mdfind -onlyin . to search the current directory. You can also specify non-existent directories and still get output (try it). The man page doesn't mention any of this.

[ Reply to This | # ]
Full directory path?
Authored by: lullabud on Aug 25, '05 02:29:57PM

You're right, it seems to require a full directory path. That's very odd... Hopefully that'll be fixed soon.



[ Reply to This | # ]
More generalized version
Authored by: ferret-slayer on Mar 08, '06 08:43:38AM

[robg adds: If you're using tcsh, the format will be slightly different: alias spot 'mdfind -onlyin `pwd`'. I tested this one, and it works quite nicely.]

This doesn't work if any directory or disk in the path has a space in the name. A more generalized version would be:

alias spot 'mdfind -onlyin "`pwd`"'



[ Reply to This | # ]
optimizing
Authored by: sjk on Aug 25, '05 06:54:03PM

Using $PWD instead of `pwd` (also changing the single quotes to double quotes so $PWD evaluates) interpolates the current working directory into the alias without running the pwd command.



[ Reply to This | # ]
optimizing
Authored by: stevev on Aug 29, '05 04:20:39PM

actually, you want the single ticks for bash so that $PWD does *not* evaluate in the alias command. by using the double ticks ("), $PWD is evaluated once and not when you are using it later (most likely in a different directory). all of this is said using bash in 10.4 btw.

hope this helps.



[ Reply to This | # ]
quoting
Authored by: sjk on Aug 30, '05 01:23:04AM

D'oh! You're absolutely right. Thanks for that correction.



[ Reply to This | # ]