0

I am trying to understand how the redirection is exactly working in this command

# tar -czf - ./Downloads/ | (pv -p --timer --rate --bytes > backup.tgz)

What is the english translation ?

All the data of tar is redirected as input to pv and then pv redirects it to backup.tgz ?

Then why is the bracket around pv required ?

Then how does this redirection make sense ?

tar -czf - ./Documents/ | (pv -n > backup.tgz) 2>&1 | dialog --gauge "Progress" 10 70

After pv what is the meaning of 2>&1 ?

ng.newbie
  • 1,175

1 Answers1

4

See What are the shell's control and redirection operators? and Order of redirections for background.

tar -czf - ./Downloads/ | (pv -p --timer --rate --bytes > backup.tgz)

tells the shell to run tar -czf - ./Downloads/ with standard output redirected to (pv -p --timer --rate --bytes > backup.tgz).

(pv -p --timer --rate --bytes > backup.tgz)

tells the shell to run pv -p --timer --rate --bytes with standard input connected to the pipe from tar, and standard output redirected to backup.tgz. The overall effect, since tar is told to create a compressed archive, outputting to its standard output (f -), is to build a compressed archive, pipe it through pv, and then have pv write it to backup.tgz. pv displays a progress bar, updated as data flows through it.

The parentheses aren’t required.

You second command changes the second half of the first pipe:

(pv -n > backup.tgz) 2>&1

again writes to backup.tgz, but also redirects the subshell’s (introduced by the parentheses) standard error to standard output, and feeds that into dialog which produces its own progress display.

The parentheses are required here if the redirections are set up in the order given in your command: pv -n > backup.tgz 2>&1 would redirect both pv’s standard output and standard error to backup.tgz, which isn’t what you want. The desired effect can also be achieved by redirecting standard error first: pv -n 2>&1 > backup.tgz.

Stephen Kitt
  • 434,908