0

If I use the following command:

cd /Users/you/foo 
tar c /Users/you/x/y/z | tar x

it will write /Users/you/x/y/z to the local directory to this path:

/Users/you/foo/Users/you/x/y/z

how can I change one of the tar commands, so the file gets written here instead:

/Users/you/foo/z

man tar on my MacOS machine did not yield any obvious solutions to this one, and I am not sure if this behavior can be changed.

  • Your actual and desired paths are exactly the same. – Patrick May 14 '18 at 21:30
  • ah thanks for the correction, that was a typo, I think I have an answer, the one I posted seems to work – Alexander Mills May 14 '18 at 21:32
  • Can you use pax? It can write tar files (with the -x ustar option), and the -s option can be used to rewrite pathnames. When we needed to make archives that could be read and written on Linux, FreeBSD, Solaris, and Mac OS X, pax was a great, portable solution – Mark Plotnick May 14 '18 at 21:53
  • @MarkPlotnick thanks, I am on MacOS, and have to support other ppl, so if many ppl don't already have pax then prob not easy to incorporate pax. One problem I have is that the -O options seems only support one file being written on. If tar c <file1> <file2> ... <filex> is used then it doesn't seem to work. – Alexander Mills May 14 '18 at 21:55
  • I added the new problem I am seeing as part of my answer to the OP – Alexander Mills May 14 '18 at 21:57
2

What I do when copying stuff around is:

tar c here/is/the/stuff | (cd /where/it/should/go; tar x)

Explanation: The first tar packages to standard output, this is piped to the second command, which is a subshell (the ( starts it) which changes directory and then runs the second tar. This is nice if you want to use e.g. file completion for messy names. Or you could use the -C option to change directory before doing the operation, like:

tar c here/is/the/stuff | tar -C /where/it/should/go -x
  • 1
    Correct, one of the main rules: Thou shalt not use absolute path names in tar archives – schily May 31 '18 at 14:36
0

I think I got this to work using the -O option of tar x which allows you to write the output to stdout, so that looks like:

tar c /Users/you/x/y/z | tar x -O > z.tgz

one problem with this technique is that it doesn't seem to support writing out multiple files:

tar c z1 z2 z3 | tar x -O > z.tgz

it looks like z.tgz will just represent z3...

  • tar x -O makes no sense, and the result definitely won't be a gzipped tarfile. – vonbrand May 17 '18 at 17:10

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.