1

Is it possible, from within a bash script, to add a file to a zip file, specifying the destination directory? That is to say, if the zip structure contains directories, can I choose which directory my file goes into? What would the command line look like?

1

Consider your ZIP file looks like this:

foo/
|--- bar/
|    |--- file1.txt
|--- baz/
|    |--- file2.txt

and you would like to add file3.txt to foo/baz/.

Create the directory structure you need, thus here mkdir -p foo/baz/, and copy your file(s) to that location. Then use zip -ur foo.zip foo/ to update your existing archive foo.zip.

That's how I would do it as it involves no unzipping.


Demo

$> # setup
$> mkdir -p foo/bar/ foo/baz/
$> touch foo/bar/file1.txt foo/baz/file2.txt
$> zip -r foo.zip foo/
$> rm -rf foo/
$> zipinfo foo.zip
Archive:  foo.zip
Zip file size: 770 bytes, number of entries: 5
drwxrwxr-x  3.0 unx        0 bx stor 19-Nov-14 17:02 foo/
drwxrwxr-x  3.0 unx        0 bx stor 19-Nov-14 17:06 foo/bar/
-rw-rw-r--  3.0 unx        0 bx stor 19-Nov-14 17:03 foo/bar/file1.txt
drwxrwxr-x  3.0 unx        0 bx stor 19-Nov-14 17:03 foo/baz/
-rw-rw-r--  3.0 unx        0 bx stor 19-Nov-14 17:03 foo/baz/file2.txt
5 files, 0 bytes uncompressed, 0 bytes compressed:  0.0%

$> # solution
$> mkdir -p foo/baz/
$> cp path/to/file3.txt foo/baz/file3.txt
$> zip -ur foo.zip foo/
$> rm -rf foo/

$> # validation
$> zipinfo foo.zip
Archive:  foo.zip
Zip file size: 932 bytes, number of entries: 6
drwxrwxr-x  3.0 unx        0 bx stor 19-Nov-14 17:02 foo/
drwxrwxr-x  3.0 unx        0 bx stor 19-Nov-14 17:06 foo/bar/
-rw-rw-r--  3.0 unx        0 bx stor 19-Nov-14 17:03 foo/bar/file1.txt
drwxrwxr-x  3.0 unx        0 bx stor 19-Nov-14 17:03 foo/baz/
-rw-rw-r--  3.0 unx        0 bx stor 19-Nov-14 17:03 foo/baz/file2.txt
-rw-rw-r--  3.0 unx        0 bx stor 19-Nov-14 17:07 foo/baz/file3.txt
6 files, 0 bytes uncompressed, 0 bytes compressed:  0.0%
  • Thanks, akrinah, and yes, more or less what I have. I create a directory structure to match what I need in the .zip. Doing it in a script involves rather a lot of checking that I'm not damaging anything already there. I hoped there might be a direct way of doing it. – Robert Nov 15 '19 at 17:48
  • Can't you create an empty directory structure inside /tmp/? It wouldn't collide with any existing structure. What potential damages are there? If the directories are empty beside the file you want to update then there should be no damage inside the ZIP to my knowledge. – akrinah Nov 18 '19 at 8:51
  • /tmp/ is new to me. I would have assumed that one needed to be root to write there, but I find not. That would be a good solution, which I shall pursue. As regards damaging existing things - probably being paranoid! – Robert Nov 19 '19 at 15:27
  • Better to be paranoid than losing precious (?) data in the archive. Depending what you store in there. You can create the directory structure anywhere you like. Can be /tmp/ but nothing speaks against ~/mytemp/. Happy hacking :) – akrinah Nov 20 '19 at 9:54
  • @akrinah, does the zip command option to specify the source and destination folders. I was trying zip -r /example/directory example.zip /tmp/testopt/ , it zips my entire /example – Geo Thomas Dec 18 '19 at 4:18

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.