.tar files without directory structure

34

4

I'm .taring some files with the path example/super_user/Output.*.

The resulting .tar looks like this:

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

But I want the file to be like the following:

- Output.zip
- Output.xml
- Output.txt

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

user36938

Posted 2011-04-18T06:13:23.390

Reputation:

Do make sure to avoid tarbombs with archives like that.

– user1686 – 2014-09-30T12:36:52.497

tar --strip-components=1000 ? – Zaar Hai – 2019-09-02T06:40:07.607

Answers

11

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

Gaff

Posted 2011-04-18T06:13:23.390

Reputation: 16 863

29

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.

Anthony Walters

Posted 2011-04-18T06:13:23.390

Reputation: 291

2This 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. – joki – 2016-11-30T10:05:01.933

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. – Brice – 2018-09-13T09:26:57.740

1It 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. – zero298 – 2019-04-25T13:54:22.350

10

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

Alessio Valentini

Posted 2011-04-18T06:13:23.390

Reputation: 201

7

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!

Paul Bhullar

Posted 2011-04-18T06:13:23.390

Reputation: 71

-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 [SU] (e.g. with "-blah is a Gnu extension and will not work everywhere"). – Hennes – 2014-09-19T11:36:41.097

I'm getting "find: -printf: unknown primary or operator" at OSX find command. Any tips? – TCB13 – 2013-02-03T15:55:00.520

3

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.

mzwt

Posted 2011-04-18T06:13:23.390

Reputation: 31

1

Another way to temporary change directory is to put the cd and tar commands inside parenthesis ( ):

(cd example/super_user; tar -cvf ../../result.tar *)

The advantage of this, is, you will always implicitly pop back to the original directory when the block is done. i.e. no need for pushd .. popd blocks or keeping track of where to cd back to.

Stephen Quan

Posted 2011-04-18T06:13:23.390

Reputation: 226

1

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.

Ignacio Vazquez-Abrams

Posted 2011-04-18T06:13:23.390

Reputation: 100 516

I've only got the option --strip-path but using it won't change anything. – None – 2011-04-18T07:12:26.593

0

pushd example/super_user
tar -cf output.tar Output.*
popd

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.

Saravana Kumar

Posted 2011-04-18T06:13:23.390

Reputation: 1