289

When I compress files with the built in zip compressor in Mac OSX, it causes an extra folder titled "__MACOSX" to be created in the extracted zip.

Can I adjust my settings to keep this folder from being created or do I need to purchase a third party compression tool?

UPDATE: I just found a freeware app for OSX that solves my problem: "YemuZip"

UPDATE 2: YemuZip is no longer freeware.

AJM's user avatar
AJM
1,8923 gold badges18 silver badges34 bronze badges
askedJun 7, 2012 at 0:39
Scott B's user avatar
5
  • The existence of _MACOSX folder doesn't creates any problem. Simply delete it.CommentedMar 14, 2014 at 6:04
  • 3
    This question appears to be off-topic because it should be in AskDifferentCommentedJan 12, 2015 at 2:48
  • 30
    It creates a problem with picky web services that require a pristine zip archive, so I'm grateful for the explanationsCommentedNov 19, 2015 at 18:15
  • 43
    @JayprakashDubey It's incredibily irritating, and as a linux user who likes not leaving trash everywhere I go, I can't stand__MACOSX,.DS_Store,._fileName, and all the other assorted turds Mac OS drops in its archives. OSX's `tar' does this as well - in blatant disregard for theprinciple of least astonishment.CommentedJun 28, 2016 at 18:13
  • Also see:apple.stackexchange.com/questions/239578/…CommentedAug 17, 2021 at 23:32

16 Answers16

472

Can be fixed after the fact byzip -d filename.zip __MACOSX/\*

And, to also delete.DS_Store files:zip -d filename.zip \*/.DS_Store

Saikat's user avatar
Saikat
17.5k22 gold badges127 silver badges146 bronze badges
answeredDec 2, 2012 at 1:28
Chris Johnson's user avatar
Sign up to request clarification or add additional context in comments.

9 Comments

to also delete .DS_Store files:zip -d filename.zip \*/.DS_Store
The accepted answer says how not to get the __MACOSX/ directory in the first place, but says nothing about what to do with an existing .zip file. This answer says how to get rid of __MACOSX/ if you've already got it. However, the original question was how not to get it in the first place, so the accepted answer is the correct one.
How might I do this recursively for all zip files?
To apply to all zip files under some root:find ~ -type f -name '*.zip' -exec zip -d '{}' __MACOSX/\* \;. Substitute your starting point for~.
A function to remove all junk files:cleanzip(){ zip -d $1 $2 __MACOSX/\* \*/.DS_Store; }. Usage:cleanzip file.zip orcleanzip file.zip MORE_JUNK_FILES (second parameter is optional)
|
135

When I had this problem I've done it from command line:

zip file.zip uncompressed

EDIT, after many downvotes: I was using this option for some time ago and I don't know where I learnt it, so I can't give you a better explanation. Chris Johnson'sanswer is correct, but I won't delete mine. As one comment says, it's more accurate to what OP is asking, as it compress without those files, instead of removing them from a compressed file. I find it easier to remember, too.

Koray Tugay's user avatar
Koray Tugay
24k49 gold badges206 silver badges335 bronze badges
answeredJul 10, 2012 at 12:47
sinuhepop's user avatar

5 Comments

This lacks explanation.
The __MACOSX/ subdirectory contains Mac resource forks and is created when you use Mac tools to create the zip file. Likewise, Mac tools will consume the __MACOSX/ subdirectory in order to set resource forks, and you'll never even see it. However, if you use Mac tools to create the zip file and some other tools to unpack it, you'll get the __MACOSX/ directory and not the resource forks. If you create the file withzip, which is a 3rd-party app, then the __MACOSX/ directory never gets created in the first place.
See my answer for an easy way to delete the _MACOSX folder after the zip file is created.
To compress a directory recursively without __MACOSX and other:zip -rX file.zip uncompressed_directory
prevention is better than cure, and simple is better than complicated. Now if we add this a an Automator service we have ourselves a productivity beast here. Great solution.
112

Inside the folder you want to be compressed, in terminal:

zip -r -X Archive.zip *

