↑ arvau

ArchWiki / Extended attributes

ja:拡張属性 ru:Extended attributes

From xattr(7): "Extended attributes are name:value pairs associated permanently with files and directories". There are four extended attribute classes: security, system, trusted and user.

By default, extended attributes are not preserved by cp, rsync, and other similar programs, see #Preserving extended attributes.

Extended attributes are also used to set Capabilities.

User extended attributes

User extended attributes can be used to store arbitrary information about a file. To create one:

$ setfattr --name=user.checksum --value="3baf9ebce4c664ca8d9e5f6314fb47fb" file.txt

Use getfattr to display extended attributes:

# file: file.txt
user.checksum="3baf9ebce4c664ca8d9e5f6314fb47fb"
$ getfattr --encoding=text --dump file.txt

To remove an extended attribute:

$ setfattr --remove=user.checksum file.txt

To find files with certain extended attributes use rawhide:

$ rh /path/to/dir '"XATTR_REGEX".reea'

Some other user extended attributes include:

  • user.mime_type: helps programs know mimetype and set it with less guesswork.

  • user.charset: used by the Apache httpd module mod_mime_xattr.

  • user.creator: The name of the application that created the file.

XDG also proposes a set of standardized extended attributes to be used by programs:

  • user.xdg.comment: supported by Dolphin and other file managers.

  • user.xdg.origin.url: for files downloaded from a url.

  • user.xdg.robots.index: "true" if a file is included in indexing, "false" otherwise

  • user.xdg.robots.backup: "true" if a file is included in backup, "false" otherwise

  • user.xdg.language

  • user.xdg.creator

  • user.xdg.publisher

  • user.xdg.origin.email.subject

  • user.xdg.origin.email.from

  • user.xdg.origin.email.message-id

user.xdg.tags is not part of the official standard, but it has become a "de facto" standard as several popular programs have implemented support for it (see #Software). It is implemented as a CSV list of user-specified tags for each file.

Preserving extended attributes

  1. mv silently discards extended attributes when the target file system does not support them.

To preserve extended attributes with text editors you need to configure them to truncate files on saving instead of using rename(2). https://unix.stackexchange.com/questions/45407

Just like you should do for any other data you do not want to lose, you should make regular backups of your extended attributes. To make a full backup of the extended attributes of all files in the current directory (recursively):

$ getfattr --dump --recursive . > backup.txt

To restore a backup:

$ setfattr --restore=backup.txt

Support

File systems

All major Linux file systems including Ext4, Btrfs, ZFS, and XFS support extended attributes. The kernel allows to have extended attribute names of up to 255 bytes and values of up to 64 KiB, but Ext4 and Btrfs might impose smaller limits, requiring extended attributes to be within a "filesystem block".

NTFS uses Alternative Data Streams to store user. The mount option user_xattr or streams_interfacexattr should be used by default. However, it might not be supported if mount option streams_interfacewindows is used. ntfs-3g supports mapping Alternative Data Streams to extended attributes in FUSE.

NFS supports extended attributes since Linux 5.9.

Software

Other tagging systems

It might not be possible to use extended attributes due to lack of support of either the file system or software. For this reason, many media formats store metadata included in the file format that can be viewed using programs like Exiftool or more specified ones like id3 for audio.

gvfs

Another filesystem-independent workaround is Gnome virtual filesystem: gvfs which is used to store metadata (gvfsd-metadata). For example, Firefox stores metadata this way and can be viewed with:

$ gio info --attributes=metadata:: downloaded.html

Other programs that use this approach include:

  • Thunar: to save file color highlights.

See also