Add stdin as another file to the tar archive

I am trying to add a file to the tar archive from the output file of a program without first generating it on disk. Think of the VERSION file that is autogenerated when you run the build script. I tried this, but a dereferenced symlink is just a named pipe, not a regular file:

 $ date +%s | \ tar cf test.tar \ --dereference \ --transform="s#/proc/self/fd/0#VERSION#" \ /proc/self/fd/0 \ other_files \ and_folders/ 

The result should be a VERSION file with a timestamp inside the tar archive without creating the file locally:

 $ tar tf test.tar VERSION other_files and_folders 
+5
source share
2 answers

I do not know how to create a “fake” file in the JAR archive. I would create a normal file, add it to the archive and then delete it later.

You might want to try the -A (or --concatenate ) --concatenate . This will allow you to create the file in /tmp , add it to the TAR archive, and then add the rest of the files in the second step. This way you can create arbitrary paths for the VERSION file.

+1
source

Although it is true that tar works with files, this does not mean that you need to create temporary files yourself, and I would advise him.

You can use process substitution for this (Bash supports what Fish does in slightly different syntax too).

 tar cvaf a.tgz <(echo 1.0.0) file1 file2 -P --transform 's:^/.*:VERSION:' 

-P ( --absolute-names ) here to identify a temporary file to convert the name to rename. Hope other files are local.

If this is not the case, and some other files are added in absolute ways (which is probably not a common case), then you can either clarify the regexp conversion, or use a two-step solution with creating the archive, and then update it (as Aaron pointed out).

+1
source

Source: https://habr.com/ru/post/1207598/


All Articles