For example, suppose I have a directory structure such as

            algebraic_varieties.tex [tagged: Math, Documentation]
            projective_geometry.pdf [tagged: Math]
            visualize_surfaces.py   [tagged: Math, Programming]
            solve_polynomials.scm   [tagged: Math]
            welcome.rtf             [tagged: Documentation]
        assignments_for_may.txt     [tagged: Math]
        using_LaTeX.pdf             [tagged: Documentation]

From the command line, is there a way I can use ls (or something like it) to display all the files tagged as "Math"?

EDIT: I made a tool called tfind that does this: https://github.com/tlehman/bin/blob/master/tfind

  • 2
    See github.com/jdberry/tag – mankoff Nov 5 '14 at 1:51
  • 1
    I second the recommendation for tag by James Berry at https://github.com/jdberry. You can install it via brew install tag, add a tag via tag -a <tag> <file>, find tags via tag -f <tag1, tag2>. Very, very handy, I use it all the time. – Chris Conover Dec 17 '14 at 17:27
  • Yes, definitely get the tag command. Installs via brew very easily. – Jay Jun 28 '16 at 0:27

I'm assuming they will be an xattr like they currently are on 10.8.3 (and older)

Currently, you can view which extended attributes a file has with ls -l@.

But to see the contents of the attributes, you have to use xattr.

| improve this answer | |
  • You were right, the tags are stored as an xattr – tlehman Jun 14 '13 at 18:59
  • FYI: With OS X El Capitan, I used the command, xattr -pl com.apple.metadata:_kMDItemUserTags filename. However, it was not very friendly output. – Mr. Lance E Sloan Jul 31 '16 at 18:01

Yes, you can find files that have a given user tag using mdfind.

Create a file and assign it a a custom tag in Finder.

Then go in a terminal; you will find it with:

mdfind "kMDItemUserTags == Math || kMDItemUserTags == Programming"


mdfind "kMDItemUserTags == Math && kMDItemUserTags == Programming"

See also -onlyin aFolder to restrict the search.

I didn't check how complex these boolean expressions can be, but these two examples work.

You can pipe the output to ls like this:

mdfind "kMDItemUserTags == mathTag || kMDItemUserTags == anotherTag" \
| while read f; do ls "$f"; md5 "$f"; done

Also, mdfind has an option "-0"

    -0                Use NUL (``\0'') as a path separator, for use with xargs -0.

which can be practical with nasty filenames.

| improve this answer | |
  • The problem with this method is that it doesn't find all the files, returning only a (possibly empty) sub-set of files. – markvgti Nov 22 '13 at 11:26
  • 3
    It appears they've introduced a simplified mdfind syntax for tags, e.g.: mdfind "tag:Math", mdfind "tag:Math OR tag:Programming" – wjv Nov 22 '13 at 13:02
  • 1
    @AntoineLecaille I have 295 files in a directory with the "Orange" tag (the Orange Label before upgrading to Mavericks). That command lists only 11 of them. Also have many files tagged "Green" --- that command finds none of them. – markvgti Nov 24 '13 at 14:27
  • 1
    @wjv I used the syntax suggested by you and got exactly the same results as I listed above. – markvgti Nov 24 '13 at 14:31
  • 1
    @markvgti Then take one Orange file that is found with this command and one that is not, and find the differences with xattr or mdls ? – alecail Nov 30 '13 at 7:59

You can get all tags of a file with:

mdls -name kMDItemUserTags filename

| improve this answer | |
  • This seems to be the inverse of what was asked. Your command dumps all tags for one file. I think the OP wants to know all files with one tag. – bmike Oct 23 '13 at 0:57
  • 2
    Still, useful. It answers the question I had when I Googled and ended up here. If there isn't a different Q/A that covers this it might be worth the effort to post and answer one yourself. – Slipp D. Thompson Nov 15 '15 at 0:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .