Delete multiple extended attributes (but not all of them) in one step

For example, I have a file with three extended attributes:

com.apple.FinderInfo
com.apple.metadata:_kMDItemUserTags
com.apple.metadata:kMDItemFinderComment

I can delete the first two using

xattr -d com.apple.FinderInfo file.txt
xattr -d com.apple.metadata:_kMDItemUserTags file.txt

But I would prefer to not invoke xattr multiple times, and to use something like this instead:

xattr -d ( com.apple.FinderInfo, com.apple.metadata:_kMDItemUserTags ) file.txt
xattr -d com.apple.FinderInfo -d com.apple.metadata:_kMDItemUserTags file.txt

Is it possible somehow?

Asked By: jsx97

||

Hm, maybe you could run xattr -d attribute1 -d attribute2 filename, if xattr supports that? That would mean the documentation is a bit wrong, but getopt-based programs often have that problem.

Let’s test that:

I don’t have MacOS to test, but I got the original source code of the xattr tool, and removed all functionality from it so that my version compiles on linux and instead prints what it deletes.

Sadly,

./xatrr -d foo -d bar foo                                                                                                                                                                               
xatrr: [Errno 2] No such file or directory: 'bar'

Thus, that’s not an option.

Well, then:

for attr in attribute1 attribute2 do; xattr -d ${attr} filename; done

is the best I could offer (without relying on more tools).

Answered By: Marcus Müller
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.