Apply Finder Tags to Files from the Terminal
Write the `com.apple.metadata:_kMDItemUserTags` extended attribute used by Finder labels.
Run `xattr -w com.apple.metadata:_kMDItemUserTags "TagName\nColorNumber" file` to tag it; delete the attribute to remove all tags.
01Command
Command
$ xattr -w com.apple.metadata:_kMDItemUserTags "项目A
1" 报告.pdf02What it does
Finder tags are stored as a plist-formatted extended attribute where each line is `name\ncolor-number` (0 = none, 1 = gray … 7 = blue). Writing it with xattr makes tagging scriptable — for example marking every file in a backup set before archiving. Spotlight picks up the change and the tag becomes searchable in Finder immediately.
03Risk
!Warning: The value must be newline-separated name/color pairs; a malformed value shows a blank tag. Removing the attribute strips every tag on the file at once, and color numbers 1–7 map to the fixed Finder palette.
04Undo
Undo
$ xattr -d com.apple.metadata:_kMDItemUserTags 报告.pdf05Sources
- xattr – Display and edit extended attributesEvidence: Documents reading/writing the metadata xattr that stores Finder tags
Last verified: 2026-09-18 · Category: Files & Folders
06Related solutions
Show file extensions for all files on Mac
Make Finder display .pdf, .docx, .png and every other extension at all times.
Remove the Quarantine Attribute with xattr
List and clear extended attributes — the quarantine flag, Finder tags, and download provenance — on any file.
Show the hidden Library folder on Mac
Permanently reveal ~/Library — where app settings, caches and support files live.