List all tags in Terminal in Mavericks?
-
29-09-2020 - |
Question
Is it feasible to list all tags already defined in OS X in Terminal?
I know there are some third-party applications that list all tags, but I just want to use Terminal to find all tags, since I use this tool to work with tags in Terminal (but unfortunately the tool doesn't provide to list tags for an entire system - it just lists for a single or multiple files selected arbitrarily).
Thanks.
Solution
The tags name are in ~/Library/Preferences/com.apple.finder.list
.
You can read it with
defaults read com.apple.finder.plist ViewSettingsDictionary
It will need some parsing to be a bit clearer :
defaults read com.apple.finder.plist ViewSettingsDictionary |
awk 'NR%12==2'| grep -o '".*"' | sed 's/^.\(.*\).\{18\}$/\1/'
Other possibility is parsing the plist as xml. To do that you'll need to first convert the plist from binary to xml using.
plutil -convert xml1 ~/Library/Preferences/com.apple.finder.plist
OTHER TIPS
You can also use tag:
tag -tgf \*|grep '^ '|cut -c5-|sort -u
tag -f \*
finds all files with tags, -t
includes tag names in the output, and -g
prints each tag on a separate line.
That list isn't comprehensive. It's only tags added through Finder. There's an additional file ~/Library/SyncedPreferences/com.apple.finder.plist
, that also lists the tags that have been manipulated in Finder.
If you tagged a file in Terminal or using another tool and never manipulated the tags in Finder, then the tags won't be in either list.
You can also use mdfind in combination with mdls to get the same result:
/usr/bin/mdfind -0 "(kMDItemUserTags == '*')" |xargs -0 mdls -name kMDItemUserTags |grep '^ ' |cut -c5- |cut -d , -f 1 |sort -u
Would be interesting to benchmark, which one is faster.
Colour followed by path:
/usr/bin/mdfind -0 "(kMDItemUserTags == '*')" | xargs -I % sh -c 'echo $(mdls -name kMDItemUserTags % | grep "^ " |cut -c5-) %'