.tar files without directory structure

Solution 1:

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 ../..

Solution 2:

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 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 preserved for these sub-directories.

Solution 3:

I've posted my answer here:

https://stackoverflow.com/questions/13924856/unix-tar-do-not-preserve-directory-structure

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