Linux: Specifying top level-directory when creating zip archive

17

1

I have project with the usual directory structure (src/, bin/, ...), i.e

project-name/
|-- bin
|-- lib
|-- src
`-- Makefile

And would like to create an archive with the following directory structure:

project-name-version/
|-- bin
|-- lib
|-- src
`-- Makefile

Is there a neat way to do this, which avoids creating a temporary directory project-name/ elsewhere, then copying the files inside a finally calling zip -r ... on that temporary directory?

(I am basically looking for some kind of path prefix or relative path option.)

leden

Posted 2012-02-22T00:18:04.027

Reputation: 546

Answers

9

Maybe this already occurred to you, but why not just use a sym link rather than copy everything?

ln -s project-name project-name-version

then use zip -r through the sym link (zip will dereference sym links by default)? When you're done you can simply rm the sym link. Perhaps it's not the most elegant solution, but I don't know an obvious way to do it through zip directly.

FatalError

Posted 2012-02-22T00:18:04.027

Reputation: 1 913

1This also works with tar if you use the -h flag. – Kyle Strand – 2016-09-28T01:18:43.703

(I'm also using the z flag; I'm not sure if that affects it.) – Kyle Strand – 2016-09-28T01:23:32.513

15

This is more an advice than an answer: use Git!

If you setup a Git repository for your project, the whole thing become quite straightforward:

git archive HEAD --prefix=project-name-version/ \
    --format=zip -o project-name-version.zip

cYrus

Posted 2012-02-22T00:18:04.027

Reputation: 18 102

1

Excellent advice, thank you. git archive documentation: http://git-scm.com/docs/git-archive

– Meglio – 2015-07-12T04:53:55.313

1While using version control is a good idea, this answer does not match the question for the generic case. – raimue – 2016-04-20T14:16:56.650

@Raim "This is more an advice than an answer: use Git!" written there since like 4 years, thank you for pointing it out... – cYrus – 2016-04-20T14:24:58.803

1If this was not intended as an answer, it should be converted to a comment. – raimue – 2016-04-20T15:42:27.480

I think that the downside of using git archive is that files like .gitignore go into the created archive, as well. Does anybody know a remedy? – Gregor – 2019-04-08T11:08:57.633

1@Gregor I think that's a desirable behavior as such files are actually part of the project, anyway you can use Git attributes, e.g., create a file named .gitattributes containing .git* export-ignore. – cYrus – 2019-04-08T14:24:14.430

Thank you, @cYrus, good solution. My goal was to make source code publicly available, in which case you do not want those files included. – Gregor – 2019-04-09T23:59:43.803