Make tar from /dev/stdin file

devicesstdintar

What I'm looking for is a way to pass in arbitrary data through STDIN, and have it get tar'd up as if its a normal file.

I tried this

$ echo test | tar -cf test.tar /dev/stdin

and that gives me a test.tar file with the contents dev/stdin. The stdin file is a link to /proc/self/fd/0.

What I want instead is for dev/stdin inside the TAR file to be a regular file with the text test inside. A similar thing happens with:

$ tar -cf test.tar <(echo test)

but the names are different.

Is this doable?

Best Answer

I don't think you can do what you want here. The problem with your approach is that tar deals in files and directory trees, which you're not providing it with commands such as this:

$ echo test | tar -cf test.tar /dev/sdtin

Even when you attempt to "wrap" your strings in temporary files using subshells such as this:

$ tar -cf test.tar <(echo test)

You can see your content is still being TAR'ed up using these temporary file descriptors:

$ tar tvf test.tar
lr-x------ vagrant/vagrant   0 2018-08-04 23:52 dev/fd/63 -> pipe:[102734]

If you're intent is just to compress strings, you need to get them into a file context. So you'd need to do something like this:

$ echo "test" > somefile && tar -cf /tmp/test.tar somefile

You can see the file's present inside of the TAR file:

$ tar tvf /tmp/test.tar
-rw-rw-r-- vagrant/vagrant   5 2018-08-05 00:00 somefile

Replicating data using tar

Most that have been working with Unix for several years will likely have seen this pattern:

$ (cd /; tar cf - .)|(cd /mnt/newroot; tar pxvf -)

I used to use this all the time to replicate data from one location to another. You can use this over SSH as well. Other methods are discussed in this U&L Q&A titled: clone root directory tree using busybox.

Backing up /

There's also this method if you're intent is to back up the entier HDD:

$ sudo tar -cvpzf backup.tar.gz --exclude=/backup.tar.gz --one-file-system /

References

Related Question