Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a project with the usual stuff (source code, yadda, yadda) and I want to have a command to Scons (but it could be whatever build system to be honest) that will do the following:

  1. Build the system -- this is easy.
  2. Create a tar ball of the system -- this is easy.
  3. When the content of the tar ball are extracted, they extract in a fubar-VERSION/ directory -- this is the tricky part.

I can do the latter manually by creating a symlink, then taring from one directory above the project one but that feel inelegant. Any idea?

To clarify, I want to be able to do this:

; pwd
; scons release
; cp project-X.Y.Z.tbz /tmp
; cd /tmp
; tar jxvf project-X.Y.Z.tbz

Thus the archives extracts in a directory named after the project with the right version number.

share|improve this question
Shouldn't this be configured in your build system? – rahmu Mar 1 '12 at 11:44
In an ideal world yes... But the question is more to do with tar rather then the build system/release mechanism. – Sardathrion Mar 1 '12 at 12:13
up vote 5 down vote accepted

With GNU tar:

tar cjf ../fubar-4.2.tar.bz2 --transform='s/^\./fubar-4.2/' --exclude='*.o' .

Traditional tar implementations have no way to do this. Even FreeBSD tar has no similar feature that I know of.

With pax, the POSIX replacement for cpio and tar:

pax -w -s '/^\./fubar-4.2/' -s '/\.o$//' . | bzip2 >../fubar-4.2.tar.bz2

Although pax is mandatory in current POSIX/Single Unix standards, many Linux distributions don't install it by default. If you support both GNU tar and pax, you'll cover almost all non-embedded unices out there.

share|improve this answer

At least bsdtar 2.8.4 and up has the -s pattern option to modify file or archive member names according to the given basic regex pattern.

# using '^\.' as pattern
bsdtar -czvf test.tar.gz .
bsdtar -tzvf test.tar.gz
bsdtar -s '|^\.|archive-X.Y.Z|' -xzvf test.tar.gz

# using '^[^/]*' as pattern
bsdtar -C .. -czvf test.tar.gz "$(basename "$(pwd -P)")"
bsdtar -tzvf test.tar.gz
bsdtar -s '|^[^/]*|archive-X.Y.Z|' -xzvf test.tar.gz
share|improve this answer

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.