Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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'm .taring some files with the path example/super_user/Output.*.

The resulting .tar looks like this:

+ example
    + super_user
          - Output.xml
          - Output.txt

But I want the file to be like the following:

- Output.xml
- Output.txt

Do you know how I can achieve this while still being in another directory?

share|improve this question
Do make sure to avoid tarbombs with archives like that. – grawity Sep 30 '14 at 12:36
up vote 7 down vote accepted

tar will preserve the file and folder structure so I don't think there's any way to instruct tar to flatten the hierarchy at creation time.

One workaround is to temporarily change directory, create the tar, then go back - a quick example below:

cd example/super_user && tar -cvf ../../result.tar Output.* && cd ../..
share|improve this answer

If the directory 'example' is at the root of the filesystem here's another way:

tar -C /example/super_user -cvf result.tar .

this will change directory to your the point that you want to do the tar. The caveat is that if there are any subdirectories under /example/super_user, the directory structure will be will be preserved for these sub-directories.

share|improve this answer

To create a tar (ARCHIVE.tar) with all files from a directory (DIR), without any parent directory information (not even ./), you can use something like:

find "DIR" -type f -printf "%f\n" | xargs tar cf ARCHIVE.tar -C "DIR"

You can play with the find to limit depth, select specific files, and many other things.

Good Luck!

share|improve this answer
I'm getting "find: -printf: unknown primary or operator" at OSX find command. Any tips? – TCB13 Feb 3 '13 at 15:55
-printf is a non standard command. Many of those only work with specific versions of find. Usually we mark those when answering a question on Super User (e.g. with "-blah is a Gnu extension and will not work everywhere"). – Hennes Sep 19 '14 at 11:36

I've posted my answer here:

repost (for lazy ppl)

This is ugly... but it works...

I had this same problem but with multiple folders, I just wanted to flat every files out. You can use the option "transform" to pass a sed expression and... it works as expected.

this is the expression:

's/.*\///g' (delete everything before '/')

This is the final command:

tar --transform 's/.*\///g' -zcvf tarballName.tgz */*/*.info

share|improve this answer

I created a temp directory. And in the directory, created symbolic links to all of the files to the files to be included. Then I did tar -h -C . so that all the files (not links, but their content) are included in the archive with the desired name.

share|improve this answer

If those are the entire contents of the tarball then you can use GNU tar's --strip-components option to remove the 2 levels before the files.

share|improve this answer
I've only got the option --strip-path but using it won't change anything. – user36938 Apr 18 '11 at 7:12
pushd example/super_user
tar -cf output.tar Output.*

pushd pushes the current directory path to the DIR stack and moves to content folder. Then, you move back to original directory by using popd.

share|improve this answer

Your Answer


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