DS_Store files are created by Finder to keep track of custom attributes of a folder. These custom attributes can be the position of icons, choice of a background image, etc. So when you compress a folder, mac does not exclude these hidden files by default.

This is problematic especially when you are developing a WordPress plugin or a theme and you need to submit the same to WordPress.org or ThemeForest.net or CodeCanyon.net. These markets require you to cleanly zip your theme and plugins, that is compress without hidden files before you submit.

Fortunately, there is an easy way to exclude DS_Store files while compressing a folder. This method involves the use of the terminal. It is basically a small command that you need to run on the terminal to compress a folder. So let’s compress a folder on a mac without DS_Store files by following the below steps:

  1. Open terminal.
  2. Move to the parent folder of the folder you need to compress. For example, if I need to compress a folder placed on my desktop then I will first move to my desktop by using the below command.
    • cd Desktop
  3. Now in order to zip the folder use the below command:
    • zip -r my-folder.zip my-folder -x "*.DS_Store"
  4. You must replace “my-folder” with the actual name of your folder. And this is all you need to do to get a clean zip file.

Command to compress folders on mac

Code Explanation

  • zip is a compression and packaging file utility for Unix.
  • -r is for recursively including all folders underneath the target folder.
  • my-folder.zip is the name of the compressed file we want to create.
  • my-folder is the name of our target folder. The folder we want compressing.
  • -x "*.DS_Store" is to exclude all files whose path ends with the string “.DS_Store”.

Shell Script To Remove DS_Store Files While Compressing

It can be difficult to remember the command if not used every other day. So in order to make it easier to remember we will create a shell script. A shell script is a collection of commands that are stored in a file.

Create a new file called “twd_clean_zip.sh”. Add the below code in the file –

#! /usr/bin/env bash
zip -r $1.zip $1 -x "*.DS_Store"

Now in order to make our script globally accessible, we need to add this file to one of our global paths. You can check the various paths under $PATH variable by executing “echo $PATH” in the terminal.

It is always good to keep your scripts away from system scripts. So for this, we will add a custom path to our global paths first. The custom path will be used to store our shell script.

Open a new terminal window (you should be inside your home directory, for example – “/Users/twd”) and execute the below command –

sudo nano .bash_profile

This will open a new file in which we will define our custom path. Copy and paste the below text into the terminal. Make sure you replace “/Users/twd/shell_scripts” with your own custom path.

export PATH="$PATH:/Users/twd/shell_scripts"

Export path

Now press “control” and “x” keys together to exit file editing on the terminal. You will be asked to save the changes, just press “y” key and then “return” key to save the changes. Re-open the terminal application and check your global paths again by executing “echo $PATH”. The custom path we added to global paths should show up under $PATH variable.

Add command to path

Next, we need to move our script file (twd_clean_zip.sh) under our custom path. You can simply copy paste the file or use the terminal to move the file. Once the file is under our custom path we need to change it’s permissions so that the script is executable. Move to the custom path directory and execute the below command.

chmod +x twd_clean_zip.sh

You should now be able to cleanly zip files using a simple command:

sudo twd_clean_zip.sh my-folder

Do not forget that you need to be inside target folder’s parent directory for the above command to work.

Clean zip script

Creating a shell script is completely optional. But it is really helpful in the long run as you do not have to remember that complete line of code to compress a folder without DS_Store files.

Do you like Gautam Thapar's articles? Follow on social!
People reacted to this story.
Show comments Hide comments
Comments to: How to compress folders on a mac without DS_Store files
  • February 7, 2018

    Thanks so much for this article! I work with many people who create WordPress themes and plugins. A lot of them use a Mac to develop (that’s what creatives do).

    I am a coder, so I’m on Windows mostly. When I pack up a theme/plugin for distribution I always feed my OCD and delete the .DS_Store folders.

    Big boon, this article. I have shared with many. Thanks again for taking the time to write this. 🙂

  • October 31, 2018


  • November 13, 2018

    Great tutorial. Thanks!

  • April 28, 2019

    Don’t use sudo for any of these commands! This will lead to broken file permissions later on.

Write a response

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

Attach images - Only PNG, JPG, JPEG and GIF are supported.

This site uses Akismet to reduce spam. Learn how your comment data is processed.


Join TheWebsiteDev