Tagging files in Terminal
How can I add tags to files by using Terminal commands?
Access to tags in Terminal can be difficult because they’re actually stored in files’ extended attributes. You can set one standard colored tag in the com.apple. FinderInfo attribute.
In order to set multiple or custom tags on a file, you need to access com.apple.metadata :_kMDItemUserTags instead. This is the extended attribute that Apple exposes most to the user (in Finder), but there is no convenient access to it in Terminal. The built-in command xattr, for displaying and manipulating extended attributes, is made more difficult to use because tags are stored in a binary format, which is hard to handle outside a programming language like Swift or Python.
A third-party command named tag is available via Homebrew, MacPorts, or from
github.com/jdberry/tag. This should make your task a lot simpler and more flexible. There’s more info at bit.ly/ecxattr via The Eclectic Light Company website, but bear in mind that it gets quite technical.