.tar files without directory structure

68

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

Solution 4

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!

Solution 5

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:
68

Related videos on Youtube

James
Author by

James

Updated on September 18, 2022

Comments

  • James
    James over 1 year

    I'm trying to create a compound index with a single Number field and a list of Strings field. When I view the status of the index it just has an exclamation mark with no explanation. I assume it is because datastore concludes that it is an exploding index based on this FAQ page: https://cloud.google.com/appengine/articles/index_building#FAQs.

    Is there any way to confirm what the actual failure reason is? Is it possible to split the list field into multiple fields based on some size limit and create multiple indexes for each chunk?

    • Admin
      Admin over 9 years
      Do make sure to avoid tarbombs with archives like that.
    • Admin
      Admin over 4 years
      tar --strip-components=1000 ?
  • TCB13
    TCB13 over 11 years
    I'm getting "find: -printf: unknown primary or operator" at OSX find command. Any tips?
  • joki
    joki over 7 years
    This is the answer to the problem at hand. The mentioned caveat is not a problem in this case. All other answers are workarounds to account for variations to the problem.
  • Brice
    Brice over 5 years
    That's indeed the solution to this question. The directory doesn't have to be the root, supposing the directory structure /home/u/foo/bar, working dir is /home/b, then tar -C foo/bar -cvf qiz.tar . works well. One caveat though, is that you can't use wildcards, i.e. tar -C foo/bar -cvf qiz.tar *.log, tar -C foo/bar -cvf qiz.tar "*.log" or tar -C foo/bar -cvf qiz.tar "foo/bar/*.log" won't work.
  • zero298
    zero298 about 5 years
    It should be noted that you can use -C multiple times in a single tar command if you need to add multiple files spread over multiple directories but still want the tar file to be flat.
  • rags
    rags almost 3 years
    This works when a single level stripping is required across all files.