Mac-Osx

MacOSX 和定位以在 iCloud Drive 上查找文件

  • September 1, 2017

我是 Linux 管理員,我習慣於使用 locate 命令。我不記得該命令是 OSX 原生的還是我使用 brew 安裝的。

您可能知道 locate 與 find 不同,因為它創建了一個數據庫,允許從終端進行快速搜尋。刷新數據庫的正常 Linux updatedb 命令對我不起作用,我必須使用:sudo /usr/libexec/locate.updatedb. 配置文件也位於:/etc/locate.rc.

我希望能夠在 iCloud Drive 上找到文件。問題是即使它設置為索引整個“/”系統,locate 也不會索引 iCloud Drive,它位於:~/Library/Mobile Documents/com~apple~CloudDocs.

我什至將出於測試目的添加到配置文件中並執行列出的更新命令。但是,locate 永遠不會在 iCloud Drive 上找到文件。

我注意到 locate 命令手冊頁上寫著:

The locate database is typically built by user ``nobody'' 
and the locate.updatedb(8) utility skips directories which are not
readable for user ``nobody'', group ``nobody'', or world.  
For example, if your HOME directory is not world-readable, 
none of your files are in the database.

所以也許我需要做一些技巧並將使用者nobody 添加到我的使用者組中,但是我以前從未聽說過nobody 使用者。如果我想將我的普通使用者組附加到nobody 使用者,也沒有usermod 命令。

你們中的任何一個聰明人有什麼建議來解決這個奇怪的請求嗎?

我會跳過嘗試對locate文件進行索引,並使用 macOS 的類似但功能更強大的系統 Spotlight。與 不同locate,它索引所有內容(但隨後將輸出限制為執行查找的使用者可讀的文件)。它還不斷更新,並索引更多的文件屬性而不僅僅是文件名。它可以在命令行中使用mdfindcommand。預設情況下,它會在所有文件的索引屬性中搜尋您提供的任何文本;如果您只想按名稱搜尋,請使用以下-name選項:

$ mdfind -name icloud-file
/Users/gordon/Library/Mobile Documents/com~apple~CloudDocs/Example-iCloud-File-2.txt
/Users/gordon/Library/Mobile Documents/com~apple~CloudDocs/Example-iCloud-File.rtf

請注意,它不使用與 ; 相同的查詢語法locate-name只是進行不區分大小寫的名稱包含查找。使用Spotlight 的元數據查詢表達式語法,您可以變得更有趣:

$ mdfind "kMDItemFSName == iCloud-File"    # this does an exact-match search, so no matches
$ mdfind "kMDItemFSName == *iCloud-File*"    # Wildcards to the rescue!
/Users/gordon/Library/Mobile Documents/com~apple~CloudDocs/Example-iCloud-File-2.txt
/Users/gordon/Library/Mobile Documents/com~apple~CloudDocs/Example-iCloud-File.rtf
$ mdfind "kMDItemFSName == *iCloud-File* && kMDItemContentType == public.plain-text"
/Users/gordon/Library/Mobile Documents/com~apple~CloudDocs/Example-iCloud-File-2.txt

要更好地了解它可以搜尋哪些屬性,請使用mdls

$ mdls "/Users/gordon/Library/Mobile Documents/com~apple~CloudDocs/Example-iCloud-File-2.txt"
_kMDItemOwnerUserID            = 501
kMDItemContentCreationDate     = 2017-09-01 19:06:49 +0000
kMDItemContentModificationDate = 2017-09-01 19:07:06 +0000
kMDItemContentType             = "public.plain-text"
kMDItemContentTypeTree         = (
   "public.plain-text",
   "public.item",
   "public.text",
   "public.data",
   "public.content",
   "public.plain-text"
)
kMDItemDateAdded               = 2017-09-01 19:07:06 +0000
kMDItemDisplayName             = "Example-iCloud-File-2.txt"
kMDItemFSContentChangeDate     = 2017-09-01 19:07:06 +0000
kMDItemFSCreationDate          = 2017-09-01 19:06:49 +0000
kMDItemFSCreatorCode           = ""
kMDItemFSFinderFlags           = 0
kMDItemFSHasCustomIcon         = (null)
kMDItemFSInvisible             = 0
kMDItemFSIsExtensionHidden     = 0
kMDItemFSIsStationery          = (null)
kMDItemFSLabel                 = 0
kMDItemFSName                  = "Example-iCloud-File-2.txt"
kMDItemFSNodeCount             = (null)
kMDItemFSOwnerGroupID          = 20
kMDItemFSOwnerUserID           = 501
kMDItemFSSize                  = 22
kMDItemFSTypeCode              = ""
kMDItemKind                    = "Plain Text Document"
kMDItemLogicalSize             = 22
kMDItemPhysicalSize            = 4096
kMDItemUserCreatedDate         = (
   "2017-09-01 19:06:49 +0000"
)
kMDItemUserCreatedUserHandle   = (
   501
)
kMDItemUserModifiedDate        = (
   "2017-09-01 19:06:52 +0000",
   "2017-09-01 19:07:06 +0000"
)
kMDItemUserModifiedUserHandle  = (
   501,
   501
)

(雖然這實際上是不完整的——例如,它不包括文本文件的內容,它是完全索引和可搜尋的。)

引用自:https://serverfault.com/questions/871310