Gzip Every File Separately

GZip every file separately

You can use gzip *


Note:

  • This will zip each file individually and DELETE the original.
  • Use -k (--keep) option to keep the original files.
  • This may not work if you have a huge number of files due to limits of the shell
  • To run gzip in parallel see @MarkSetchell's answer below.

How to gzip each file separately inside a folder and then if successful delete the original file

I don't understand the issue you're having. According to the gzip man page:

Whenever possible, each file is replaced by one with the extension .gz, while keeping the same ownership modes, access and modification times.

My only guess is you're having permissions issues. Make sure you're logged in as the owner of the file that you are gzipping. However, in the gzip version on my machine, I get an obvious Operation not permitted warning when I try to gzip a file that I don't own. The operation still succeeds, but it's clear what is going on.

Compress multiple files individually with Gzip


gzip */*.txt

But the extension for each file will be .txt.gz, as gzip uses it to know the original filename.

How to gzip all files in all sub-directories in bash

No need for loops or anything more than find and gzip:

find . -type f ! -name '*.gz' -exec gzip "{}" \;

This finds all regular files in and below the current directory whose names don't end with the .gz extension (that is, all files that are not already compressed). It invokes gzip on each file individually.


Edit, based on comment from user unknown:

The curly braces ({}) are replaced with the filename, which is passed directly, as a single word, to the command following -exec as you can see here:

$ touch foo
$ touch "bar baz"
$ touch xyzzy
$ find . -exec echo {} \;

./foo
./bar baz
./xyzzy

Gzip multiple files individually and keep the original files

Your > in the last command gets parsed by the same shell which runs find. Use a nested shell:

find . -type f -name "*cache.html" -exec sh -c "gzip < {} > {}.gz" \;

How to gzip all files in all sub-directories into one compressed file in bash


tar -zcvf compressFileName.tar.gz folderToCompress

everything in folderToCompress will go to compressFileName

Edit: After review and comments I realized that people may get confused with compressFileName without an extension. If you want you can use .tar.gz extension(as suggested) with the compressFileName

Gzip with all cores

If you are on Linux, you can use GNU's xargs to launch as many processes as you have cores.

CORES=$(grep -c '^processor' /proc/cpuinfo)
find /source -type f -print0 | xargs -0 -n 1 -P $CORES gzip -9
  • find -print0 / xargs -0 protects you from whitespace in filenames
  • xargs -n 1 means one gzip process per file
  • xargs -P specifies the number of jobs
  • gzip -9 means maximum compression


Related Topics



Leave a reply



Submit