Apply Finder Tags to Files from the Terminal

Write the `com.apple.metadata:_kMDItemUserTags` extended attribute used by Finder labels.

macOSmetadataFinderxattrtagsMedium risk
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" 报告.pdf

02What 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

RiskMedium risk
!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 报告.pdf

05Sources

Last verified: 2026-09-18 · Category: Files & Folders

06Related solutions