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

10.4: A c-shell alias for searching via Spotlight UNIX
Tiger only hintThis hint is motivated by this earlier hint, and other stuff I've read elsewhere. I created a csh alias to search via Spotlight. Results are returned in chronological order and logically grouped.

Here is a detailed description of the construction of the alias, so that readers can modify it to suit their own needs. The alias has five parts connected by four pipes. To reassemble the alias, put the five parts on a single line and add it to your .cshrc file.

As a summary, here's how it works. If one types:
 $ spot xanadu 
mdfind is asked to find all files in the current directory containing xanadu in their metadata. The output is piped to ls -lt, to sort it into chronological order (most recent at the top). The result of this is then piped to sed for tidying. Read on for the details...

Line 1:
 alias spot 'mdfind -onlyin "`pwd`" \!* |
This takes the argument of spot, xanadu in the example, and presents it to mdfind with the restriction of searching in the current directory.

Line 2:
 sed s/^.\*\$/\"\&\"/g | 
mdfind returns full path names for the files, without escaping any spaces or other special characters. sed will wrap the full path name in quotes (" ") to protect special characters from the shell.

Line 3:
 xargs ls -lt |
xargs takes the wrapped path names and passes them to ls -lt, which will then sort them in chronological order.

Line 4:
 sed  s:"`pwd`"/:: |
sed will remove from the path name the part from root up to the current directory. The assumption being that the user already knows what directory she|he is in.

Line 5:
 sed "s/^[-dltrwx]*\ *[0-9]*\ [a-z]*\ *[a-z]*\ *[0-9]*//"'
Finally, sed will remove the output of ls -lt from the beginning of the line, up to the modification date of the file. Note that the last part has four escaped spaces.

Here is the alias as a single line:

alias spot 'mdfind -onlyin "`pwd`" \!* | sed s/^.\*\$/\"\&\"/g | xargs ls -lt | sed  s:"`pwd`"/:: | sed "s/^[-dltrwx]*\ *[0-9]*\ [a-z]*\ *[a-z]*\ *[0-9]*//"'

