Name

xattr — stdin  stdout  - file  -- opt  --help  --version

Synopsis

xattr [-[cdpw]] [options] attributes [files]

Files in OS X can have not only “normal” attributes, such as read, write, and execute permission, but also extended attributes, which can be any file metadata you dream up. Extended attributes are created and manipulated with the xattr command. For example, let’s define an attribute called com.example.color, assign it the value blue, and apply it to the file myfile:[13]

touch myfile                          Create an empty filexattr -w com.example.color blue myfile

Now list the file and look for the @ symbol in the output, indicating that extended attributes are present:

ls -l@ myfile
-rw-r--r--@ 1 smith  staff  0 Mar 26 22:19 myfile

and display its extended attribute values with xattr:

xattr -l myfile
com.example.color: blue

or just one attribute alone, by name:

xattr -p com.example.color myfile
blue

You can delete one attribute with -d:

xattr -d com.example.color myfile

or all of them with -c:

xattr -c myfile

While you can amuse yourself all day by creating and viewing attributes, their practical use is for Macintosh applications to store important data about files. For instance, the Finder maintains an extended attribute named com.apple.FinderInfo, displayed here in hexadecimal:

xattr -l letter.docx
00000000  57 58 42 4E 4D 53 57 44 00 ...

Useful options

-l

Display both the name and value of attributes, not just one or the other.

-r

Recursively operate on all files in a directory.

-s

Do not follow ...

Get Macintosh Terminal Pocket Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.