Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pipe tar extract into tar create

I have a tar.gz right now, and I want to extract just a file or two from it, and pack/add those into a new tar.gz, all in one go. Of course I can just save to a temporary file and work with it, but the ABSOLUTE requirement is to do this all without having any intermediate file output, i.e. piping. In other words, what I would like is something like the following pseudo-code (obviously the syntax is incorrect)

tar -xvf first.tar.gz subdir1/file1 subdir2/file2 | tar cf - | gzip > second.tar.gz

Does anyone know the proper syntax for this? I have tried many variants, but to no avail.

I am also very open to the idea of using cpio, but again, I am stumped by how to get the syntax down properly, and from what I understand, cpio intakes only archives or filenames, not files.

Any help will be greatly appreciated.

EDIT: There is no particular filename pattern inside the tarball to extract. Given that the BSD and GNU tar can only search one pattern at a time, I'm not sure if it's even possible to use the include/exclude flags, respectively.

like image 331
user1522407 Avatar asked Jul 13 '12 02:07

user1522407


People also ask

How do you create tar?

To create a tar file, use the cvf command line option, list the name of the resulting tar file first followed by a directory whose contents you want to tar-up. If you forget to list the tar file target (hw10. tar) in the tar command, tar will exit with an error message.

How is tar extracted?

The most common uses of the tar command are to create and extract a tar archive. To extract an archive, use the tar -xf command followed by the archive name, and to create a new one use tar -czf followed by the archive name and the files and directories you want to add to the archive.

How do I extract or untar a tar file?

$ tar xvf /dev/rfd096ds15 The x option tells tar to extract files, v displays them, and f tells tar to use the next argument (/dev/rfd096ds15) as the name of the backup. (If the use of wildcards is especially important, you should use the cpio(C) command instead of tar; see ``Creating a backup with cpio'' for details.)


2 Answers

I am assuming that you are using or that you can get GNU tar.

You can use the --delete option to process one tar file to another. E.g.:

% tar cf x.tar a b c d
% tar tf x.tar
a
b
c
d
% cat x.tar | tar f - --delete b c > y.tar
% tar tf y.tar
a
d
%

Note that you can specify multiple names to delete. Then you just need to figure out how specify all the files to get rid of on the command line, instead of the files to keep.

like image 187
Mark Adler Avatar answered Oct 13 '22 03:10

Mark Adler


If you know the filename pattern that you are going to extract, try this:

tar zcf second.tar.gz --include='filepattern' @first.tar.gz

Here is an example showing the inclusion of multiple files:

% tar cf x.tar a b c d
% tar tf x.tar
a
b
c
d
% cat x.tar | tar cf - --include='a' --include='d' @- > y.tar
% tar tf y.tar
a
d
%
like image 2
fvwmer Avatar answered Oct 13 '22 04:10

fvwmer