[robg adds: This works only in csh; bash experts are welcomed to weigh in with their 'how to make this work in bash' feedback. I tried, but failed. I tried using backslashes to break up the alias into a displayable five-line cut-n-paste alias, but that also failed (I think xargs kills it, somehow). Finally, if you just want to test the alias at the command line, not in .cshrc, you'll have to remove the double quotes around both instances of pwd.]
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[11,189 views]  

10.4: A c-shell alias for searching via Spotlight | 9 comments | Create New Account
Click here to return to the '10.4: A c-shell alias for searching via Spotlight' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A copout way to make it work with bash
Authored by: googoo on Mar 30, '06 08:12:36AM
This is a copout, but you could make a bash-executible file that duplicates the commands of the csh alias. This works for me:

#!/bin/bash
mdfind -onlyin "`pwd`" "$*" | sed s/^.\*\$/\"\&\"/g| xargs ls -lt | sed s:"`pwd`"/:: | sed "s/^[-dltrwx]*\ *[0-9]*\ [a-z]*\ *[a-z]*\ *[0-9]*//"

The shebang (#!) line should be the first line in the file, and everything else should be on the second line. Save the file with the name spot, and run chmod 755 spot. Then, move the file to a directory in your path.

-Mark

PS. Nice hint!



[ Reply to This | # ]
A copout way to make it work with bash
Authored by: lagroue on Mar 30, '06 08:26:16AM

:o) Same idea, same moment !

You use $* ? I used $@ !
Who is right ?



[ Reply to This | # ]
A copout way to make it work with bash
Authored by: googoo on Mar 30, '06 11:26:06AM

I think it works either way. When double-quoted, $* expands to all of your arguments separated by spaces in one set of quotes. $@ expands to all your arguments. (When double-quoted, it expands to all your arguments, each double-quoted.) I think your way ($@) is actually cleaner because you can control how the arguments are passed to mdfind, but my brief tests found no actual differences in the results. When I changed the word order and used my "$*" version, I got the same files. I have not checked, but I think mdfind parses out all the words and returns files that have each.

-Mark



[ Reply to This | # ]
Bash
Authored by: lagroue on Mar 30, '06 08:24:57AM

I don't know enough the alias command in bash.

But as ~/bin is in my $PATH, I created the /bin/spot file with this content:

#!/bin/sh
mdfind -onlyin "`pwd`" $@ \
| sed s/^.\*\$/\"\&\"/g \
| xargs ls -lt \
| sed  s:"`pwd`"/:: \
| sed "s/^[-dltrwx]*\ *[0-9]*\ [a-z]*\ *[a-z]*\ *[0-9]*//"
Remove the \ on the end of line to put the whole command on a single line - leave the #!/bin/sh alone, though.

Then I made it executable :

~$ chmod +x bin/spot

And there I go !

~$ spot spot
 Mar 30 18:16 bin/spot
...

Not great bash, I guess. Still, thanks for this useful spot command !



[ Reply to This | # ]
10.4: A c-shell alias for searching via Spotlight
Authored by: Melle on Mar 30, '06 09:02:32AM

How's about putting this in your .bash_profile:

function spot {
mdfind -onlyin "`pwd`" "$@" |
sed s/^.\*\$/\"\&\"/g |
xargs ls -lt |
sed s:"`pwd`"/:: |
sed "s/^[-dltrwx]*\ *[0-9]*\ [a-z]*\ *[a-z]*\ *[0-9]*//";
}



[ Reply to This | # ]
10.4: A c-shell alias for searching via Spotlight
Authored by: chleuasme on Mar 30, '06 10:47:46AM
or for example :
function spot {
    mdfind -0 -onlyin "$(pwd)" "$@" |
    xargs -0 ls -lt |
    sed "s:^[-dltrwx]* *[0-9]* *[a-z]* *[a-z]* *[0-9]*\(.*\)$(pwd)/\(.*\):\1  \2:"
}


[ Reply to This | # ]
$PWD
Authored by: sjk on Mar 30, '06 07:10:01PM

I'd suggest using the $PWD envariable to get the current working directory instead of running `pwd` processes.



[ Reply to This | # ]
$PWD
Authored by: chleuasme on Mar 31, '06 06:40:55AM
you're certainly right

and I correct a bug when using the code of my previous post (when searching from / for example) :

function spot {
    LOC=$( { [ "$PWD" = / ] && echo /; } || echo . )
    mdfind -0 -onlyin "$PWD" "$@" |
    xargs -0 ls -lt |
    sed "s:^[-dltrwx]* *[0-9]* *[a-z]* *[a-z]* *[0-9]* *\([^/]*\)$PWD:\1  $LOC:"
}
you can also delete everything concerning the LOC variable here, or prefer the use of something like mdfind -0 -onlyin "$PWD" "kMDItemDisplayName = '$1'cd"" at the second line for fast by name search (but that imply to invoque the function with an escaped argument : beware the shell fiename expansion, in particular '*' wildcard)

[ Reply to This | # ]
10.4: A c-shell alias for searching via Spotlight
Authored by: nicholst on Apr 08, '06 01:59:43PM

I'm a (t)csh user, so I didn't need to bashify. However I made an improvement that I think would be helpful for either shell.

The problem is that if your search hits on any directories then 'ls' will show you the contents of the directories, which isn't really desirable. So I recommend adding two more ls options, 'd' & 'F'. 'd' makes the directory name itself be listed instead of the contents, and 'F' let's you know the item is a directory by appending a '/' after the name.

So my alias now looks like

alias spot 'mdfind -onlyin "`pwd`" \!* | sed s/^.\*\$/\"\&\"/g | xargs ls -ldFt | sed s:"`pwd`"/:: | sed "s/^[-dltrwx]*\ *[0-9]*\ [a-z]*\ *[a-z]*\ *[0-9]*//"'

-T

[ Reply to This | # ]