4

If I have the following zip:

my.zip
|- folder
|  `- to-update.txt
`- some-other-file.txt 

and a folder structure:

working-directory
`- folder
   `- to-update.txt

I wish to copy the file to-update.txt from my working directory into the zip. To do that I would use:

# Currently in working-directory
zip -ru /path/to/my.zip .

However, if my.zip's copy of to-update.txt is newer, this will not work and zip outputs (with -v):

zip diagnostic: folder/ up to date
zip diagnostic: folder/to-update.txt up to date

How can I get it to overwrite any files (and still add new ones) from my filesystem/working directory even if they have an older timestamp?

Unlike the file-sync option, I do not want to remove missing files, so some-other-file.txt should remain untouched.

7
  • Doesn't simply zip my.zip Folder/to-update.txt do what you want? In my test it does. Commented Jun 13, 2023 at 12:11
  • I want to sync the whole folder. There may be multiple files in folder or even in working-directory.
    – Druckles
    Commented Jun 13, 2023 at 14:53
  • Then recursivlty, like zip -r my.zip folder/ Commented Jun 13, 2023 at 15:01
  • Yep, seems to work, thank you. Please feel free to post an answer.
    – Druckles
    Commented Jun 13, 2023 at 15:06
  • 1
    @BenVoigt but his issue was that there is no outright "replace" flag Commented Jun 14, 2023 at 14:25

3 Answers 3

13

Simply re-adding the files to the archive should accomplish this:

zip -r my.zip .

-u does the same but only if the files are newer.

1

My (hacky) solution is to change/update the timestamps of the files. As it was in a Docker container, we can live with it.

With touch, built from these solutions:

find working-directory -mindepth 1 -exec touch -m {} +
  • -mindepth ignores the working directory: .
  • -m only changes the modified timestamp and leaves the access timestamp
  • + instead of \; is the difference between touch -m x y z and touch x && touch y && touch z

As I was in Docker though, I did not have write access to the files and had to copy them before zipping them:

cp -r working-directory /tmp/working-directory/
(cd /tmp/working-directory && zip -ru /path/to/my.zip .)
rm -rf /tmp/working-directory

Not the nicest solution, but it'll do. If anyone has any one-liners with zip (installing unzip, for instance, is not desired), please feel free to post them.

0

The simple solution would be to delete the file before adding it again:

zip -rd /path/to/my.zip to-update.txt
zip -ru /path/to/my.zip to-update.txt

(As I'm not using zip, the commands above were written theoretically.)

1
  • 1
    -d doesn't delete with -r. -d on its own requires a list of files, so this doesn't work as given for the specified use case, where the files to update/delete are defined by the directory structure.
    – Druckles
    Commented Jun 13, 2023 at 14:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .