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.
Is it a “Folder” or a “Directory”?
Aside: In Linux, what Windows users call “folders” are called “directories” – in case you wondered.
First up, a little command to zip a directory and all its contents, so you know the basics.
Zip a directory and all its contents
The following command in Linux will get your directory zipped up, with all its contents (known as recusively zipping a directory – recursively meaning “going into all subdirectories and getting them, infinitely, until there are no more contents to fish out…”).
zip -r zipfilename.zip directoryname
In the above:
- zipfilename.zip = the name you want the zip you make to have.
- directoryfilename = the name of the directory you want to collect all contents of and archive (zip up).
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 directory and subdirectory 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.
If you’re still confused by this, I can make a video tutorial. Leave a comment.
Hello,
I am trying to ZIP a directory and want to exclude a couple of subdirectories. For this I have used the below command as per your article in my CentOS 7 system:
Code:
# zip -r myproject.zip myproject -x myproject/backup1/**\* myproject/backup2/**\*
However that will still include subdirectories named (but there are no content of myproject/backup1 and myproject/backup1 ) within myproject/backup1 and myproject/backup2
I need to totally exclude ‘backup1’ and ‘backup2’ directories.
Am I doing something wrong? What will be the exact command?
Please help me.
Any suggestion should be appreciated.
Thanks,
Hey Tarak,
I think you’d want to just exclude the whole subdirectory – so don’t put `myproject/backup1/**\*` instead put `myproject/backup1` – or similar. Note I’m writing this free text from the WP admin though so I could be wrong 🙂
Good luck
Actually it doesn’t work 🙁 This is driving me crazy for two hours now …. How to exclude node_modules and ALL its subdirs and files.
I had the same issue and found out that you need to wrap it in ‘ and put a * at the end. Like this -x ‘folder_to_exclude/*’