Where -X means: Exclude those invisible Mac resource files such as “_MACOSX” or “._Filename” and .ds store files

source

Note as per comment by@SpinUp__ADavis: Will only work for the folder and subsequent folder tree you are in and has to have the* wildcard.

answeredApr 29, 2014 at 18:12
pompalini's user avatar

6 Comments

This does not work as you think it does:-X does not exclude .DS_Store files. The reason it may work for your application is that you're only zipping*, which is a wildcard matching files that don't begin with ".". Try it on a directory containing a dot-file, and you'll see it gets included.
See my answer for an easy way to delete the _MACOSX folder after the zip file is created.
No. Doingzip -r -X Archive.zip myfolder on a folder still results in a zip file containing .DS_Store file.
@Rockallite: you are correct this will only work when you zip the folders, tree or files in the folder you are in. Hence you cannot pass a folder as an argument or it won't work as expected, only with the * wildcard will it work.
|
48

This command did it for me:

zip -r Target.zip Source -x "*.DS_Store"

Target.zip is the zip file to create.Source is the source file/folder to zip up. The-x parameter specifies the file/folder to exclude.

If the above doesn't work for whatever reason, try this instead:

zip -r Target.zip Source -x "*.DS_Store" -x "__MACOSX"
answeredMar 21, 2014 at 14:24
Adil Hussain's user avatar

2 Comments

The OP asked how to exclude the __MACOSX folder. Your first code snippet obviously doesn't work, because it doesn't name that as a folder to exclude.
Thezip command line utility never creates a__MACOSX directory, so the first snippet does work, and the-x "__MACOSX" part is not needed.
22

I'm using this Automator Shell Script to fix it after.It's showing up as contextual menu item (right clicking on any file showing up in Finder).

while read -r p; do  zip -d "$p" __MACOSX/\* || true  zip -d "$p" \*/.DS_Store || truedone
  1. Create a new Service with Automator
  2. Select "Files and Folders" in "Finder"
  3. Add a "Shell Script Action"

automator shell script

contextual menu item in Finder

adamsfamily's user avatar
adamsfamily
2,0042 gold badges25 silver badges43 bronze badges
answeredMay 25, 2016 at 7:05
benedikt's user avatar

5 Comments

Wouldn't it be better to do the compressing using zip in the Automator Shell Script, rather than compress using Apple built-in, then remove unwanted files?
@Matt you can add the archive step in automator or use another shell command as well.
@Matt also, Apple built-in handles automatic file naming and name increment when file exists. With shell script this part would be a lot of work.
It throws an error if the .DS_Store folder is not present in the archive. I suggest an improvement by adding ` || true` after each zip command (such aszip -d "$p" __MACOSX/\* || true into your answer.
While the other answers are good, I like this one best because it can be used in a very Macish way.
17
zip -r "$destFileName.zip" "$srcFileName" -x "*/\__MACOSX" -x "*/\.*"
  • -x "*/\__MACOSX": ignore __MACOSX as you mention.
  • -x "*/\.*": ignore any hidden file, such as .DS_Store .
  • Quote the variable to avoid file if it's named with SPACE.

Also, you can build Automator Service to make it easily to use in Finder.Check link below to see detail if you need.

Github

answeredJun 11, 2018 at 8:10
Jian Jason's user avatar

2 Comments

Heh, the only correct answer amongst a sea of garbage, is also the one with the least votes. /sigh
It worked for me but I had to remove the quotes around and the dollar symbol before destFileName.zip and srcFileName
16

The unwanted folders can be also be deleted by the following way:

zip -d filename.zip "__MACOSX*"

Works best for me

Machavity's user avatar
Machavity
31.8k27 gold badges97 silver badges108 bronze badges
answeredMay 9, 2016 at 15:51
SabU's user avatar

Comments

12

Cleanup .zip from.DS_Store and__MACOSX, including subfolders:

zip -d archive.zip '__MACOSX/*' '*/__MACOSX/*' .DS_Store '*/.DS_Store'

Walkthrough:

  1. Create .zip as usual by right-clicking on the file (or folder) and selecting "Compress ..."
  2. Open Terminal app (search Terminal in Spotlight search)
  3. Typezip in the Terminal (but don't hit enter)
  4. Drag .zip to the Terminal so it converts to the path
  5. Copy paste-d '__MACOSX/*' '*/__MACOSX/*' .DS_Store '*/.DS_Store'
  6. Hit enter. Congratulations! Your archive is sparkling clean!
  7. Usezipinfo archive.zip to list files inside to check (optional)
answeredApr 8, 2021 at 10:07
Afanasii Kurakin's user avatar

Comments

9

You can't.

But what you can do is delete those unwanted folders after zipping. Command linezip takes different arguments where one, the-d, is for deleting contents based on a regex. So you can use it like this:

zip -d filename.zip __MACOSX/\*
answeredApr 26, 2016 at 4:52
Alejandro Pablo Tkachuk's user avatar

Comments

7

Thezip command line utility never creates a__MACOSX directory, so you can just run a command like this:

zip directory.zip -x \*.DS_Store -r directory

In the output below,a.zip which I created with thezip command line utility does not contain a__MACOSX directory, buta 2.zip which I created from Finder does.

$ touch a$ xattr -w somekey somevalue a$ zip a.zip a  adding: a (stored 0%)$ unzip -l a.zipArchive:  a.zip  Length     Date   Time    Name --------    ----   ----    ----        0  01-02-16 20:29   a --------                   -------        0                   1 file$ unzip -l a\ 2.zip # I created `a 2.zip` from Finder before thisArchive:  a 2.zip  Length     Date   Time    Name --------    ----   ----    ----        0  01-02-16 20:29   a        0  01-02-16 20:31   __MACOSX/      149  01-02-16 20:29   __MACOSX/._a --------                   -------      149                   3 files

-x .DS_Store does not exclude.DS_Store files inside directories but-x \*.DS_Store does.

The top level file of a zip archive with multiple files should usually be a single directory, because if it is not, some unarchiving utilites (likeunzip and7z, but not Archive Utility, The Unarchiver,unar, ordtrx) do not create a containing directory for the files when the archive is extracted, which often makes the files difficult to find, and if multiple archives like that are extracted at the same time, it can be difficult to tell which files belong to which archive.

Archive Utility only creates a__MACOSX directory when you create an archive where at least one file contains metadata such as extended attributes, file flags, or a resource fork. The__MACOSX directory contains AppleDouble files whose filename starts with._ that are used to store OS X-specific metadata. Thezip command line utility discards metadata such as extended attributes, file flags, and resource forks, which also means that metadata such as tags is lost, and that aliases stop working, because the information in an alias file is stored in a resource fork.

Normally you can just discard the OS X-specific metadata, but to see what metadata files contain, you can usexattr -l.xattr also includes resource forks and file flags, because even though they are not actually stored as extended attributes, they can be accessed through the extended attributes interface. Both Archive Utility and thezip command line utility discard ACLs.

answeredJan 2, 2016 at 18:50
nisetama's user avatar

3 Comments

This only works for directories, not for a bunch of file inside a directory.
@MichaelTrouw Yes, but you should not usually create an archive that contains multiple files at the top level in the first place.
I agree to disagree because there are use cases where this is fine. For example, one in which the end user of a zipfile will never be a user. But a server.
7

I have a better solution after read all of the existed answers. Everything could done by a workflow in a single right click.NO additional software,NO complicated command line stuffs andNO shell tricks.

The automator workflow:

  • Input: files or folders from any application.

  • Step 1: Create Archive, the system builtin with default parameters.

  • Step 2: Run Shell command, with input as parameters. Copy command below.

    zip -d "$@" "__MACOSX/*" || true

    zip -d "$@" "*/.DS_Store" || true

Save it and we are done! Just right click folder or bulk of files and choose workflow from services menu. Archive with no metadata will be created alongside.

IMAGE UPDATE: I chose "Quick Action" when creating a new workflow - here’s an English version of the screenshot:

enter image description here

Sample

Dave Everitt's user avatar
Dave Everitt
17.9k8 gold badges69 silver badges101 bronze badges
answeredDec 19, 2017 at 5:11
ttimasdf's user avatar

5 Comments

This is the best solution! Thank you so much! I used the camera in Google Translate to verify some of the specific settings in this screenshot (because I can't read Chinese myself, unfortunately), but this works beautifully. It creates a clean Archive.zip in the same folder. Very good. :-)
@ErikvanderNeut sorry for the inconvenience, I just casually post a screenshot of my own configuration at that time 😅 I'll update the answer later
It's all good man :-) I really like your solution, so thank you for posting it.
It would be better if you capture the screenshot in English.
@Anduin - just added a screenshot in English after reproducing this for my own workflow.
2

do not zip any hidden file:

zip newzipname filename.any  -x "\.*"

with this question, it should be like:

zip newzipname filename.any  -x "\__MACOSX"

It must be said, though, zip command runs in terminal just compressing the file, it does not compress any others. So do this the result is the same:

zip newzipname filename.any
answeredMay 9, 2017 at 1:39
Xian Shu's user avatar

2 Comments

__MACOSX does not start with.
yeah, I made a mistake. It has been modified. Thanks for reminding me
2

Keka does this. Just drag your directory over the app screen.

enter image description here

answeredApr 23, 2022 at 17:27
Christina's user avatar

1 Comment

Note that it's specifically the "Exclude Mac resource forks" checkbox that prevents the folder and files from being created in the archive.
1

Do you mean thezip command-line tool or the Finder's Compress command?

Forzip, you can try the--data-fork option. If that doesn't do it, you might try--no-extra, although that seems to ignore other file metadata that might be valuable, like uid/gid and file times.

For the Finder's Compress command, I don't believe there are any options to control its behavior. It's for the simple case.

The other tool, and maybe the one that the Finder actually uses under the hood, isditto. With the-c -k options, it creates zip archives. With this tool, you can experiment with--norsrc,--noextattr,--noqtn,--noacl and/or simply leave off the--sequesterRsrc option (which, according to the man page, may be responsible for the__MACOSX subdirectory). Although, perhaps the absence of--sequesterRsrc simply means to use AppleDouble format, which would create ._ files all over the place instead of one__MACOSX directory.

answeredJun 7, 2012 at 1:09
Ken Thomases's user avatar

1 Comment

Yes, I'm referring to the finder's "Compress" utility. As it is, I'm getting a folder full of ._ files which screws up WordPress' appearance/editor
0

This is how i avoid the__MACOSX directory when compress files withtar command:

$ cd dir-you-want-to-archive$ find . | xargs xattr -l # <- list all files with special xattr attributes..../conf/clamav: com.apple.quarantine: 0083;5a9018b1;Safari;9DCAFF33-C7F5-4848-9A87-5E061E5E2D55./conf/global: com.apple.quarantine: 0083;5a9018b1;Safari;9DCAFF33-C7F5-4848-9A87-5E061E5E2D55./conf/web_server: com.apple.quarantine: 0083;5a9018b1;Safari;9DCAFF33-C7F5-4848-9A87-5E061E5E2D55

Delete the attribute first:

find . | xargs xattr -d com.apple.quarantine

Runfind . | xargs xattr -l again, make sure no any file has the xattr attribute. then you're good to go:

tar cjvf file.tar.bz2 dir

answeredFeb 23, 2018 at 14:42
Zhang Huangbin's user avatar

Comments

0

Another shell script that could be used with the Automator tool (see also benedikt's answer on how to create the script) is:

while read -r f; do    d="$(dirname "$f")"    n="$(basename "$f")"    cd "$d"    zip "$n.zip" -x \*.DS_Store -r "$n"done

The difference here is that this code directly compresses selected folders without macOS specific files (and not first compressing and afterwards deleting).

answeredOct 13, 2020 at 22:59
Jurij's user avatar

Comments

Protected question. To answer this question, you need to have at least 10 reputation on this site (not counting theassociation bonus). The reputation requirement helps protect this question from spam and non-answer activity.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.