I saw sometime 2 ways of writting: with and without -c

Do the 2 following case output the same and have equal speed ?

mysqldump -u root dbname | gzip > test.sql
mysqldump -u root dbname | gzip -c > test.sql

What about continous pipe?

mysqldump -u root dbname | gzip | anotherprogram

Thanks for clarify


According to man gzip:
"If no files are specified, or if a file name is "-", the standard input is compressed to the standard output."

In all your cases you piping source stream to gzip's STDIN without specifying content to compress as a source file. In these cases gzip sends compressed stream to STDOUT by default that makes -c option useless/redundant.

The -c option is useful when you calling gzip as gzip -c sourceFile so in this case instead of
compressing sourceFIle+adding extension GZ+deleting sourceFile
it will send compressed stream to STDOUT and won't delete sourceFile.
For example:

gzip -c sourceFile | anotherProgram
  • Ty for clarify! – Thanh Trung Aug 21 '18 at 12:35
  • Also, I suggest, "speed" is not really a primary-concern here: you simply want to find something that works! There is no appreciable "overhead" associated with any of these possible ways to do it. Also note that the running time of any alternative will vary naturally from one run to the next, perhaps giving the impression that "one or the other one is 'faster,'" when in fact this is not so. – Mike Robinson Aug 21 '18 at 14:42
  • I don't think gzip decides the output mode based on > redirect, but rather if the input files are a) specified as CLI parameters or b) piped in using |. So the -c would be mandatory in this case gzip -c file1 file2 > foo.gz, but not for this cat file1 file2 | gzip > foo.gz. – Vlastimil Ovčáčík Aug 21 '18 at 15:10
  • @VlastimilOvčáčík I didn't said "gzip decides the output mode based on > redirect", but I used phrase that ended with: "in your use case". -c isn't mandatory if one want to compress file1 and file2 (gzip file1 file2) separately even it's CLI. According to man gzip all -c is doing is: "Write output on standard output; keep original files unchanged." – Alex Aug 21 '18 at 15:49
  • 1
    What I was trying to say is that it does not matter if there is redirect or pipe behind the gzip command. What matters is that there is pipe before it. I was not talking about the trailing | but about the leading one. I probably didn't make this clear, but if you reread my comments with this in mind it should make sense. Otherwise I am sorry for bothering you. – Vlastimil Ovčáčík Aug 21 '18 at 17:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.