BASH Script to Delete Mac Extended Attributes

Sometimes when you download lots of junk from the internet, MacOSX likes to stomp all over your files leaving pesky little sprinkles of annoyance called “extended attributes”

They show up when ever you ls -l as little @ symbols at the end of the file mode section.

Terminal
1
2
3
4
[~/tmp]
[17:06:17 mburke]$ ls -l
total 40
-rw-r--r--@ 1 mburke  staff  18946 Sep 24 17:05 image-from-internet.png

You can view the attributes with the xattr command:

Terminal
1
2
3
4
[~/tmp]
[17:06:18 mburke]$ xattr image-from-internet.png
com.apple.metadata:kMDItemWhereFroms
com.apple.quarantine

There is also a handy switch to ls that can show them inline:

Terminal
1
2
3
4
5
6
[~/tmp]
[17:09:37 mburke]$ ls -l@
total 40
-rw-r--r--@ 1 mburke  staff  18946 Sep 24 17:05 image-from-internet.png
  com.apple.metadata:kMDItemWhereFroms      120
  com.apple.quarantine       68

You can remove them one at a time with xattr -d $NAME_OF_ATTRIBUTE, but this is really obnoxious. You first would have to look up which attributes are applied, and then type in the name of each one.

So I’ve got this script in my $PATH that can do it for me.

~/bin/xattr-rm
1
2
3
4
5
6
7
8
9
#!/bin/bash

while (( "$#" )); do
  xattr $1 | while read attr; do
      echo "removing $attr from $1"
      xattr -d $attr $1
  done
  shift
done

Now when I want to kill them, it’s an easy one-liner:

Terminal
1
2
3
4
5
6
7
8
9
[~/tmp]
[17:11:53 mburke]$ xattr-rm image-from-internet.png
removing com.apple.metadata:kMDItemWhereFroms from image-from-internet.png
removing com.apple.quarantine from image-from-internet.png

[~/tmp]
[17:12:36 mburke]$ ls -l
total 40
-rw-r--r--  1 mburke  staff  18946 Sep 24 17:05 image-from-internet.png

You could even do a whole folder at once with xattr-rm * or use find . to -exec the command over every file in a directory.

Yay scripting!

Comments