Gzip Compression Commands
Gzip is a widely used command-line utility for file compression and decompression. It is a standard tool on Unix-like operating systems and is essential for reducing file sizes, saving disk space, and speeding up data transfer.
Compressing Files with Gzip
To create a .gz compressed file from an existing file, you can use the basic gzip command followed by the filename.
gzip test.txt
This command will replace test.txt with test.txt.gz.
Compressing to a Specific Location
If you need to compress a file and redirect the output to a different name or location, use the -c option. This option writes the compressed output to standard output, which can then be redirected.
gzip -c test.txt > test_custom.txt.gz
This command compresses test.txt and saves the output as test_custom.txt.gz, keeping the original test.txt file intact.
Decompressing Gzip Files
To uncompress a .gz file, use the -d option with the gzip command.
gzip -d test.txt.gz
This will decompress test.txt.gz back to its original form, test.txt.
Displaying Compression Ratio
You can view the compression ratio and other details of a compressed file using the -l option.
gzip -l *.gz
This command lists details for all .gz files in the current directory, showing the original size, compressed size, and compression ratio.
Recursive Compression of Directories
Gzip can also be used to recursively compress all files within a specified directory. The -r option enables this functionality.
gzip -r documents_directory
This command will compress each file inside documents_directory, creating .gz versions of them.
Compressing While Keeping the Original File
If you wish to create a compressed file without deleting the original, you can use input and output redirection.
gzip < test.txt > test.txt.gz
This method reads from test.txt and writes the compressed output to test.txt.gz, leaving test.txt untouched.
