xattr

Display and manipulate extended attributes of one or more files, including directories and symbolic links.

Syntax
   List attributes
      xattr [-lrsvx] file ...

   Print the value associated with the given attribute:
      xattr -p [-lrsvx] attr_name file ...

   Write a given attribute name with a value:
      xattr -w [-rsx] attr_name attr_value file ...

   Delete the given attribute from file:
      xattr -d [-rsv] attr_name file ...

   Clear all attributes including their associated values:
      xattr -c [-rsv] file ...

   Display help:
      xattr -h | --help

Key
   -c  CLear all Atrributes.

   -d  Delete the given attribute.

   -h  Help.

   -l  By default, the first two command forms either display just the attribute names or
       values, respectively. The -l option causes both the attribute names and corresponding
       values to be displayed. For hex display of values, the output is preceeded with the hex
       offset values and followed by ASCII display, enclosed by '|'.

   -p  Print the value associated with the given attribute.

   -r  If a file argument is a directory, act as if the entire contents of the directory
       recursively were also specified (so that every file in the directory tree is acted upon).

   -s  If a file argument is a symbolic link, act on the symbolic link itself, rather than
       the file that the symbolic link points at.

   -v  Force the the file name to be displayed, even for a single file.

   -w  Write a given attribute name with a value.

   -x  Force the attribute value to be displayed in the hexadecimal representation.       

Extended attributes are arbitrary metadata stored with a file, but separate from the filesystem attributes (such as modification time or file size). The metadata is often a null-terminated UTF-8 string, but can also be arbitrary binary data.
One or more files may be specified on the command line.

In macOS, extended attributes are most often seen in the 'iPhoto Library' and 'Photo Booth Library' - these are folders with a bundle bit applied so that they appear in the Apple Finder as a single file, this is done to hide the files so that they can be managed by a single application.

For the first two forms of the command, if there is more than one file, the file name is displayed along with the actual results. When only one file is specified, the display of the file name is suppressed unless the -v option, is also specified.

In the first form of the command ( -w with no other mode option specified), the names of all extended attributes are listed.
Attribute names can also be displayed using ls -l@

In the second form, the -p option (print), the value associated with the given attribute name is displayed.

Attribute values are usually displayed as strings. However, if nils are detected in the data, the value is displayed in a hexadecimal representation.

The -w option normally assumes the input attribute value is a string. Specifying the -x option causes xattr to expect the input in hexadecimal (whitespace is ignored). The xxd command can be used to create hexadecimal representations from exising binary data, to pass to xattr.

Preserving extended attributes

cp and mv preserve Extended Attributes by default. To copy a file without it's Extended Attributes, use cp -X

rsync -E will preserve attributes even when transferring between Linux, FreeBSD, and MacOS.

tar preserves Extended Attributes but the storage format is not compatible with GNU tar however both versions of tar are available for the other OS, so it is possible to transfer files between OS's and preserve the Extended Attributes.

xattr is an abbreviation for 'extended attributes'.

xattr exits with zero status on success. On error, non-zero is returned, and a message is printed to standard error.

Examples

Remove the bundle bit from the MyApp folder and convert it back to a normal folder.

$ xattr -d com.apple.FinderInfo ~/MyApp

Remove the 'Where From' data from a downloaded or emailed file, (typically Images or videos) this is visible via Finder > Get Info and will display the source URL or email address. Also see AppleScript for doing this.

$ xattr -d com.apple.metadata:kMDItemWhereFroms /Path/To/File

Remove all Extended Attributes from the 'example.txt' file:

$ xattr -c example.txt

Remove all Extended Attributes from all the files in a directory:

$ attr -rc /path/to/directory

Copy the com.apple.FinderInfo attribute from the /usr directory to the MyDir directory:

$ xattr -px com.apple.FinderInfo /usr
  00 00 00 00 00 00 00 00 40 00 00 00 00 00 00 00
  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

$ xattr -l MyDir

$ xattr -wx com.apple.FinderInfo \
    "`xattr -px com.apple.FinderInfo /usr`" MyDir

$ xattr -l MyDir
  com.apple.FinderInfo:
  00000000  00 00 00 00 00 00 00 00 40 00 00 00 00 00 00 00  |........@.......|
  00000010  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  |................|
  00000020

“I think too much. I think ahead. I think behind. I think sideways. I think it all” ~ Winona Ryder

Related macOS commands:

ls - List information about file(s)
apple.stackexchange.com - Editing iPhoto-specific metadata


 
Copyright © SS64.com 1999-2019
Some rights reserved