How to gzip every file separately ?

find command and pipes ("|") in Unix-like systems are very powerful features. The former retrieves the files matching the specified pattern. The latter one helps to transform the output of the previous operation into the input for the next command. They can be used together to, for instance, compress all matching files separately.

Let's start by creating some files with different extensions:

bartosz:/tmp$ mkdir compress_files && cd compress_files
bartosz:/tmp/compress_files$ touch file1.txt && touch file2.doc && touch file3.csv
bartosz:/tmp/compress_files$ ls
file1.txt  file2.doc  file3.csv
 
bartosz:/tmp/compress_files$ find * -type f -print0 | xargs -0r gzip -k
bartosz:/tmp/compress_files$ ls
file1.txt  file1.txt.gz  file2.doc  file2.doc.gz  file3.csv  file3.csv.gz