OS X Extended Attributes and xattr

Perm url with updates: http://xahlee.org/comp/OS_X_extended_attributes_xattr.html

OS X Extended Attributes and xattr

Xah Lee, 2010-07-02

This article explains Mac OS X's Extended Attributes and the command line utility xattr.

So, i was working on OS X again. ( OS X 10.5) When i do “ls -l”, i noticed the “@” sign, e.g.:

-rw-rw-r--@  1 _www    admin  1046505 Nov 10  2007 collection.jpg

What the fuck is that? 10 years working in unixes, i haven't seen that before. So, some web search found me: Extended file attributes, and the new command line utility “xattr”.

What's Extended Attribute?

Here's a quote from Wikipedia:

Extended file attributes is a file system feature that enables users to associate computer files with metadata not interpreted by the filesystem, whereas regular attributes have a purpose strictly defined by the filesystem (such as permissions or records of creation and modification times). Unlike forks, which can usually be as large as the maximum file size, extended attributes are usually limited in size to a value significantly smaller than the maximum file size. Typical uses can be storing the author of a document, the character encoding of a plain-text document, or a checksum.

Mac OS X 10.4 and later support extended attributes by making use of the HFS+ filesystem Attributes file B*-tree feature which allows for named forks. Although the named forks in HFS+ support arbitrarily large amounts of data through extents, the OS support for extended attributes only supports inline attributes, limiting their size to that which can fit within a single B*-tree node. Any regular file may have a list of extended attributes. HFS+ supports an arbitrary number of named forks, and it is unknown if the Mac OS imposes any limit on the number of extended attributes. Each attribute is denoted by a name and the associated data. The name is a null-terminated Unicode string. The Mac OS X APIs support listing[2], getting[3], setting[4], and removing[5] extended attributes from files or directories. The xattr utility may be used from the Terminal as well.[6]

Extended Attributes are used to store author name, character encoding used, short comments, security/quarantine status, ... and basically any small amount of info about the file.

Note that standard data such as creation date, modification date, permission, are in contrast the normal attributes supported by the whole system. While, HFS/HFS+ file system's resource fork, widely used before Mac OS X , can be considered as a more elaborate extended attribute system, that it also hold main data of the file. (See: Mac OS X Resource Fork and Command Line Tips)

How to use Command Line for extended Attribute

Use “ls -l@ filename”

Apple also made some changes to “ls”. When you do a “ls -l” on a file, if the file has extended attributes, it'll display a “@” sign. You can also use “ls -l -@ filename” to show extended attributes. Example:

-rw-rw-r--@  1 _www    admin  1046505 Nov 10  2007 collection.jpg

Here's a quote from “man ls”:

If the file or directory has extended attributes, the permissions field printed by the -l option is followed by a '@' character. Otherwise, if the file or directory has extended security information (such as an access control list), the permissions field printed by the -l option is followed by a '+' character.

Use xattr

There's no man page for xattr, but “xattr -h” gives a usage summary:

usage: xattr [-l] [-r] [-v] [-x] file [file ...]
       xattr -p [-l] [-r] [-v] [-x] attr_name file [file ...]
       xattr -w [-r] [-v] [-x] attr_name attr_value file [file ...]
       xattr -d [-r] [-v] attr_name file [file ...]

The first form lists the names of all xattrs on the given file(s).
The second form (-p) prints the value of the xattr attr_name.
The third form (-w) sets the value of the xattr attr_name to the string attr_value.
The fourth form (-d) deletes the xattr attr_name.

  -h: print this help
  -r: act recursively
  -l: print long format (attr_name: attr_value and hex output has offsets and
      ascii representation)
  -v: also print filename (automatic with -r and with multiple files)
  -x: attr_value is represented as a hex string for input and output
vmm:vmm xahlee$

What to Put in Extended Attribute?

Extended Attributes are name/value pairs. The names are Unicode characters of less than 128 bytes. (they are probably encoded in utf-8 or utf-16) The values can be text or binary, recommended to be less than 4kb.

There's not standardize structure for names. Anyone can add a attribute named for example “xyz” to a particular file. Apple recommends the use of reverse DNS naming scheme to prevent attribute name conflicts. This is the same scheme adopted by Java. Example:


If you have a domain name, such as “example.com” then your attribute name for file of author can be “com.example.author”. If you don't have a domain name, just make sure the name is not likely used by others.

Popular posts from this blog

11 Years of Writing About Emacs

does md5 creates more randomness?

Google Code shutting down, future of ErgoEmacs