Macでファイルにタグを付ける

MacのFinderでファイルを右クリックして「情報を見る」を選ぶと,次のような画面が現れる。ここにタグを複数設定することができる。次のファイルにはタグ「Deep Learning」「LeCun」「Bengio」「Hinton」「2015」が設定されている。

MacのFinderで「情報を見る」

タグはコマンドで確認できる:

$ mdls -name kMDItemUserTags nature14539.pdf 
kMDItemUserTags = (
    "Deep Learning",
    LeCun,
    Bengio,
    Hinton,
    2015
)

このタグはSpotlightで検索できるし,次のようにコマンドでも検索できる:

$ mdfind "kMDItemUserTags == Hinton && kMDItemUserTags == Bengio"
$ mdfind "kMDItemUserTags == 'Deep Learn*'"

検索にはファイル名も含ませることができる:

$ mdfind "kMDItemUserTags == Hinton && kMDItemFSName == *.pdf"

kMDItemFSName 以外に kMDItemDisplayName もあるが,こちらは表示名である(例えば「Documents」と「書類」)。

これ以外にどんなメタデータがあるかは,mdls ファイル名 として調べられる。

これらのメタ情報はMac固有のものである。別のMacに送る場合は rsync -auvzE .... のように -E オプションを付ける必要がある。

タグを操作するもっと便利なコマンドとして tag がある。Homebrew でもインストールできる。

$ tag --help
tag - A tool for manipulating and querying file tags.
  usage:
    tag -a | --add <tags> <path>...     Add tags to file
    tag -r | --remove <tags> <path>...  Remove tags from file
    tag -s | --set <tags> <path>...     Set tags on file
    tag -m | --match <tags> <path>...   Display files with matching tags
    tag -f | --find <tags> <path>...    Find all files with tags (-A, -e, -R ignored)
    tag -u | --usage <tags> <path>...   Display tags used, with usage counts
    tag -l | --list <path>...           List the tags on file
  <tags> is a comma-separated list of tag names; use * to match/find any tag.
  additional options:
        -v | --version      Display version
        -h | --help         Display this help
        -A | --all          Display invisible files while enumerating
        -e | --enter        Enter and enumerate directories provided
        -R | --recursive    Recursively process directories
        -n | --name         Turn on filename display in output (default)
        -N | --no-name      Turn off filename display in output (list, find, match)
        -t | --tags         Turn on tags display in output (find, match)
        -T | --no-tags      Turn off tags display in output (list)
        -g | --garrulous    Display tags each on own line (list, find, match)
        -G | --no-garrulous Display tags comma-separated after filename (default)
        -c | --color        Display tags in color
        -p | --slash        Terminate each directory name with a slash
        -0 | --nul          Terminate lines with NUL (\0) for use with xargs -0
             --home         Find tagged files in user home directory
             --local        Find tagged files in home + local filesystems
             --network      Find tagged files in home + local + network filesystems

使い方の例:

$ tag --add LeCun,Bengio,Hinton,2015,'Deep Learning' nature14539.pdf
$ tag nature*.pdf   # タグをリスト
$ tag --find Hinton,2015  # AND検索
$ tag --find 'deep learning' /Volumes/opt  # /Volumes/opt 以下に限定

残念なことに mdfind と違ってワイルドカード * が使えない。

$ mdfind "kMDItemUserTags == 'Deep Learn*'" # 見つかる
$ tag --find 'Deep Learn*'                  # 見つからない

追加リンク:Macで検索結果を絞り込む

追加リンク:Terminal command to get all of a file's metadata?mdls 以外に xattr -lls -l@ がある。画像についてはmacOSの sips、ImageMagickの identify、写真については exiftool がメタ情報を拾ってくれる)

追記:ファイルの種類を調べるには file コマンドがあるが、これをAIで賢くしたGoogleの Magika ができた。