0

When I want to redirect any verbose output I usually do the following,

[arif@arif test]$ rsync -v /home/arif/storage . 1> stdout
[arif@arif test]$ cat stdout 
storage

sent 177 bytes  received 35 bytes  424.00 bytes/sec
total size is 88  speedup is 0.42

But when I do the same thing with gzip it creates different result,

[arif@arif test]$ gzip -v something 1> stdout 2> stderr
[arif@arif test]$ cat stdout 
[arif@arif test]$ cat stderr 
something:    0.0% -- replaced with something.gz

Why verbose output of gzip goes to stderr instead of stdout? This is very problematic for scripting as at the end of the script, an error file is checked and if anything found on the file, a cleanup process is done.

Question is ,

  • Why verbose output of gzip goes to stderr instead of stdout?
  • How can I overcome this?
don_crissti
  • 82,805
arif
  • 1,459

1 Answers1

1

It's actually better for gzip to print the diagnostic output to stderr, because otherwise you wouldn't be able to distinguish diagnostics from data (in the case of -c).

Each program is free to send its messages wherever it likes; gzip sends them to stderr

1060     /* Display statistics */
1061     if(verbose) {
1062         if (test) {
1063             fprintf(stderr, " OK");
1064         } else if (decompress) {
1065             display_ratio(bytes_out-(bytes_in-header_bytes), bytes_out,stderr);
1066         } else {
1067             display_ratio(bytes_in-(bytes_out-header_bytes), bytes_in, stderr);
1068         }
1069         if (!test && !to_stdout)
1070           fprintf(stderr, " -- %s %s", keep ? "created" : "replaced with",
1071                   ofname);
1072         fprintf(stderr, "\n");
1073     }

The workaround would be to deal with each utility on its own terms and expect the specific gzip message on stderr, or omit the -v option.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255