58

To obtain a fresh zip file just like as tar does, do I have to perform rm foo.zip before executing zip?

$ mkdir foo; touch foo/bar
$ zip -r foo.zip foo
  adding: foo/ (stored 0%)
  adding: foo/bar (stored 0%)
$ rm foo/bar; touch foo/baz
$ zip -r foo.zip foo
  adding: foo/ (stored 0%)
  adding: foo/baz (stored 0%)
$ unzip -l foo.zip
Archive:  foo.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2011-10-27 07:49   foo/
        0  2011-10-27 07:49   foo/bar
        0  2011-10-27 07:49   foo/baz
---------                     -------
        0                     3 files

2 Answers 2

60

Use the -FS option to "file sync"

zip -FSr foo.zip foo

This will add any new files in the folder to the zip, and delete any files from the zip that aren't in the folder.

5
  • Which version of zip has this option? My zip command does not recognise "-S"
    – Jayan
    Jun 26, 2014 at 11:08
  • 5
    The switch is -FS not -F and -S together. This has been there since zip 3.0 at least. Check man zip.
    – Paul
    Jun 26, 2014 at 13:53
  • How is "new file" defined? Its filesize changes, date modified, both? Just curious
    – Moseleyi
    May 31, 2018 at 4:20
  • This just work for Zip 3.0 and don't work for Zip 2.32 (didn't remove old files in zip pack) Jul 20, 2019 at 23:05
  • You mean -FS -r, right?
    – avalanche1
    Sep 5, 2019 at 17:31
13

An alternative to using the -FS option (or deleting the old ZIP file first), is to overwrite the existing ZIP file by having zip output to stdout and redirect it to the designated file:

zip -r - foo >foo.zip

If foo.zip exists, it will be overwritten by shell redirection, meaning you'll get a fresh new ZIP file 100% of the time, every time.

Option Explanation
-r Recursively add directory and its contents
- Output ZIP contents to stdout
foo The directory to be zipped
>foo.zip Redirect stdout to file foo.zip
1
  • You mean output to stdout
    – flarn2006
    Aug 26, 2019 at 19:53

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.