xattr - macOS


Overview

xattr manages extended attributes associated with files and directories on macOS. Extended attributes provide a way to store arbitrary metadata with files beyond the traditional attributes like file size, owner, and permissions. They are commonly used by macOS itself and by third-party applications to store additional information about files.

Syntax

xattr [-rvf] [-p] [-z] [-w attr[:value]] file...

Options/Flags

  • -r (Recursive): Apply the operation to all files and directories under the specified paths.
  • -v (Verbose): Log detailed information about the operation.
  • -f (Force): Ignore permission errors and continue processing.
  • -p (Print): Print the value of the specified attribute.
  • -z (Zero): Force the value of the specified attribute to the null string.
  • -w (Write): Set the specified attribute to the given value.

Examples

Get the value of an extended attribute:

xattr -p com.apple.metadata:_kMDItemFinderComment /path/to/file

Set an extended attribute:

xattr -w com.example.app:my_attribute "my_value" /path/to/file

Remove an extended attribute:

xattr -z com.example.app:my_attribute /path/to/file

Common Issues

  • Error: “xattr: path not provided”

  • Solution: Specify the path to the file or directory you want to operate on.

  • Error: “xattr: No such attribute”

  • Solution: Check that the specified attribute exists and is valid.

Integration

Combine with find:

find /path/to/dir -type f -exec xattr -rv com.example.app:my_attribute {} \;

Use with scripts:

#!/bin/bash
file="/path/to/file"
xattr_name="com.example.app:my_attribute"

if xattr -p "$xattr_name" "$file" &> /dev/null; then
  echo "Attribute value: $(xattr -p "$xattr_name" "$file")"
else
  echo "Attribute '$xattr_name' not found"
fi
  • ls: List files and directories, including extended attributes.
  • getfattr: Get the value of an extended attribute.
  • setfattr: Set the value of an extended attribute.
  • chflags: Change file flags, including extended attribute flags.