How to compress folders on a mac without DS_Store files

An easy way to get rid of ds_store files while compressing folders on Mac.

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:

Open terminal.

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

Now in order to zip the folder use the below command:

zip -r my-folder.zip my-folder -x "*.DS_Store"

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.

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"

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.

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.

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.