dcsimg

Extended File Attributes and ZSH

Extended attributes are a fairly new addition to Linux. Learn how to manage files more effectively with extended attributes and the Z Shell.

A filesystem doesn’t just store the contents of files. It also stores a great deal of file metadata, or data about the files. In addition to the file’s name, file metadata includes attributes such as access rights (permissions) and the type of the file (whether it’s a directory, a symbolic link, a device or something else).

Extended filesystem attributes are a fairly new addition to the Linux kernel that allow you to create your own per-file attributes.[ Extended filesystem attributes were first mentioned in the September 2003 issue of Linux Magazine, available online at http://www.linux-mag.com/2003-09/acls_01.html, and were described in more detail in the June 2004 “Compile Time” column, available online at http://www.linux-mag.com/2004-06/compile_01.html.] Because extended attributes are user-defined (name, value) pairs, there’s vitually no limit to the kinds of applications you can create.

Setting and Retrieving Extended Attributes

For example, if you have a collection of photographs from a digital camera, you can use extended attributes to record when the photo was taken, the contents of the picture (people, sights, and so on), and your camera settings. With extended attributes, keeping such metadata in a separate database isn’t necessary; each set of attributes is “attached” to the file.

On Linux, extended attributes are manipulated using the setfattr and getfattr commands to set and retrieve extended attributes, respectively. (See the sidebar “Enabling Extended Attributes” if your system or filesystem does not yet support extended attributes.)

Continuing with the photographs example, to store the year a picture was taken, use setfattr:

$ setfattr –n user.year –v 1990 oldpicture.jpg

The user prefix is a name space, a mechanism that provides for different classes of extended attributes. As shown above, attribute names are always specified in the full namespace.attribute form. The user name space is intended for any user-defined attributes. (Other name spaces, such as trusted may exist, but special permissions may be needed to access them, so it’s best to use user.)

To retrieve the information stored in a file’s extended attribute, use getfattr:

$ getfattr oldpicture.jpg
# file: oldpicture.jpg
user.year=1990

A word of caution before you store a lot of valuable information in extended attributes: many backup programs such as tar are not yet aware of extended file attributes. If you archive your files with such a” na