Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

cd /tmp && tar -cvf - /etc | tar -xvf -

183 views
Skip to first unread message

hongy...@gmail.com

unread,
Apr 14, 2021, 2:10:47 AM4/14/21
to
I try to understand the following command sequenc:

$ cd /tmp && tar -cvf - /etc | tar -xvf -

But l'm still not so clear on the execution logic of the 2nd and 3rd commands. Any hints will be highly appreciated.

Regards,
HY

Oğuz

unread,
Apr 14, 2021, 3:24:24 AM4/14/21
to
On Wednesday, April 14, 2021 at 9:10:47 AM UTC+3, hongy...@gmail.com wrote:
> I try to understand the following command sequenc:
>
> $ cd /tmp && tar -cvf - /etc | tar -xvf -
`tar -c' creates an archive, `tar -x' extracts its contents. `-f -' means "write to stdout" or "read from stdin" depending on whether `-c' or `-x' is given. So what that command does is much less the same as `cp -R /etc /tmp'.

Janis Papanagnou

unread,
Apr 14, 2021, 5:15:03 AM4/14/21
to
The data transfer might be easier to understand if written as

( cd srcdir && tar cf - . ) | ( cd targetdir && tar xf - )

Note: this is functionally not the same as the above command but
probably makes the intention with the pipe connector more obvious.

Janis

> Regards,
> HY
>

jo...@schily.net

unread,
Apr 14, 2021, 7:25:22 AM4/14/21
to
In article <983fa996-a084-4948...@googlegroups.com>,
hongy...@gmail.com <hongy...@gmail.com> wrote:
>I try to understand the following command sequenc:
>
>$ cd /tmp && tar -cvf - /etc | tar -xvf -

If you are running this command as root using a standard tar implementation,
this destroys the content of the /etc directory.

This is beacause:

1) The first tar archives absulute path names and the second tar
extracts absolute path names.

2) A standard tar extracts files unconditionally, regardless of whether
the current file in the filesystem is newer than the file from
the archive that is going to be extracted.

There are modern tar implementations that avoid this:

Star implements methods since ~35 years to avoid this problem:

star strips off leading slashes while extracting TAR archives unless
the option -P is in use

star by default does only extract files if they are newer than existing
files in the filesystem unless the option -U is used.

Gtar strips off leading slashes while creating archives.

--
EMail:jo...@schily.net Jörg Schilling D-13353 Berlin
Blog: http://schily.blogspot.com/
URL: http://cdrecord.org/private/ http://sourceforge.net/projects/schilytools/files/

William Unruh

unread,
Apr 14, 2021, 1:13:06 PM4/14/21
to
It is an overly complicate way of doing say
rsync -av /etc /tmp
Ie, the first tar creates a tar archive of /etc and send it to stdout
(-f - means create the tar file onto the file stdout.) and the second
tar takes the file stdin and unpacks the tar file. Ie., this is just a
copy but without rsyncs checks cryptographic hash checks that the copy
is an accurate copy.


>
> Regards,
> HY

Helmut Waitzmann

unread,
Apr 14, 2021, 6:44:47 PM4/14/21
to
"hongy...@gmail.com" <hongy...@gmail.com>:
Stop running commands you don't understand!  You'll not only destroy
a saved disk image (as done three days ago) but also ruin your
'/etc' file hierarchy. 

Read the 'tar' manual to understand what those two 'tar' invocations
are doing.  The tutorial, which is part of the GNU tar
documentation, might be a place to start. 

hongy...@gmail.com

unread,
Apr 15, 2021, 9:21:14 AM4/15/21
to
Thank you and all the others here for the insightful notes and explanations.

Regards,
HY

hongy...@gmail.com

unread,
Apr 15, 2021, 9:28:20 AM4/15/21
to
On Thursday, April 15, 2021 at 6:44:47 AM UTC+8, Helmut Waitzmann wrote:
> "hongy...@gmail.com" <hongy...@gmail.com>:
> >I try to understand the following command sequenc:
> >
> >
> >$ cd /tmp && tar -cvf - /etc | tar -xvf -
> >
> >But l'm still not so clear on the execution logic of the 2nd and
> >3rd commands. Any hints will be highly appreciated.

> Stop running commands you don't understand!

Thank you for your very very very correct suggestions. In fact, I will never run any commands that I cannot understand on a production machine.

> You'll not only destroy
> a saved disk image (as done three days ago) but also ruin your
> '/etc' file hierarchy.
>
> Read the 'tar' manual to understand what those two 'tar' invocations
> are doing. The tutorial, which is part of the GNU tar
> documentation, might be a place to start.

I've tried to pin out the pertinent hints/clues from the manual first, but lost in the manual of massive information.

Regards,
HY

Helmut Waitzmann

unread,
Apr 15, 2021, 4:38:22 PM4/15/21
to
"hongy...@gmail.com" <hongy...@gmail.com>:

>I've tried to pin out the pertinent hints/clues from the manual
>first, but lost in the manual of massive information.

Depending on the version of 'tar', the shell command line


cd /tmp && tar -cvf - /etc

will create an archive containing all files and directories of the
'/etc' hierarchy, e. g.  the names '/etc', '/etc/passwd',
'/etc/services'…

Now, if you feed that archive (by means of the pipe) to the
extracting 'tar' command

tar -xvf -

that extracting tar command will extract its contents to all files
and directories as named in the archive, that is the '/etc'
hierarchy, e. g.  the names '/etc', '/etc/passwd', '/etc/services'
and so on. 

=> All files will be written over itself. 


The problem is, that, with the pipe, the second 'tar' starts running
before the first 'tar' has finished its job writing the archive. 
This might cause a file to be opened for writing before it has been
fully read. 

As the second 'tar' opens the files for writing, specifying the
'O_TRUNC' 'open(2)' flag, it will truncate the file before it has
been fully read by the first 'tar'. 

To get that job right, you could do


( cd / && tar -cvf - ./etc ) |
( cd /tmp && tar -xvf - . )

The first 'tar' command would read the directory './etc' (which
effectively is the same as '/etc' because the working directory is
'/') and the files './etc/passwd', './etc/services' and so on. 

Now, the archive member's names are './etc', './etc/passwd',
'./etc/services' and so on rather than '/etc', '/etc/passwd',
'/etc/services' and so on. 

=> The second 'tar' would extract the archive to the directory
'./etc' and the files './etc/passwd', './etc/services' and so on. 

But, because the working directory of the second command is '/tmp/'
rather than '/', the contents of the archive will be extracted to
'/tmp/./etc', '/tmp/./etc/passwd', '/tmp/./etc/services' and so on,
which are the same as '/tmp/etc', '/tmp/etc/passwd',
'/tmp/etc/services'. 

hongy...@gmail.com

unread,
Apr 15, 2021, 7:06:05 PM4/15/21
to
Thank you very much for your in-depth explanation.

HY
0 new messages