Skip to content

Instantly share code, notes, and snippets.

@rcanepa
Last active August 12, 2016 16:28
Show Gist options
  • Save rcanepa/f60ee5e8b41b2420d4092ab41d7eadbc to your computer and use it in GitHub Desktop.
Save rcanepa/f60ee5e8b41b2420d4092ab41d7eadbc to your computer and use it in GitHub Desktop.
UNIX tarpipe, gzip
```
$ (cd src && tar -c .) | (cd dst && tar -xp)
1^ 2^ 3^
```
1^ this fork a new process that execute those commands and use the writing end of the pipe as its STDOUT
2^ the pipe has two ends (to write and read), and both of them have a fd
3^ this fork a new process that execute those commands using the reading end of the pipe as its STDIN
Also, using && instead of ; to separate commands means that if the first one fails, the second one won't be executed.
On the other side, with ; both commands would be executed regardless of their result. So, in this case, we don't want
to execute tar -c inside another directory but src. (We don't want to compress or extract the wrong directory.)
$ tar -c foo.py
foo.py000644 000765 000024 00000000466 12753374164 013011 0ustar00rcanepastaff000000 000000 from os import fork, getpid, pipe, read, write
read_end, write_end = pipe()
child_pid = fork()
if child_pid:
print "parent is about to read"
data = read(read_end, 1000)
print "parent read"
print "data:", data
else:
print "Child is about to write"
write(write_end, "hi dad")
print "Child wrote"
$ tar -c foo.py | wc -l
14
$ tar -c foo.py | wc -c
10240
$ tar -c foo.py | gzip | wc -c
295
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment