Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create tar into stdout on Ubunutu 16

tar cannot write to standard output on Ubuntu 16:

prod ~    $ cat /etc/os-release  | grep -i version
VERSION="16.04.2 LTS (Xenial Xerus)"
VERSION_ID="16.04"
VERSION_CODENAME=xenial

prod ~    $ tar -cf - tmp
tar: Refusing to write archive contents to terminal (missing -f option?)
tar: Error is not recoverable: exiting now

Let's try on CentOS7:

[root@drft068 ~]# tar -cf - /tmp
tar: Removing leading `/' from member names
tar: /tmp/mongodb-27018.sock: socket ignored
tar: /tmp/mongodb-27017.sock: socket ignored
tmp/00017770000000000000000000000000131574472010

What do I do wrong?

like image 528
rlib Avatar asked Sep 17 '17 11:09

rlib


1 Answers

To have a proper SO answer here, I condensed @Kamajii and @Cyrus comments to the questions (please consider doing that yourselves ...).

tar notices that stdout will end up on a terminal. Besides the output is binary and thus not really readable for humans it is also a security risk.

As soon as you redirect stdout of the tar process, it will work. One example given involves cat, which doesnt care if you give it a binary blob. Thus

tar -cf - tmp | cat

will display the binary stuff. Otherwise tar -cf - tmp > mytmp.tar is probably closer than what you want to use (although for this example a tar -cf mytmp.tar tmp would have been the typical call).

like image 91
Felix Avatar answered Nov 03 '22 03:11

Felix