5

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

$ls
1.txt
$ls |xargs gzip && ls 
1.txt.gz

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?

Sinoosh
  • 2,101

1 Answers1

12

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.

Byte Commander
  • 110,243