Spectator
Spectator

Reputation: 79

Compress files in a directory in a zip file with the shell

I want to compress files from the filesystem to a directory within a new zip archive or update an old one. So here is my example:

directory/
  |-file1.ext
  |-file2.ext
  |-file3.ext

in the zip archive it should look like this:

new_directory/
  |-file1.ext
  |-file2.ext
  |-file3.ext

I could copy the files to a new directory and compress them from there but i do not want to have that extra step. I haven't found an answer to that problem on google. The man page doesn't mention anything like that aswell so I hope somebody can help me.

Upvotes: 0

Views: 362

Answers (1)

Wintermute
Wintermute

Reputation: 44023

I don't think the zip utility supports this sort of transformation. A workaround is to use a symbolic link:

ln -s directory new_directory
zip -r foo.zip new_directory
rm new_directory

If other archive formats are an option for you, then this would be a bit easier with a tar archive, since GNU tar has a --transform option taking a sed command that it applies to the file name before adding the file to the archive.

In that case, you could use (for example)

tar czf foo.tar.gz --transform 's:^directory/:new_directory/:' directory

Upvotes: 3

Related Questions