Possible to tag a folder via terminal?
You can use xattr. This copies the tags from file1 to file2:

xattr -wx com.apple.metadata:_kMDItemUserTags "$(xattr -px com.apple.metadata:_kMDItemUserTags file1)" file2;xattr -wx com.apple.FinderInfo "$(xattr -px com.apple.FinderInfo file1)" file2

The tags are stored in a property list as a single array of strings:

$ xattr -p com.apple.metadata:_kMDItemUserTags file3|xxd -r -p|plutil -convert xml1 - -o -
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"&gt;
&lt;plist version="1.0"&gt;
&lt;array&gt;
    &lt;string&gt;Red
6&lt;/string&gt;
    &lt;string&gt;new tag&lt;/string&gt;
    &lt;string&gt;Orange
7&lt;/string&gt;
    &lt;string&gt;Yellow
5&lt;/string&gt;
    &lt;string&gt;Green
2&lt;/string&gt;
    &lt;string&gt;Blue
4&lt;/string&gt;
    &lt;string&gt;Purple
3&lt;/string&gt;
    &lt;string&gt;Gray
1&lt;/string&gt;
&lt;/array&gt;
&lt;/plist&gt;


If the kColor flag in com.apple.FinderInfo is unset, Finder doesn't show the circles for colors. If the kColor flag is set to orange and the file has the red tag, Finder shows both red and orange circles. You can set the kColor flag with AppleScript:

xattr -w com.apple.metadata:_kMDItemUserTags '("Red\n6","new tag")' ~/desktop/file4;osascript -e 'on run {a}' -e 'tell app "Finder" to set label index of (POSIX file a as alias) to item 1 of {2, 1, 3, 6, 4, 5, 7}' -e end ~/desktop/file4

xattr -p com.apple.FinderInfo file|head -n1|cut -c28-29 prints the value of the bits used for the kColor flag. Red is C, orange is E, yellow is A, green is 4, blue is 8, magenta is 6, and gray is 2. The flag that would add 1 to the values is not used in OS X.

Edit: you can also use tag:

tag -l file # list
tag -a tag1 file # add
tag -s red,blue file # set
tag -r \* file # remove all tags
tag -f green # find all files with the green tag
tag -f \* # find all files with tags
tag -m red * # match (print files in * that have the red tag)


tag can be installed with brew install tag or sudo port install tag.

$ tag -h
tag - A tool for manipulating and querying file tags.
  usage:
    tag -a | --add &lt;tags&gt; &lt;file&gt;...     Add tags to file
    tag -r | --remove &lt;tags&gt; &lt;file&gt;...  Remove tags from file
    tag -s | --set &lt;tags&gt; &lt;file&gt;...     Set tags on file
    tag -m | --match &lt;tags&gt; &lt;file&gt;...   Display files with matching tags
    tag -l | --list &lt;file&gt;...           List the tags on file
    tag -f | --find &lt;tags&gt;              Find all files with tags
  &lt;tags&gt; 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
        -n | --name         Turn on filename display in output (default)
        -N | --no-name      Turn off filename display in output (list)
        -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)
        -H | --home         Find tagged files only in user home directory
        -L | --local        Find tagged files only in home + local filesystems (default)
        -R | --network      Find tagged files in home + local + network filesystems
        -0 | --nul          Terminate lines with NUL (\0) for use with xargs -0