Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add stdin as another file to a tar archive

I am trying to add a file to a tar archive from a program output without generating it on disk first. Think of a file VERSION, that is autogenerated, when the build script runs. I tried this but the dereferenced symlink is only 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 file VERSION with a timestamp inside the tar archive w/o locally generating a file:

$ tar tf test.tar
VERSION
other_files
and_folders
like image 368
Boldewyn Avatar asked Nov 24 '14 14:11

Boldewyn


2 Answers

I don't know a way to create a "fake" file entry in a JAR archive. I'd create a normal file, add it to the archive and then delete it afterwards.

You may want to try the option -A (or --concatenate), though. That would allow you to create the file in /tmp, add it to the TAR archive and then append the rest of the files in a second step. That way, you can create arbitrary paths for the VERSION file.

like image 167
Aaron Digulla Avatar answered Oct 04 '22 20:10

Aaron Digulla


While it's true that tar works on files, that doesn't mean you have to create temporary files by yourself, and I would advice against it.

You can use process substitution for that (Bash supports that, Fish does as well in a slightly different syntax).

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

-P (--absolute-names) is here to identify the temporary file for name transformation to rename it. Hopefully other files are local.

If that's not the case and some other files are being added by absolute paths (which is probably not the common case), then you can either clarify the transformation regexp or use two-step solution with creating the archive first and then updating it (as Aaron pointed out).

like image 20
eush77 Avatar answered Oct 04 '22 19:10

eush77