I would like to flatten and archive the following structure:

├── a
│   └── a.txt
├── ...
│   └── ...
└── z
    └── z.txt

in this tar archive:


I could, for example,

  1. flatten the structure in some temporary location with cp and then archive
  2. create the archive iteratively (tar option --append) and use --directory each time
  3. flatten the structure at extraction time with --strip-components

Any other smart idea?

2 Answers 2


Just found out about --transform:

 File name transformations:

      --strip-components=NUMBER   strip NUMBER leading components from file
                             names on extraction
      --transform=EXPRESSION, --xform=EXPRESSION
                             use sed replace EXPRESSION to transform file


tar -caf archive.tar.xz --transform 's|.*/\(.*\)|\1|g' $(find . -type f)
  • 3
    Better as find . -type f -print0 | tar --null -T - --transform='s|.*/||' -caf archive.tar.xz. That $(find . -type f) only works correctly if file paths don't contain characters of IFS nor wildcards nor backslash and the list is not too big. Note that you need the GNU implementation of tar for --transform (and that -a). Aug 6 at 14:56

I'd untar everything into a directory, and use rsync to put everything into one directory, and tar it up again.

You must log in to answer this question.

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