Showing posts with label Gzip Command. Show all posts
Showing posts with label Gzip Command. Show all posts

Tuesday, 8 September 2020

tar gzip example - How to work with files that are tar'd and gzip'd

LPI tar gzip, LPI Exam Prep, LPI Tutorial and Material, LPI Cert Exam, LPI Certifications

tar gzip FAQ: How do I work with tar archives that have been created with tar and gzip?


When you work on Unix, Linux, and Mac OS X systems, you'll quickly find that tools like tar and gzip are your good friends, so learning how to work with them is very important. Here's a quick look at how to work with the most common tar/gzip scenarios.

1) Create a tar'd and gzip'd archive of a directory


A lot of times you'll have a directory that you want to either (a) make a backup copy of, or (b) share with other people. The most common way to do that these days is to create an archive that is "tar'd and gzip'd".

Here's how you create a tar'd and gzip'd archive of a directory (i.e., a "tar tgz" file) in your current folder named mydirectory:

tar czvf mydirectory.tgz mydirectory

A few quick notes about this tar/gzip example:

1. I've included four options with the tar command:

      1. c - create a new archive.
      2. z - gzip'd the archive.
      3. v - work verbosely, showing me the name of each file you add. This is optional.
      4. f - specifies that you want to use the following filename (mydirectory.tgz) as the name of the archive.

2. When creating an archive that has been tar'd and gzip'd, this is considered a "tar tgz" file, so it's common practice to end your filename with the extension tgz (though you can call it whatever you want).

2) Extract the contents of a tar/gzip (tar tgz) archive


Now imagine that you have just received a tar/gzip archive like this from someone else. To extract the contents of the archive issue a very similar command, this time using an x (for "eXtract") instead of the c argument, like this:

tar xzvf mydirectory.tgz

This command extracts whatever was in that tar'd and gzip'd archive to your filesystem.

3) List the contents of a tar/gzip archive


If that previous command sounded scary, maybe it should be. If this is an archive you just received from someone else, you may want to look at its contents before just extracting it to your filesystem.

Fortunately you can list the contents of an archive very easily, using the t option ("lisT") instead of the x argument, like this:

tar tzvf mydirectory.tgz

This command lists the contents of the archive, but does not extract the files in the archive to your filesystem.

4) How to gzip an existing tar file


As you work in the Unix world you'll also run into files named with a "tar.gz" extension, like this:

foo.tar.gz

When you see this ".tar.gz" file extension, it indicates that the file has probably been tar'd first, and then gzip'd second. This is a two-step process -- and the way I used to do this -- that works like this:

# step 1
tar cvf mydirectory.tar mydirectory

# step 2
gzip mydirectory.tar

As a practical matter you can treat a tar.gz file just like a tgz file, and extract its contents using the same command that was shown earlier:

tar xzvf mydirectory.tar.gz

Once I learned that I could tar and gzip a file with one command I stopped using this two-step process, but again, either way will work.

Thursday, 4 June 2020

Linux gzip: How to work with compressed files

Linux Tutorial and Material, Linux Certification, Linux Exam Prep, Linux Prep

If you work much with Unix and Linux systems you'll eventually run into the terrific file compression utilities, gzip and gunzip. As their names imply, the first command creates compressed files (by gzip'ing them), and the second command unzip's those files.

In this post I take a quick look at the gzip and gunzip file compression utilities, along with their companion tools you may not have known about: zcat, zgrep, and zmore.

The Unix/Linux gzip command


You can compress a file with the Unix/Linux gzip command. For instance, if I run an ls -l command on an uncompressed Apache access log file named access.log, I get this output:

-rw-r--r--   1 al  al  22733255 Aug 12  2008 access.log

Note that the size of this file is 22,733,255 bytes. Now, if we compress the file using gzip, like this:

gzip access.log

we end up creating a new, compressed file named access.log.gz. Here's what that file looks like:

-rw-r--r--   1 al  al  2009249 Aug 12  2008 access.log.gz

Notice that the file has been compressed from 22,733,255 bytes down to just 2,009,249 bytes. That's a huge savings in file size, roughly 10 to 1(!).

There's one important thing to note about gzip: The old file, access.log, has been replaced by this new compressed file, access.log.gz. This might freak you out a little the first time you use this command, but very quickly you get used to it. (If for some reason you don't trust gzip when you first try it, feel free to make a backup copy of your original file.)

