76

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?

dhag
  • 15,736
  • 4
  • 55
  • 65
boo na
  • 761
  • 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

5 Answers5

103

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 &lt-- NEW FILE 

Usage

$ zip -ur existing.zip ParentDir 
> updating: ParentDir/ChildDir/Logs (stored 0%)
>   adding: ParentDir/ChildDir/Logs/logs4.txt (stored 96%)
9

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.

ryekayo
  • 4,763
  • 5
    -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
  • 1
    @richard, turns out the source I got that from says you can use the -r command to modify an existing zipped file: http://www.linuxnix.com/2014/05/linuxunix-zip-and-unzip-command-examples.html – 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
  • Thanks for that, I had to relook the answer as I knew that -r stood for recursive and never used it to add files to an existing zipped file. – ryekayo Oct 13 '14 at 20:17
  • 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
4

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
1

The -u flag works for files to

zip -ur zip_to_upload.zip file_to_add.txt

Regards

David Lopes
  • 111
  • 1
1

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)
despina
  • 111
  • 2