GZIP/GUNZIP with no extra storage`

When you gzip (or compress) a file, the original plus the compressed copy will exist in the same filesystem until the compression is complete. Then the old file is removed, leaving the compressed version. The worst case for an uncompressible file is that twice the amount of space will be needed temporarily. For very large files such as a tar file of multiple directories and files will not be compressible in the current filesystem. From the man pages for gzip and compress, you will see that the target file is optional — which means that the program is also a filter and can be used with pipes and redirection. So the resultant gzipped (or gunzipped) file can be in another filesystem. Here is an example:

cat myBigFile | gzip >/var/tmp/mySmallerFile.gz

or

gzip < mtBigFile > /var/tmp/mySmallerFile.gz

Both examples take stdin, compress the stream of data and write the result to stdout. No additional disk storage is required as all conversion takes place in the stream through memory.

Another use of streaming gzip is copying a large directory using tar or fbackup, compressing the result and sending it over the network to another system. Here’s an example:

Breaking down each section, the tar output (-f) is missing which means stdout is the output and that is piped to gzip. From gzipstdout is then piped to ssh (could also be remsh or rexec) where cat is used to read the stdin sent to ssh and stdout becomes the remote file. The remote file is named .tgz signifying a tar file that has been gzipped.

cd /mydata

tar -cv * | gzip | ssh cat > /var/tmp/mydata.tgz

Assuming that the directories and files are compressible, the transfer over the network will be much quicker than with the uncompressed data.

– See more at: http://serviceitdirect.com/blog/gzipgunzip-no-extra-storage#sthash.YjxD6Ldb.dpuf


Tags: