32

I'm trying to add unzipped files to an existing already zipped folder say new folder.zip. Is it possible to use zip -r new folder.zip after adding some unzipped files to them? Will this command compress the folder? Is there any alternative to do this?

  • you can pipe it, unzip the original file | then zip up the entire content as how you'd create a brand new file. I'm not sure how you'd do this with a single command. – unixmiah Oct 13 '14 at 19:44
44

Use the update flag: -u

Example:

zip -ur existing.zip myFolder

This command will compress and add myFolder (and it's contents) to the existing.zip.


Advanced Usage:

The update flag actually compares the incoming files against the existing ones and will either add new files, or update existing ones.

Therefore, if you want to add/update a specific subdirectory within the zip file, just update the source as desired, and then re-zip the entire source with the -u flag. Only the changed files will be zipped.

If you don't have access to the source files, you can unzip the zip file, then update the desired files, and then re-zip with the -u flag. Again, only the changed files will be zipped.

Example:

Original Source Structure


ParentDir
├── file1.txt
├── file2.txt
├── ChildDir
│   ├── file3.txt
│   ├── Logs
│   │   ├── logs1.txt
│   │   ├── logs2.txt
│   │   ├── logs3.txt

Updated Source Structure


ParentDir
├── file1.txt
├── file2.txt
├── ChildDir
│   ├── file3.txt
│   ├── Logs
│   │   ├── logs1.txt
│   │   ├── logs2.txt
│   │   ├── logs3.txt 
│   │   ├── logs4.txt <-- NEW FILE 

Usage

$ zip -ur existing.zip ParentDir 
> updating: ParentDir/ChildDir/Logs (stored 0%)
>   adding: ParentDir/ChildDir/Logs/logs4.txt (stored 96%)
  • Thanks! How can I specify the folder in the zip to add it to? – Aaron Esau Apr 20 '16 at 1:08
  • I added a section to answer your question. :) – Ethan Strider Apr 26 '16 at 14:50
0

I believe you can add files using this command:

zip -r zipfile.zip newfile.txt newfile1.txt

The -r option of the zip command allows you to adding files.

  • -r is recursive option. – ctrl-alt-delor Oct 13 '14 at 20:06
  • @richard, I am gonna fix that. Gimme a few – ryekayo Oct 13 '14 at 20:06
  • @richard, turns out the source I got that from says you can use the -r command to modify an existing zipped file: linuxnix.com/2014/05/… – ryekayo Oct 13 '14 at 20:07
  • 2
    add — Update existing entries and add new files. If the archive does not exist create it. This is the default mode. (is no option is needed to replace or add files. -r can be used if adding directories, or when not adding directories. Well any time that you don't want to just add the directory and no content, so pretty much all of the time. So in short the example is correct, but the explanation is wrong.) (best to test it, this is just my interpretation of the manual). – ctrl-alt-delor Oct 13 '14 at 20:16
  • 2
    For safety reasons, are you SURE you want to modify an existing archive? Instead I'd at least make a safety copy, do the add, and then recheck the integrity of the archive before committing to the new one. – mdpc Oct 13 '14 at 21:18
0

The -u flag works for files to

zip -ur zip_to_upload.zip file_to_add.txt

Regards

0

I used with success the zip command. with the grow (-g) option, in order to add a subfolder folder1/folder2 containing the files fileA.txt and fileB.txt:

zip -gr folder.zip folder1/folder2

-r means recurse dir.

From my understanding -g should grow the zip files, not creating a new zip file (that may be useful with very large zip archives). In case of issues you can try first to create subfolders in folder.zip with (e.g.):

zip -g folder.zip folder1
zip -g folder.zip folder2
0

If you have lots of file in the same place with your zip archive and you want to copy all of them into the archive, you can do:

zip -ur zipfile.zip !(zipfile.zip)

This updates your zip file( the -u option) recursively (-r option) with all the files found except the zip file itself ( ! is the negation of the zipfile)

Then, you can simply delete all the files except the zip file following the same rule above:

rm !(zipfile.zip)
New contributor
despina is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.