Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create tar-file without folder strucure

I have to compress multiple files to one tar-File. As I have to repeat this for many files in many different folders I wrote a shell script and started it from a parent directory. The shell script contains multiple lines like the following:

tar -cf a/b/c/tarfile.tar a/b/c/*

This creates a tar-file in the specified folder but inside the tar-File the folder structure of a/b/c/ is still existent.

What I want is that all files in c/ are placed in tarfile.tar WITHOUT the folder structure a/b/c/. Is this possible?

like image 839
ala Avatar asked Aug 27 '12 14:08

ala


1 Answers

tar -cf a/b/c/tarfile.tar -C a/b/c . will switch to the directory a/b/c and read in the entire directory (the . - you could specify specific files as well, but a wildcard will not do what you're expecting). The -C <directory> <filelist> pattern can be repeated as necessary to process additional files from different locations.

Another possibility given your original example would be cd a/b/c; tar cf ../../../tarfile.tar *, but that doesn't give you the possibility to pull multiple files from different locations (of course you could still use -C, but the relative paths would have to be adjusted accordingly.

like image 82
twalberg Avatar answered Sep 21 '22 03:09

twalberg