gzip all files without deleting them

find . -type f | \
while read -r x
  gzip -c "$x" > "$x.gz"

The -c pushes the result to stdout and keeps the original alone. The disadvantage is, that you need to find the files yourself. For more sophisticated traversing, you can use find(1), however, like above: . searches starting from the current directory, and -type f returns the name of every regular file.

find . -type f -not \( -name '*.gz' -or -name '*[~#]' \) -exec sh -c 'gzip -c "{}" > "{}.gz"' \;

You could easily switch it around to include what you want to compress ( -name '*.txt -or -name '*.html etc.) instead of like now, excluding some files (already compressed, backup and temporary files).

Handles spaces in the filename just fine too.

Change gzip to echo gzip for testing. Or skip the -exec part all together.

Edit: Oh, I forgot to mention that this doesn't check if <target>.gz already exists. This may or may not be a problem.

Edit2: Ok, here we go with something that checks for existing file. If that may be wanted. Pardon the oneliney-ness.

while read file; do if [ ! -f "$file.gz" ]; then echo "Compressing $file"; gzip -c "$file" > "$file.gz"; else echo "Not overwriting $file.gz";  fi  done < <(find . -type f -not \( -name '*.gz' -or -name '*[~#]' \))

My find-foo is maybe not what it could be, it may very well be possible to skip directly in find.

gzip -k option

gzip 1.6 (June 2013) added the -k, --keep option, so now you can:

gzip -kr .

and it won't delete your original files.

Found at: https://unix.stackexchange.com/questions/46786/how-to-tell-gzip-to-keep-original-file