我有一个约350张图像的文件夹(它们是扫描食谱)。为了更容易找到它们,我在bash shell脚本中编写了一个搜索程序。我在Mac OS X 10.8 Mountain Lion上有bash版本3.2。
我的计划的基本想法:
目录是什么样的:
+Recipes
-files
-files
-files
+.search
-search (shell script)
+results
-links
-links
这是我的代码:
#!/bin/bash
#
# Search program for recipes.
# version 2
# Clean out results folder
rm -rf ~/Google\ Drive/Recipes/.search/results/*
# Display the search dialog
query=$(osascript <<-EOF
set front_app to (path to frontmost application as Unicode text)
tell application front_app to get text returned of (display dialog "Search for:" default answer "" with title "Recipe Search")
EOF)
# If query is empty (nothing was typed), exit
if [ "$query" = "" ]
then
exit
fi
echo "Searching for \"$query\"."
# Search for query and (hard) link. The output from 'ln -v' is stored in results
# 'ln' complains if you search for the same thing twice... mdfind database lagging?
results=$(mdfind -0 -onlyin ~/Google\ Drive/Recipes/ "$query" | xargs -0 -J % ln -fv % ~/Google\ Drive/Recipes/.search/results)
if [ "$results" ]
then
# Results were found, open the folder
open ~/Google\ Drive/Recipes/.search/results/
else
# No results found, display a dialog
osascript <<-EOF
beep
set front_app to (path to frontmost application as Unicode text)
tell application front_app to display dialog "No results found for \"" & "$query" & "\"." buttons "Close" default button 1 with icon 0
EOF
fi
第一次工作正常。如果你两次搜索相同的东西,它会中断。
说明:让我说我搜索“鸡”。 34个文件匹配,并在结果目录中创建硬链接。
现在,我再次运行程序,并搜索相同的东西 - “鸡”。该目录已清空(rm
)。但现在,发现/链接停止工作只有6或7个食谱将被链接。似乎正在发生的事情是mdfind
在搜索目录中找到结果,在删除后,然后ln
无法建立链接。但它没有找到主要文件......
我得到了
ln: ~/Google Drive/Recipes/.search/results/recipe.jpg: no such file or directory
我看了mdfind used for creating symlinks not working as expected;他们有类似的问题(但没有帮助)。
感谢任何帮助......这让我很长时间都很烦恼。
答案 0 :(得分:1)
您可以使用mdimport
重新索引目录或文件。
$ touch aa
$ mdfind -onlyin . -name aa
/Users/lauri/Desktop/aa
$ rm aa
$ mdimport .
$ mdfind -onlyin . -name aa
$
Spotlight不会为以句点开头的目录编制索引,因此您只需重命名.search
目录。
答案 1 :(得分:0)
Spotlight命令使用文件内容的缓存索引。我不知道有什么办法迫使mdfind
不这样做。蛮力方法是在删除文件后用mdutil -E
清除缓存,但您可能不想这样做。