how do I zip a whole folder tree in unix, but only certain files?


Unix Problem Overview

I've been stuck on a little unix command line problem.

I have a website folder (4gb) I need to grab a copy of, but just the .php, .html, .js and .css files (which is only a couple hundred kb).

I'm thinking ideally, there is a way to zip or tar a whole folder but only grabbing certain file extensions, while retaining subfolder structures. Is this possible and if so, how?

I did try doing a whole zip, then going through and excluding certain files but it seemed a bit excessive.

I'm kinda new to unix.

Any ideas would be greatly appreciated.

Unix Solutions

Solution 1 - Unix

Switch into the website folder, then run

zip -R foo '*.php' '*.html' '*.js' '*.css' 

You can also run this from outside the website folder:

zip -r foo website_folder -i '*.php' '*.html' '*.js' '*.css'

Solution 2 - Unix

You can use find and grep to generate the file list, then pipe that into zip


find . | egrep "\.(html|css|js|php)$" | zip -@

(-@ tells zip to read a file list from stdin)

Solution 3 - Unix

This is how I managed to do it, but I also like ghostdog74's version.

tar -czvf archive.tgz `find  test/ | egrep ".*\.html|.*\.php"`

You can add extra extensions by adding them to the regex.

Solution 4 - Unix

I liked Nick's answer, but, since this is a programming site, why not use Ant to do this. :)

Then you can put in a parameter so that different types of files can be zipped up.

Solution 5 - Unix

you may want to use find(GNU) to find all your php,html etc files.then tar them up

find /path -type f \( -iname "*.php" -o -iname "*.css" -o -iname "*.js" -o -iname "*.ext" \) -exec tar -r --file=test.tar "{}" +;

after that you can zip it up

Solution 6 - Unix

You could write a shell script to copy files based on a pattern/expression into a new folder, zip the contents and then delete the folder. Now, as for the actual syntax of it, ill leave that to you :D.


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionwilldanceforfunView Question on Stackoverflow
Solution 1 - UnixCurtis TaskerView Answer on Stackoverflow
Solution 2 - UnixNickView Answer on Stackoverflow
Solution 3 - UnixMitMaroView Answer on Stackoverflow
Solution 4 - UnixJames BlackView Answer on Stackoverflow
Solution 5 - Unixghostdog74View Answer on Stackoverflow
Solution 6 - UnixbarfoonView Answer on Stackoverflow