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 ZIPfile.zip `unzip -l ZIPfile.zip | grep .DS_Store | awk '{print $4}'`
where, naturally, ZIPfile.zip 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.
Thanks for the code… and yes, it works! For those who don’t want to bother with the command line, Terminal, etc., here is an easy to use, drag and drop application:
http://gotoes.org/ZipMacFilesForPC/index.htm
zipper
12 Dec 08 at 5:37 PM
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
mike
10 Apr 09 at 8:00 PM
Mike, you can use a simple shell loop to achieve what you want:
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.
Meitar
11 Apr 09 at 1:39 AM
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.
Regards,
Mark
mark
18 Nov 09 at 7:32 PM
Hey Meitar, funny to stumble upon a former 5th-ave-veteran :o)
Was looking for something like that… terminal is all fine, but nowadays I like it less fancy, so here’s a good (and free) app that does the same.
ZipCleaner from http://homepage.mac.com/roger_jolly/software/
Create zip… drag & drop on app… confirm.. done. ;o)
Eric
5 Jan 10 at 10:46 PM