Here is the current command I'm running:

tar --directory=/Users/joe/images -czvf images.tgz dir1/image1.jpg dir2/image2.jpg

As you can see, this is using /Users/joe/images as the root directory, and then including images from different directories under that root.

However, I need to be able to include those images under a common parent inside of the tar, such that if I were to then open that tar file, I would see this directory structure:

  • common/image1.jpg
  • common/image2.jpg

Is there a flag or way to execute this command that allows me to specify that "parent" directory that files should go under?

  • If all the images are just one level deep, try --strip-components=1.
    – muru
    May 12, 2015 at 20:04

1 Answer 1


Your --directory suggests you're using either GNU or modern BSD tar.

With GNU tar:

tar --directory=/Users/joe/images --transform='s|[^/]*|common|' \
  -czvf images.tgz dir1/image1.jpg dir2/image2.jpg

With BSD tar:

tar --directory=/Users/joe/images -s'|[^/]*|common|' \
  -czvf images.tgz dir1/image1.jpg dir2/image2.jpg

The idea being the same: replace the leading path component with common.

Note that that substitution also applies to symlink targets in there (which you can disable with the S flag to the substitution).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .