BrT

Musings about technology and other neat stuff

BrT random header image

Removing all extended attributes from a directory tree

May 7th, 2008 · 12 Comments

Here’s a simple one-liner I came up with to remove all HFS+ extended attributes on all files and directories below the current one in Mac OS X. Useful before creating a tar file that will be shared with non-Mac users (tar in OSX will create additional “resource” files to store extended attributes, which when untared on a Mac will be correctly reinstated, but in other systems will leave spurious hidden files all over the place).


for i in $(ls -Rl@ | grep '^      ' | awk '{print $1}' | sort -u); \
   do echo echo Removing $i ... >&2;  \
   find . | xargs xattr -d $i 2>/dev/null ; done

The space after the caret in the grep command is produced by typing Ctrl-V and then Tab, to insert a Tab character.

Tags: · ·

12 responses so far ↓

  • 1  Dominik Hoffmann // Jun 6, 2008 at 8:42 pm

    This only puts out the name of the attribute it’s supposed to remove, but doesn’t actually. I am running this in sudo mode.

  • 2  Hitchhiker // Jun 7, 2008 at 8:52 am

    Dominik: if you are typing “sudo” followed by the command shown, then only the first command (the “for” is executed as root, so the attributes will not be removed (the xargs command is the one that needs to be run as root). You need to enter a root shell (you could run “sudo bash”) and then type the command in that shell so that all the commands execute as root.

  • 3  Dominik Hoffmann // Jun 7, 2008 at 1:11 pm

    Hi Hitchhiker: I had executed the command inside a root shell, via sudo -s. — Dominik

  • 4  Hitchhiker // Jun 7, 2008 at 4:06 pm

    Do you get any errors when executing the command? I see nothing wrong in the command as shown, it should work properly.

  • 5  Hitchhiker // Jun 7, 2008 at 4:07 pm

    Dominik: you could try removing the “2> /dev/null” after the xattr command, to have errors shown on the terminal.

  • 6  Dominik Hoffmann // Jun 9, 2008 at 12:25 am

    This is what I see:

    echo Removing com.apple.AddressBook.ImageTransform.ABClipRect_1 …
    xargs: unterminated quote

    Dominik Hoffmann

  • 7  Hitchhiker // Jun 9, 2008 at 6:37 am

    The “unterminated quote” message makes me think there is a filename in the tree you are processing that has a quote character (single or double quote) in it. You could try adding the -t option to xargs (xargs -t xattr .., still leaving out the “2> /dev/null”), which will print the commands it executes, and see which command is printed before the error.

  • 8  Dominik Hoffmann // Jun 9, 2008 at 1:51 pm

    xargs -t doesn’t output anything.

  • 9  Dominik Hoffmann // Jun 9, 2008 at 3:32 pm

    Okay, I made the working directory ~/.ssh/.

    This time it worked:

    echo Removing com.apple.metadata:_kTimeMachineNewestSnapshot …
    xattr -d com.apple.metadata:_kTimeMachineNewestSnapshot . ./authorized_keys ./id_dsa ./id_dsa.pub ./known_hosts
    echo Removing com.apple.metadata:_kTimeMachineOldestSnapshot …
    xattr -d com.apple.metadata:_kTimeMachineOldestSnapshot . ./authorized_keys ./id_dsa ./id_dsa.pub ./known_hosts

    However, with the working directory being ~ and the -R option removed from the ls instruction I have

    xargs: unterminated quote
    echo Removing com.apple.metadata:_kTimeMachineNewestSnapshot …
    xargs: unterminated quote
    echo Removing com.apple.metadata:_kTimeMachineOldestSnapshot …
    xargs: unterminated quote

  • 10  Dominik Hoffmann // Jun 9, 2008 at 3:57 pm

    This entry in the macosx.com forums, http://macosx.com/forums/unix-x11/19015-xargs-grep.html, pointed me in the right direction. It suggests the use of the -0 option in xargs in conjunction with the -print0 primary in find. It therefore works, when I execute

    for i in $(ls -Rl@ | grep ‘^ ‘ | awk ‘{print $1}’ | sort -u); do echo echo Removing $i … >&2; find . -print0 | xargs -0t xattr -d $i 2>/dev/null ; done

    Now it would be interesting to know, why you guys don’t need those modifications and I do.

    Dominik

  • 11  Hitchhiker // Jun 9, 2008 at 11:43 pm

    Dominik: I figured that was the problem. You must have some files with quote characters in them (or some other punctuation), which is why it doesn’t work with the standard separators.

    Glad you got it to work!

  • 12 PantherModem » Blog Archive » GCC Says: error: expected unqualified-id before ‘OTHER’ token // Jul 4, 2008 at 4:42 pm

    [...] coming from files with extended attributes. I cleaned out all the extended attributes (thanks to Diego Zamboni’s script), but the error still remained. In the end the only thing that worked was to delete the erroneous [...]

Leave a Comment