When i pipe to gzip it can not accept stdin i should using xargsto convert stdin to argument

$ls |xargs gzip && ls 

every thing is ok . but when i want to compress a cpio archive file

$ls | cpio -ov | gzip > archive.cpio.gz

also it is ok and this ls | cpio -ov | xargs gzip does not work.why in the second situation gzip accept stdin and it can not accept argument?


There is a difference between command-line arguments and standard input.

gzip accepts filenames as arguments. It will read the raw data from the specified files and compress them. If you have a command outputting a list of filenames, you can use xargs to pass those filenames to gzip as command-line arguments.

However, if no file arguments are passed to gzip, it defaults to read its raw data from standard input and print the compressed result to standard output. If you have a command outputting raw data which you want to compress, you can pipe it to gzip.

| improve this answer | |
  • 1
    thank you,i read many question about xargs i could not understand it .your mean is xargs only convert a list of file names to arguments. it is not completely true to saying xargs convert stdin to argument? – Sinoosh Sep 29 '16 at 13:17
  • 3
    xargs reads text from its standard input (usually you use a pipe to get another command's output there) and constructs a new command line composed of its own arguments and the text from standard input and executes that new command. It does not care whether the input are file names or anything else, just in your example it makes only sense with file names. – Byte Commander Sep 29 '16 at 13:26
  • sorry i am a newbie in linux,when i pipe cmd1 | cmd2 cmd 2 see a text from its stdin and it can not recognizable for those command that accept file names as argument....in my example entire data (not only file name) write to stdout and it is possible for pipe to gzip without xargs, are these correct? – Sinoosh Sep 29 '16 at 13:49
  • my means is if the output of cmd1 is a list of filenames – Sinoosh Sep 29 '16 at 13:59
  • I'm sorry, you are confusing me. – Byte Commander Sep 29 '16 at 14:11

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.