Archiving and Compression
If you want to use gzip on several files in a directory, just use a wildcard. You might not end up gzipping everything you think you will, however, as this example shows.
$ ls -F bible/ moby-dick.txt paradise_lost.txt $ ls -l * -rw-r--r-- scott scott 1236574 moby-dick.txt -rw-r--r-- scott scott 508925 paradise_lost.txt bible: -rw-r--r-- scott scott 207254 genesis.txt -rw-r--r-- scott scott 102519 job.txt $ gzip * gzip: bible is a directory -- ignored $ ls -l * -rw-r--r-- scott scott 489609 moby-dick.txt.gz -rw-r--r-- scott scott 224425 paradise_lost.txt.gz bible: -rw-r--r-- scott scott 207254 genesis.txt -rw-r--r-- scott scott 102519 job.txt
Notice that the wildcard didn't do anything for the files inside the bible directory because gzip by default doesn't walk down into subdirectories. To get that behavior, you need to use the -r (or --recursive) option along with your wildcard.
$ ls -F bible/ moby-dick.txt paradise_lost.txt $ ls -l * -rw-r--r-- scott scott 1236574 moby-dick.txt -rw-r--r-- scott scott 508925 paradise_lost.txt bible: -rw-r--r-- scott scott 207254 genesis.txt -rw-r--r-- scott scott 102519 job.txt $ gzip -r * $ ls -l * -rw-r--r-- scott scott 489609 moby-dick.txt.gz -rw-r--r-- scott scott 224425 paradise_lost.txt.gz bible: -rw-r--r-- scott scott 62114 genesis.txt.gz -rw-r--r-- scott scott 35984 job.txt.gz
This time, every file — even those in subdirectories — was gzipped. However, note that each file is individually gzipped. The gzip command cannot combine all the files into one big file, like you can with the zip command. To do that, you need to incorporate tar, as you'll see in "Archive and Compress Files with tar and gzip."
Just as with zip, it's possible to adjust the level of compression that gzip uses when it does its job. The gzip command uses a scale from 0 to 9, in which 0 means "no compression at all" (which is like tar, as you'll see later), 1 means "do the job quickly, but don't bother compressing very much," and 9 means "compress the heck out of the files, and I don't mind waiting a bit longer to get the job done." The default is 6, but modern computers are fast enough that it's probably just fine to use 9 all the time.
$ ls -l -rw-r--r-- scott scott 1236574 moby-dick.txt $ gzip -c -1 moby-dick.txt > moby-dick.txt.gz $ ls -l -rw-r--r-- scott scott 1236574 moby-dick.txt -rw-r--r-- scott scott 571005 moby-dick.txt.gz $ gzip -c -9 moby-dick.txt > moby-dick.txt.gz $ ls -l -rw-r--r-- scott scott 1236574 moby-dick.txt -rw-r--r-- scott scott 487585 moby-dick.txt.gz
Remember to use the -c option and pipe the output into the actual .gz file due to the way gzip works, as discussed in "Archive and Compress Files Using gzip."
Note - If you want to be clever, define an alias in your .bashrc file that looks like this:
alias gzip='gzip -9'
That way, you'll always use -9 and won't have to think about it.
Getting files out of a gzipped archive is easy with the gunzip command.
$ ls -l -rw-r--r-- scott scott 224425 paradise_lost.txt.gz $ gunzip paradise_lost.txt.gz $ ls -l -rw-r--r-- scott scott 508925 paradise_lost.txt
In the same way that gzip removes the original file, leaving you solely with the gzipped result, gunzip removes the .gz file, leaving you with the final gunzipped result. If you want to ensure that you have both, you need to use the -c option (or --stdout or --to-stdout) and pipe the results to the file you want to create.
$ ls -l -rw-r--r-- scott scott 224425 paradise_lost.txt.gz $ gunzip -c paradise_lost.txt.gz > paradise_lost.txt $ ls -l -rw-r--r-- scott scott 508925 paradise_lost.txt -rw-r--r-- scott scott 224425 paradise_lost.txt.gz
It's probably a good idea to use -c, especially if you plan to keep behind the .gz file or pass it along to someone else. Sure, you could use gzip and create your own archive, but why go to the extra work?
Note - If you don't like the gunzip command, you can also use gzip -d (or --decompress or --uncompress).
Fast/Flexible Linux OS Recovery
On Demand Now
In this live one-hour webinar, learn how to enhance your existing backup strategies for complete disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible full-system recovery solution for UNIX and Linux systems.
Join Linux Journal's Shawn Powers and David Huffman, President/CEO, Storix, Inc.
Free to Linux Journal readers.Register Now!
- Back to Backups
- Download "Linux Management with Red Hat Satellite: Measuring Business Impact and ROI"
- Google's Abacus Project: It's All about Trust
- Secure Desktops with Qubes: Introduction
- Fancy Tricks for Changing Numeric Base
- Seeing Red and Getting Sleep
- Secure Desktops with Qubes: Installation
- Working with Command Arguments
- Linux Mint 18
- CentOS 6.8 Released
Until recently, IBM’s Power Platform was looked upon as being the system that hosted IBM’s flavor of UNIX and proprietary operating system called IBM i. These servers often are found in medium-size businesses running ERP, CRM and financials for on-premise customers. By enabling the Power platform to run the Linux OS, IBM now has positioned Power to be the platform of choice for those already running Linux that are facing scalability issues, especially customers looking at analytics, big data or cloud computing.
￼Running Linux on IBM’s Power hardware offers some obvious benefits, including improved processing speed and memory bandwidth, inherent security, and simpler deployment and management. But if you look beyond the impressive architecture, you’ll also find an open ecosystem that has given rise to a strong, innovative community, as well as an inventory of system and network management applications that really help leverage the benefits offered by running Linux on Power.Get the Guide