Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I need to create a zip archive where the unzipped result preserves the directory structure from the specified directory down. I also need to do this without actually switching to the directory.

My question is similar to this. After having read a great deal on the issue, I don't feel that anything has completely answered my question.

For example, I have:


I need to be able to perform zip -r /tmp/sub_dir/ /tmp/sub_dir/pertinent_dir/* such that the result only contains pertinent_dir and below. Again, I need to do this regardless of my current directory context. I've tried -j, but that doesn't seem to work. Perhaps it's not possible. Is there something I'm missing?

share|improve this question

migrated from Apr 25 '12 at 13:21

This question came from our site for professional and enthusiast programmers.

Are you opposed to changing to the target directory within a subshell and doing the zip within that same subshell? – Nick Atoms Apr 25 '12 at 3:03

2 Answers 2

up vote 6 down vote accepted
sh -c "cd /tmp/sub_dir/pertinent_dir/../  \
&& zip -r pertinent_dir/*"
share|improve this answer

You could use jar instead of zip:

jar cfM /tmp/sub_dir/ -C /tmp/sub_dir pertinent_dir
share|improve this answer
You should probably mention that jar is part of the Java installation, and creates compatible zipfiles which can be extracted with unzip and so on. – user unknown Apr 26 '12 at 1:14

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.