Working with Tags in OS X

Working with Tags in OS X

Yesterday I wrote about how to use tags to group applications and how to use Alfred to search for them. This post will be how to work with Tags in OS X and ways to work with them from both the GUI and the Command Line.

How to add tags to files

The easiest way to add tags to your files is by selecting the file, pressing ⌘ and i to open the Get Info window for that file. From there you can add your tag by typing it in the text box on the top.

Sublime_Text_2_Info

You can add multiple tags by pressing enter after each one.

View all of the files you tagged

After tagging a few of your files you can find them by that tag a few ways. One way to do this is to click on View and then Show Sidebar or press ⌥ ⌘ P  (Option, Command and P) to open the sidebar (if it wasn’t open already) then click on Show next to Tags and then select the tag. On the main pane to the right you’ll see the result of the tag regardless where it’s at in the filesystem.

listtags

Working with Tags in the command line

Working with tags in the command line isn’t easy but there are some tools to do this. The one I found to be the most helpful is called “tag” by jdberry on GitHub. Tag can be installed using Brew or MacPorts.

Homebrew
brew install tag

MacPorts
sudo port install tag

If you don’t have either installed you can install my favorite, Brew using this command:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

And read more about it here: http://brew.sh/

Installing tag using Brew

brew install tag

Listing all files with tags

tag -tgf \*
Outputs the following:

/Applications/Caffeine.app
presentation
/Applications/Coda 2.app
dev
web
/Applications/CodeKit.app
dev
web
/Applications/Dash.app
docs
documentation
man
web

This output shows the file and then the various tags associated with it below. There are a few switches you can do to change this output, removing the -g has it display on one line per file:

tag -tf \*
/Applications/4K Video Downloader.app movie,movies,video,videos
/Applications/ADPassMon.app ldap,network
/Applications/Angry IP Scanner.app admin,net,network
/Applications/Apple Configurator.app admin,apple,net,network
/Applications/Caffeine.app presentation
/Applications/CleanMyDrive.app admin
/Applications/Coda 2.app dev,web
/Applications/CodeKit.app dev,web
/Applications/Colloquy.app chat,irc
/Applications/Dash.app dev,docs,documentation,man,web

Adding tags to a file

Adding a tag to a file is easy with this tool.

tag -a dev /Applications/CodeKit.app

or if I had more than one tag you could do

tag -a dev,web /Applications/CodeKit.app

and more than one file and more than one tag

tag -a dev,web /Applications/CodeKit.app /Applications/Dash.app

Tags has a few other options

    tag -a | --add  ...     Add tags to file
    tag -r | --remove  ...  Remove tags from file
    tag -s | --set  ...     Set tags on file
    tag -m | --match  ...   Display files with matching tags
    tag -l | --list ...           List the tags on file
    tag -f | --find               Find all files with tags

Using tags

listtags

With the left sidebar on you can select the Tags below the heading and then on the right it will display the filtered list of the items.

Other CLI Solutions

Brett Terpstra has a great CLI tool that does Tagging and makes batch tagging super simple
http://brettterpstra.com/projects/vitag/