How to: Zip contents of a directory, excluding certain sub-directory

How to: Zip contents of a directory, excluding certain sub-directory featured image
(Last Updated On: February 20, 2018)

Have you ever wanted to zip an entire directory, but exclude the contents of one or two (or more!) sub-directories, using PuTTy (or another terminal) over SSH.

I know I have – this is a great way to backup a website, or make a copy of a website, and exclude big folders you don’t need, like the contents of a caching, or some other backup folder, which you just don’t need.

Recursively zip a directory and all contents – excluding one subdirectory

The command below will allow you to do just that!

zip -r zipperall.zip dir -x dir/subdir/**\*

Obviously you should replace zipfilename, dir, and subdir, with your actual directy names.

Recursively zip a directory and all contents – excluding more than one subdirectory

If you have more than one subdirectory to zip up, just add a space and repeat the code at the end, for example:

zip -r zipperall.zip dir -x dir/subdir/**\* dir/anothersubdir/**\*

This will zip up the entire contents of your directory, but skipping the contents of those sub directories.

Linux distro notes

Note you might need to put quotes around some of this in certain flavours, but this should work in Ubuntu or CentOS Linux, which are our favourites.

Talk to us!

Leave a comment below if this helped – or didn’t help – you, or you have a general code improvement you’d like to leave.

Leave a Reply

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

Silicon Dales