The Linux gunzip command


The gunzip ("g unzip") command works just the opposite of gzip, converting a gzip'd file back to its original format. In the following example I'll convert the gzip'd file we just created back to its original format:

gunzip access.log.gz

Running that command restores our original file, as you can see in this output:

-rw-r--r--   1 al  al  22733255 Aug 12  2008 access.log

The Linux file compress utilities (zcat, zmore, zgrep)


I used to think I had to uncompress a gzip'd file to work on it with commands like cat, grep, and more, but at some point I learned there were equivalent gzip versions of these same commands, appropriately named zcat, zgrep, and zmore. So, anything you would normally do on a text file with the first three commands you can do on a gzip'd file with the last three commands.

For instance, instead of using cat to display the entire contents of the file, you use zcat to work on the gzip'd file instead, like this:

zcat access.log.gz

(Of course that output will go on for a long time with roughly 22MB of compressed text.)

You can also scroll through the file one page at a time with zmore:

zmore access.log.gz

And finally, you can grep through the compressed file with zgrep:

zgrep '/java/index.html' access.log.gz

There are also two other commands, zcmp and zdiff, that let you compare compressed files, but I personally haven't had the need for them. However, as you can imagine, they work like this:

zmp file1.gz file2.gz

or

zdiff file1.gz file2.gz

Linux gzip / compress summary


As a quick summary, just remember that you don't have to uncompress files to work on them, you can use the following z-utilities to work on the compressed files instead:

◉ zcat
◉ zmore
◉ zgrep
◉ zcmp
◉ zdiff

Thursday, 14 May 2020

Linux gzip: How to work with compressed files

LPI Tutorial and Material, LPI Certification, LPI Guides, LPI Exam Prep

If you work much with Unix and Linux systems you'll eventually run into the terrific file compression utilities, gzip and gunzip. As their names imply, the first command creates compressed files (by gzip'ing them), and the second command unzip's those files.

In this post I take a quick look at the gzip and gunzip file compression utilities, along with their companion tools you may not have known about: zcat, zgrep, and zmore.

The Unix/Linux gzip command


You can compress a file with the Unix/Linux gzip command. For instance, if I run an ls -l command on an uncompressed Apache access log file named access.log, I get this output:

-rw-r--r--   1 al  al  22733255 Aug 12  2008 access.log

Note that the size of this file is 22,733,255 bytes. Now, if we compress the file using gzip, like this:

gzip access.log

we end up creating a new, compressed file named access.log.gz. Here's what that file looks like:

-rw-r--r--   1 al  al  2009249 Aug 12  2008 access.log.gz

Notice that the file has been compressed from 22,733,255 bytes down to just 2,009,249 bytes. That's a huge savings in file size, roughly 10 to 1(!).

There's one important thing to note about gzip: The old file, access.log, has been replaced by this new compressed file, access.log.gz. This might freak you out a little the first time you use this command, but very quickly you get used to it. (If for some reason you don't trust gzip when you first try it, feel free to make a backup copy of your original file.)

The Linux gunzip command


The gunzip ("g unzip") command works just the opposite of gzip, converting a gzip'd file back to its original format. In the following example I'll convert the gzip'd file we just created back to its original format:

gunzip access.log.gz

Running that command restores our original file, as you can see in this output:

-rw-r--r--   1 al  al  22733255 Aug 12  2008 access.log


The Linux file compress utilities (zcat, zmore, zgrep)


I used to think I had to uncompress a gzip'd file to work on it with commands like cat, grep, and more, but at some point I learned there were equivalent gzip versions of these same commands, appropriately named zcat, zgrep, and zmore. So, anything you would normally do on a text file with the first three commands you can do on a gzip'd file with the last three commands.

For instance, instead of using cat to display the entire contents of the file, you use zcat to work on the gzip'd file instead, like this:

zcat access.log.gz

(Of course that output will go on for a long time with roughly 22MB of compressed text.)

You can also scroll through the file one page at a time with zmore:

zmore access.log.gz
And finally, you can grep through the compressed file with zgrep:

zgrep '/java/index.html' access.log.gz

