Search code examples
bashdirectoryzip

zipping a folder and its sub folder


Is there any way I can zip a folder and also zip one of the subfolder in a single line bash command?

Changes/
   folder1/
   folder2/
   folder3/         

I want to zip Changes (parent folder) and folder3 (sub-folder)

Note: I don't want to create zip folder extra (no duplication) i.e. after zip I don't want to see this:

Changes.zip 
  Changes/   (Duplicate folder)
    Folder1/
    Folder2/
    Folder3.zip
     Folder3/ (Duplicate folder)

Expected Output

Changes.zip
  Folder1/
  Folder2/
  Folder3.zip

Code I've run

cd Changes/; zip -r ../Changes.zip * ; 

Solution

  • How about this:

    cd Changes/ && zip -r Folder3.zip Folder3/* && rm -r Folder3/ && cd .. && zip Changes.zip Changes/* && rm -r Changes/
    

    It's not elegant, but should work.

    I've used && instead of ; so that it won't proceed to the second command unless the first completes successfully, and added the wildcard * to the end of each path to get the contents of the directory and not the directory itself ("no duplication", as you put it). Then we use rm -r to recursively remove the directory which has been zipped.