How to verify if an extended attribute exist?

When running

 xattr -d com.apple.quarantine /Applications/XXX.app

sometimes I get

 xattr: /Applications/XXX.app: No such xattr: com.apple.quarantine

How does one verify if any named attribute exist for a given XXX.app?


Solution 1:

The following xattr command causes both the attribute names and corresponding values to be displayed, if any exist.

xattr -l file

You can also use the ls command:

ls -@ file

That said, the ... No such xattr: com.apple.quarantine message is a non-fatal message, meaning there's no real need to make it conditional, and if your goal is to remove the com.apple.quarantine extended attribute from a lot of files at once in a given directory, then cd to the target directory and use the following command:

for f in *; do xattr -dr com.apple.quarantine "$f" 2>/dev/null; done

The above one-liner will delete the com.apple.quarantine extended attribute off every file that has this and act as if the entire contents of the directory recursively were also specified (so that every file in the directory tree is acted upon), thats the -r option, while not showing errors for files that don't have the target extended attribute. (That's what the 2>/dev/null does.)

Solution 2:

There is no direct way to test the existance of a specific extended attribute. In your case you could, with

file=/path/to/file
attr=attribute

do something like

if [[ $(xattr "$file") = *$attr* ]]; then
    xattr -d $attr "$file"
fi

or, as a one-liner

[[ $(xattr "$file") = *$attr* ]] && xattr -d $attr "$file"