How to fix “ghost” files in the Finder

Sometimes the Mac Finder can get its knickers in a twist about files that you *ought* to be able to open just fine but Finder says no. You may see a message that says “Item XYZ is used by Mac OS X and can’t be opened.”

This can happen if the Finder is in the middle of a copy and the source disk is suddenly disconnected. During a copy the Finder sets a special type/creator code on files, and when the copy completes the proper type/creator code is restored. But if the copy is interrupted then sometimes instead of recovering gracefully the Finder leaves the files with their temporary attributes.

So if you do have these “ghost” files, here is a bit of command-line magic to remove the Finder’s temporary attributes (you would need to change the “`/path/to/files`” bit):

mdfind -onlyin /path/to/files -0 “kMDItemFSTypeCode==brok && kMDItemFSCreatorCode==MACS” | xargs -0 -n1 xattr -d com.apple.FinderInfo

(This is a single command, all on one line, which you type in Terminal.app.)

What this does is use [the Spotlight tool][mdfind] to find only files that have a Mac file type of “brok” and a creator code set to “MACS”. Then for each file we remove the Finder attributes (which means the Finder reverts to using the file-name’s extension when deciding how to open a file).

The `-onlyin` flag is used to restrict the search to a particular folder and its sub-folders, a belt and braces approach to making sure we only fix the files we are interested in.

This post was suggested by [@hobbsy][hobbsy], who had probs with RAW pictures he had copied across disks.

Although the way I showed him how to do this originally was needlessly convoluted because I forgot what one can do with `mdfind`.

Although the way I showed him how to do this was so much simpler because it was wrapped in a drag-and-drop Mac application created using [Sveinbjörn Þórðarson’s Platypus][playtpus], so no need to type the wrong thing into the shell and accidentally delete everything.

[hobbsy]: http://twitter.com/hobbsy
[playtpus]: http://sveinbjorn.org/platypus/
[codes]: http://arstechnica.com/staff/2009/09/metadata-madness/
[mdfind]: https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man1/mdfind.1.html

6 thoughts on “How to fix “ghost” files in the Finder

  1. david Post author

    @Adrian Please give some details of the problem you are having. What command did you run? What was the result? What were you were expecting to happen?

  2. Josh

    You are awesome. I couldn’t for the life of me figure out what was going on with the files and Windows opened them without issue. Even Quick View worked, but VLC or any other proper media player couldn’t touch them. Disappointed in Apple for this one as I see posts that are more than 2 years old looking for answers. Thankfully NIX is underneath OS X’s quirks so they can be fixed. Thanks much.

  3. Michael

    I have the same issue as Adrian. I ran the command and individual files were corrected, but folders in that path were not and are still “ghosts”. Is there an edit to the command (ie different type or creator code that broke/MACS? ) that will adjust folders? I’ve seen other threads suggest that the type is set to bsy for folders…

  4. david Post author

    @Michael

    You want to search for folders with a creation date of 8 a.m. on 24 January 1984 and then change the creation date. You can get and set the creation date with the GetFileInfo and SetFile tools (used to be only installed with the developer tools, think it is installed as standard since 10.6).

    I spent five minutes trying to work out mdfind’s date format, but gave up. Might write up a full answer soon.

  5. Scott

    I came across this page looking for a solution to brok / macs in yosemite. Nothing I’ve been able to find works except

    setfile -t ” *

Leave a Reply

Your email address will not be published. Required fields are marked *