One Minute Mac Tip: Remove .DS_Store files from ZIP Archives

The Mac OS X Finder has some nifty features, one of which is an exceptionally useful contextual menu item to create ZIP archives of folders. Unfortunately, the Finder also has some really, really annoying habits, one of which is to create a file named .DS_Store in each folder a user opens (when not in Column view). What this means is that if you create a ZIP archive on your Mac and then send it to someone who unzips it without the Finder (such as a Windows user using the Windows Explorer), the recipient will see a lot of litter in the form of useless and meaningless .DS_Store files.

If you’re not afraid of the Terminal, this can be avoided. Put the following lines in your ~/.profile (or similar):

alias rmds='find . -name ".DS_Store" -type f -print0 | xargs -0 rm'

What this does is creates a new command that you can use (rmds) which recursively finds and deletes any regular file named “.DS_Store” starting from the current directory. Thus, running this command in the folder you are about to create an archive out of will clean it first, and will prevent unnecessary confusion on the part of your archive file recipient.

Alternatively, another way to do this is to use the command-line zip program and an (admittedly more complicated) pipeline to remove the .DS_Store files after they have been added to the archive. To do that, use this series of commands:

zip -d `unzip -l | grep .DS_Store | awk '{print $4}'`

where, naturally, is the ZIP archive you want to remove the .DS_Store files from. Creating an alias out of that command (and making it work for paths that contain spaces) is left as an exercise for the reader. ;)

As an aside, the alias, find and xargs commands are incredibly useful in their own right and can be used to do a lot of pretty amazing things. As always, man command will give you the nitty gritty.

Also as an aside, you can stop the Finder from creating .DS_Store files entirely when browsing network volumes (like Windows shares) with another command, documented in Apple’s Knowledge Base.

5 replies on “One Minute Mac Tip: Remove .DS_Store files from ZIP Archives”

  1. I had to have a new hard drive installed and all my data recovered. Suddenly I’m seeing .DS_Store files EVERYWHERE! I didn’t even know till now that files were hidden with an initial dot.

    Unrelated to zipping, is there a “find and change” routine I could run that would re-apply the dot to this file name throughout my directories so they will all be invisible again? Apparently the dots got removed during data transfer. Of course, OS X won’t let me add them manually…

    Thanks for any help!

    Mike Storer

  2. is there a “find and change” routine I could run that would re-apply the dot to this file name throughout my directories so they will all be invisible again?

    Mike, you can use a simple shell loop to achieve what you want:

    find . -name 'DS_Store' -print | while read line; do # find all files named "DS_Store"
        base=`basename "$line"` # get the filename, "DS_Store"
        dir=`dirname "$line"` # get the directory name
        mv "$dir/$base" "$dir/.$base" # rename appropriately

    Copy the above code, open the Terminal application, which will automatically place you at a shell prompt in your home directory, then paste the code above into the shell and press return.

  3. Hi Meitar,

    Thank you for the useful information. I have been playing with a DropScript 0.5 droplet I made to delete these invisible files (.DS_Store and ._*), but whenever the script runs, the modification date of the folder is also altered to reflect the date the script was run. I’ve been contemplating using a zip archive to hold the folder while the invisibles are removed as a means to preserve the mod date on the folder. I am pretty much a novice at this. Is this even possible? Could you suggest a method that might not even require making the zip archive in the first place?

    Thanks for your post and anything you could do to point me in the right direction.


Comments are closed.