There are also two other commands, zcmp and zdiff, that let you compare compressed files, but I personally haven't had the need for them. However, as you can imagine, they work like this:

zmp file1.gz file2.gz

or

zdiff file1.gz file2.gz


Linux gzip / compress summary


As a quick summary, just remember that you don't have to uncompress files to work on them, you can use the following z-utilities to work on the compressed files instead:

◉ zcat
◉ zmore
◉ zgrep
◉ zcmp
◉ zdiff

Saturday, 16 March 2019

Gzip Command in Linux

gzip command compresses files. Each single file is compressed into a single file. The compressed file consists of a GNU zip header and deflated data.

Gzip Command, Linux Tutorial and Material, Linux Certifications, Linux Guides

If given a file as an argument, gzip compresses the file, adds a “.gz” suffix, and deletes the original file. With no arguments, gzip compresses the standard input and writes the compressed file to standard output.

Difference between Gzip and zip command in Unix and when to use which command


◈ ZIP and GZIP are two very popular methods of compressing files, in order to save space, or to reduce the amount of time needed to transmit the files across the network, or internet.

◈ In general, GZIP is much better compared to ZIP, in terms of compression, especially when compressing a huge number of files.

◈ The common practice with GZIP, is to archive all the files into a single tarball before compression. In ZIP files, the individual files are compressed and then added to the archive.

◈ When you want to pull a single file from a ZIP, it is simply extracted, then decompressed. With GZIP, the whole file needs to be decompressed before you can extract the file you want from the archive.

◈ When pulling a 1MB file from a 10GB archive, it is quite clear that it would take a lot longer in GZIP, than in ZIP.

◈ GZIP’s disadvantage in how it operates, is also responsible for GZIP’s advantage. Since the compression algorithm in GZIP compresses one large file instead of multiple smaller ones, it can take advantage of the redundancy in the files to reduce the file size even further.

◈ If you archive and compress 10 identical files with ZIP and GZIP, the ZIP file would be over 10 times bigger than the resulting GZIP file.

Syntax :


gzip [Options] [filenames]

Example:


$ gzip mydoc.txt

This command will create a compressed file of mydoc.txt named as mydoc.txt.gz and delete the original file.

Gzip Command, Linux Tutorial and Material, Linux Certifications, Linux Guides

Options:


1. -f option: Sometimes a file cannot be compressed. Perhaps you are trying to compress a file called “myfile1” but there is already a file called “myfile1.gz”. In this instance, the “gzip” command won’t ordinarily work.

To force the “gzip” command to do its stuff simply use -f option:

$ gzip -f myfile1.txt

This will forcefully compress a file named myfile.txt even if there already exists a file named as myfile.txt.gz

2. -k option: By default when you compress a file using the “gzip” command you end up with a new file with the extension “.gz”.If you want to compress the file and keep the original file you have to run the gzip command with -k option:

$ gzip -k mydoc.txt

The above command would end up with a file called “mydoc.txt.gz” and “mydoc.txt”.

3. -L option: This option displays the gzip license.

$ gzip -L filename.gz

OUTPUT :

Apple gzip 264.50.1 (based on FreeBSD gzip 20111009)
Copyright (c) 1997, 1998, 2003, 2004, 2006 Matthew R. Green
All rights reserved.

4. -r option: This option can compress every file in a folder and its subfolders.This option doesn’t create one file called foldername.gz. Instead, it traverses the directory structure and compresses each file in that folder structure.

gzip -r testfolder

This will compress all the files present in the testfolder.

5. -[1-9] option: It allows to change the compression level.A file can be compressed in different ways. For instance, you can go for a smaller compression which will work faster or you can go for maximum compression which has the tradeoff of taking longer to run.The speed and compression level can vary by levels using numbers between 1 and 9.

$ gzip -1 mydoc.txt

This will get maximum compression at the slowest speed

$ gzip -9 mydoc.txt

To get minimum compression at the fastest speed

6. -v option: This option displays the name and percentage reduction for each file compressed or decompressed.

$ gzip -v mydoc.txt

OUTPUT:

new.txt:       18.2% -- replaced with new.txt.gz

7. -d option: This option allows to decompress a file using the “gzip” command.

$ gzip -d mydoc.txt.gz

This command will unzip the compressed file named as mydoc.txt.gz.