327

I have an AppleScript script that runs a stress test. Part of the test is to open, save, and close certain files. Somehow, the files have picked up some "extended attributes" that prohibit the files from being saved. That causes the stress test to fail.

How do I remove the extended attributes?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
tames
  • 3,279
  • 3
  • 17
  • 4
  • 1
    Extended attributes can't prevent a file from being saved. Are you sure it's not a permissions problem, or an ACL? – Lily Ballard Jan 28 '11 at 21:19
  • Did you just upgrade to 10.7.3? It seems there is a bug where many GUI apps (including TextEdit, but also other apps - including one I wrote myself which doesn't do anything strange) will now set the quarantine bit on files. As @Bavarious said, you can remove it using @xattr@. Look into TextWrangler, which is free, and I think has good AppleScript support. I'm told it doesn't set the quarantine bit. – Abhi Beckert Mar 03 '12 at 09:01
  • In short: you don't need to remove these attributes unless you encounter some serious problem(s). – NeoZoom.lua Jan 17 '23 at 21:04

5 Answers5

467

Use the xattr command. You can inspect the extended attributes:

$ xattr s.7z
com.apple.metadata:kMDItemWhereFroms
com.apple.quarantine

and use the -d option to delete one extended attribute:

$ xattr -d com.apple.quarantine s.7z
$ xattr s.7z
com.apple.metadata:kMDItemWhereFroms

you can also use the -c option to remove all extended attributes:

$ xattr -c s.7z
$ xattr s.7z

xattr -h will show you the command line options, and xattr has a man page.

Joël
  • 1,563
  • 1
  • 18
  • 31
  • 2
    Apple has a man page for it available online: http://developer.apple.com/documentation/Darwin/Reference/ManPages/man1/xattr.1.html – Quinn Taylor Oct 19 '11 at 03:45
  • It appears that the -c option was removed in Mountain Lion (10.8). xattr -h no longer lists -c and produces "-c not recognized" when used. Very painful now to remove extended attributes one by one. – helioz Jan 29 '13 at 02:39
  • 1
    `-c` is not working for me (OS X 10.6.8). `-d` also didn't have any effect though it didn't complain. I had to explicitly name each extended attribute like so: `xattr -rd ` (deletes recursively). To find out the attributes present (likely the same attributes in the entire directory tree) use `xattr ` as in the answer above. – mindthief Oct 02 '13 at 11:35
  • @mindtheif no one mentioned here that this requires the xCode command line tools. On 10.6.8 it's called something different. [Here's a link](https://github.com/kennethreitz/osx-gcc-installer/) to a pre-built installer on GitHub so you don't have to install all of xCode to see if this is your problem. –  Oct 23 '13 at 14:34
  • 6
    With Xcode installed on my 10.8.5 system, `xattr -c` works fine for me (and successfully stripped all metadata). – Doktor J Nov 20 '13 at 19:55
  • I found I had to escalate privileges (sudo) to remove the 'com.apple.quarantine' attribute. – Raymond Kroeker Mar 22 '14 at 17:50
  • ***`-c`*** fails on OS X 10.5: ***`option -c not recognized`***. We use one for testing on Apple's PowerPC. It looks like the downlevel version of OS X supports ***`-l`***, ***`-p`***, ***`-w`*** and ***`-d`***. – jww Mar 05 '16 at 18:31
  • Note that if `xattr -c` fails with `Operation not permitted`, you may need to unlock the file using `sudo chflags nouchg {file_to_modify}` – Benjamin R Nov 04 '17 at 02:43
  • @mindthief, I explain why you get the error in my answer below. https://stackoverflow.com/a/58616002/1896134 – JayRizzo Oct 29 '19 at 22:06
119

Removing a Single Attribute on a Single File

See Bavarious's answer.


To Remove All Extended Attributes On a Single File

Use xattr with the -c flag to "clear" the attributes:

xattr -c yourfile.txt



To Remove All Extended Attributes On Many Files

To recursively remove extended attributes on all files in a directory, combine the -c "clear" flag with the -r recursive flag:

xattr -rc /path/to/directory



A Tip for Mac OS X Users

Have a long path with spaces or special characters?

Open Terminal.app and start typing xattr -rc, include a trailing space, and then then drag the file or folder to the Terminal.app window and it will automatically add the full path with proper escaping.

cwd
  • 53,018
  • 53
  • 161
  • 198
  • 1
    ***`-c`*** fails on OS X 10.5: ***`option -c not recognized`***. We use one for testing on Apple's PowerPC. It looks like the downlevel version of OS X supports ***`-l`***, ***`-p`***, ***`-w`*** and ***`-d`***. – jww Mar 05 '16 at 18:28
  • @cwd: the Finder drag-n-drop into terminal does not work with some 'special' Finder files such as `. textClipping` as described in https://apple.stackexchange.com/questions/301871/how-to-convert-finders-textclipping-files-to-plain-text/301873 – ccpizza Oct 12 '17 at 11:35
  • @jww I explain why you get the error in my answer below. https://stackoverflow.com/a/58616002/1896134 – JayRizzo Oct 29 '19 at 22:04
28

Try using:

xattr -rd com.apple.quarantine directoryname

This takes care of recursively removing the pesky attribute everywhere.

Jesse
  • 8,605
  • 7
  • 47
  • 57
venkat
  • 281
  • 3
  • 2
22

Answer (Individual Files)


1. Showcase keys to use in selection.

xattr ~/Desktop/screenshot\ 2019-10-23\ at\ 010212.png
    # com.apple.FinderInfo
    # com.apple.lastuseddate#PS
    # com.apple.metadata:kMDItemIsScreenCapture
    # com.apple.metadata:kMDItemScreenCaptureGlobalRect
    # com.apple.metadata:kMDItemScreenCaptureType

2. Pick a Key to delete.

xattr -d com.apple.lastuseddate#PS ~/Desktop/screenshot\ 2019-10-23\ at\ 010212.png
xattr -d kMDItemIsScreenCapture ~/Desktop/screenshot\ 2019-10-23\ at\ 010212.png

3. Showcase keys again to see they have been removed.

xattr -l ~/Desktop/screenshot\ 2019-10-23\ at\ 010212.png
    # com.apple.FinderInfo
    # com.apple.metadata:kMDItemScreenCaptureGlobalRect
    # com.apple.metadata:kMDItemScreenCaptureType

4. Lastly, REMOVE ALL keys for a particular file

xattr -c ~/Desktop/screenshot\ 2019-10-23\ at\ 010212.png

Answer (All Files In A Directory)


1. Showcase keys to use in selection.

xattr -r ~/Desktop

2. Remove a Specific Key for EVERY FILE in a directory

xattr -rd com.apple.FinderInfo ~/Desktop

3. Remove ALL keys on EVERY FILE in a directory

xattr -rc ~/Desktop

WARN: Once you delete these you DON'T get them back!
FAULT ERROR: There is NO UNDO.


Errors


I wanted to address the error's people are getting. Because the errors drove me nuts too... On a mac if you install xattr in python, then your environment may have an issue.

There are two different paths on my mac for xattr

type -a xattr

    # xattr is /usr/local/bin/xattr    # PYTHON Installed Version
    # xattr is /usr/bin/xattr          # Mac OSX Installed Version

So in one of the example's where -c will not work in xargs is because in bash you default to the non-python version.

Works with -c

/usr/bin/xattr -c

Does NOT Work with -c

/usr/local/bin/xattr -c
    # option -c not recognized

My Shell/Terminal defaults to /usr/local/bin/xattr because my $PATH /usr/local/bin: is before /usr/bin: which I believe is the default.

I can prove this because, if you try to uninstall the python xattr you will see:

pip3 uninstall xattr
Uninstalling xattr-0.9.6:
  Would remove:
    /usr/local/bin/xattr
    /usr/local/lib/python3.7/site-packages/xattr-0.9.6.dist-info/*
    /usr/local/lib/python3.7/site-packages/xattr/*
Proceed (y/n)?

Workarounds


To Fix option -c not recognized Errors.

  1. Uninstall any Python xattr you may have: pip3 uninstall xattr
  2. Close all Terminal windows & quit Terminal
  3. Reopen a new Terminal window.
  4. ReRun xattr command and it should now work.

OR

If you want to keep the Python xattr then use

/usr/bin/xattr

for any Shell commands in Terminal


Example:


Python's version of xattr doesn't handle images at all:

Good-Mac:~ JayRizzo$ xattr ~/Desktop/screenshot\ 2019-10-23\ at\ 010212.png
    # com.apple.FinderInfo
    # Traceback (most recent call last):
    #   File "/usr/local/bin/xattr", line 8, in <module>
    #     sys.exit(main())
    #   File "/usr/local/lib/python3.7/site-packages/xattr/tool.py", line 196, in main
    #     attr_value = attr_value.decode('utf-8')
    # UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb0 in position 2: invalid start byte

Good-Mac:~ JayRizzo$ /usr/bin/xattr ~/Desktop/screenshot\ 2019-10-23\ at\ 010212.png
    # com.apple.FinderInfo
    # com.apple.lastuseddate#PS
    # com.apple.metadata:kMDItemIsScreenCapture
    # com.apple.metadata:kMDItemScreenCaptureGlobalRect
    # com.apple.metadata:kMDItemScreenCaptureType

Man Pages

MAN PAGE for OSX xattr

MAN PAGE for Python xattr VERSION 0.6.4

NOTE: I could not find the python help page for current VERSION 0.9.6

Thanks for Reading!

JayRizzo
  • 3,234
  • 3
  • 33
  • 49
8

Another recursive approach:

# change directory to target folder:
cd /Volumes/path/to/folder

# find all things of type "f" (file), 
# then pipe "|" each result as an argument (xargs -0) 
# to the "xattr -c" command:
find . -type f -print0 | xargs -0 xattr -c

# Sometimes you may have to use a star * instead of the dot.
# The dot just means "here" (whereever your cd'd to
find * -type f -print0 | xargs -0 xattr -c
bob
  • 7,539
  • 2
  • 46
  • 42
  • ***`-c`*** fails on OS X 10.5: ***`option -c not recognized`***. We use one for testing on Apple's PowerPC. It looks like the downlevel version of OS X supports ***`-l`***, ***`-p`***, ***`-w`*** and ***`-d`***. – jww Mar 05 '16 at 18:31
  • @jww I explain why you get the error in my answer below. https://stackoverflow.com/a/58616002/1896134 – JayRizzo Oct 29 '19 at 22:04