Zip folder except for a subdirectory

1

To a folder in Linux use the command below.

zip -r arquivoZipado.zip pastaSerZipada/

How can I zip , except a subdirectory of folderSerZipada ?

    
asked by anonymous 08.06.2018 / 13:53

1 answer

2

zip has the flag -x or --exclude , which allows you to define file name and directory masks to ignore during the operation.

>

So, to ignore the entire subdir-fora-do-zip subdirectory in the compression of the following file structure ...

para-zipar/
  ├─ arquivo-no-zip
  ├─ subdir-fora-do-zip/
  │    └─ arquivo-fora-do-zip
  └─ subdir-no-zip
       └─ arquivo-no-zip

... execute:

zip -r zipado.zip para-zipar -x '*subdir-fora-do-zip*'
  

Note: Asterisks are important to ensure that the directory in question is ignored regardless of the level of recursion it is in.

Testing the compressed file, you can check the success of using flag -x :

$ unzip -t zipado.zip 
Archive:  zipado.zip
    testing: para-zipar/              OK
    testing: para-zipar/arquivo-no-zip   OK
    testing: para-zipar/subdir-no-zip/   OK
    testing: para-zipar/subdir-no-zip/arquivo-no-zip   OK
No errors detected in compressed data of zipado.zip.
    
09.06.2018 / 06:43