Working with Compressed Files

0
114
1. bzip2 [.bz2]
 This is one of the newer formats available, and my format of choice, owning to its superior compression ratio, its based on the "Burrows-Wheeler Transform", and a achieves one of the best lossless compression ratios available as of yet.
 The bzip2 tool which is used to compress and decompress files in this format, and its included with OSX plus, every Linux Distribution I have ever seen, and also should be included with other OS such as Solaris and BSD.
 Compress Example
 In this first example, the specified file is compress, and replaced with the compressed file, the .bz2 is appended to the filename.
 bzip2 nameoffile
 Decompress Example 1
 In this first decompression example, the specified file is decompressed and replaces the compressed version, the .bz2 extension is removed. The -d command switch is used to specify the file is to be decompressed.
 bzip2 -d nameoffile.bz2
 Decompress Example 2
 In this second decompression example, the specified file is decompressed, but this time does not overwrite the compressed file, but instead is written to the outputfile. The command switch -c is used in addition to the last example, this writes the decompressed data to the stdout (console).
 bzip2 -dc nameoffile.bz2 >outputfile
 Thats it for this type of file when used on its own, I talk more about it in the tar section. For more information have a look at the man page, which is accessed like this man bzip2. Below are links to the home page for bzip2 and also a technical article on the technology behind it.
2. compress/uncompress [.Z]
 This is one of the original compression formats, and has been replaced by gzip, which will decompress these files. That said the original tools to compress and decompress in this format are still around so are worth covering separately. The tools in question are; compress and uncompress, which are part of OSX, should already be part of your Linux distribution, and also should be available on other OS like Solaris and BSD.
 Compress Example 1
 In this first example, the specified file is replaced with the compressed file, the extension .Z is appended to the original filename.
 compress nameoffile
 Compress Example 2
 In this next example the specified file is compressed, but the original file is not replaced, instead the compressed data is piped to the specified output file. One thing to note if you use compress like this is that the original filename or extension is not automatically retained. You will see that the -c command switch is used, and tells compress to write the compressed data to the stdout.
 compress -c nameoffile >outputfile
 Decompress Example 1
 In this first example, the compressed file is decompressed, the compressed file is replaced with the decompressed one, the .Z extension is removed.
 uncompress nameoffile.Z
 Decompress Example 2
 With this next example, the compress tools is used to decompress. This decompression is specified with the -d command switch. Other than that the behavior is the same as the uncompress tools.
 compress -d nameoffile.Z
 As I mentioned above, this type of compressed file has now been replaced with gzip, which will decompress these files without any problems, so as you can expect the examples included in the gzip section are valid.
3. gzip/gunzip [.z .gz .gzip]
 This is the most common type of compression used in the non-windows world, and has many know file extensions associated with it, the most common are; .z and .gz with .gzip being unusual but still out in the world. The tools you use to work with these files are gzip and gunzip. Just like most of the other tools these are part of just about every flavor of UNIX known, such as Linux, OSX and Solaris. Compress Example 1
 Compress Example 1
 In this first example the specified filename is compressed and replaced with the compressed version, the default extension of .gz is appended.
 gzip nameoffile
 Compress Example 2
 In this next example the specified file is compressed, but the original file is not replaced, instead the compressed data is piped to the specified output file. One thing to note if you use gzip like this is that the original filename or extension is not automatically retained. You will see that the -c command switch is used, and tells gzip to write the compressed data to the stdout.
 gzip -c nameoffile >outputfile
 Decompress Example 1
 In this first example, the compressed file is decompressed, the compressed file is replaced with the decompressed one, the .gz, .z or .gzip extension is removed.
 gunzip nameoffile.gz
 or
 gunzip nameoffile.z
 or
 gunzip nameoffile.gzip
 Decompress Example 2
 With this next example, the gzip tools is used to decompress. This decompression is specified with the -d command switch. Other than that the behavior is the same as the gunzip tools.
 gzip -d nameoffile.gz
 or
 gzip -d nameoffile.z
 or
 gzip -d nameoffile.gzip
 Yes you would be right in thinking all of the above are effectively the same, with the only difference being the extension, where I have intentionally shown one example for each of the common gzip extensions.
 NOTE: gzip/gunzip are not the same as zip and will NOT work with zip files.
4. tar [.tar .tar.Z .tar.z .tar.gz .tgz .tar.bz2]
 This one is different to all of the others, in that tar the tool thats used with these extensions by itself does not compress anything, it was actually intended for use with tape backup media. Compression/decompress is only possible when tar is used in conjunction with any of the other tools. Now as this is the cause of the majority of the confusion, I have included compress and decompress examples for each of the most common combinations. No need to worry about this tool, its been part of all Linux distributions since the beginning of time, and is included with OSX, Solaris and BSD.
 NOTE: Warning for OSX users, although this tool is included as part of the OS, under 10.2.8 or earlier it does not support the -j or -y switches, so you will have to use bzip2 -d nameoffile.tar.bz2 | tar -xvf , which is this third example in the tar.bz2 section below. On the other hand if you are using 10.3 or later, this problem has been fixed with the -j switch being fully supported.
.tar
 These files are not compressed at all, they are just "concatenated" into a single file. You manipulate these files like this :-
 Creation Example
 To copy all the files in a specified directory into the named .tar file you use the tar command like this.
 tar cvf nameoffile.tar nameofdirectory
 The command switches used are cvf these are "create", "verbose" and "file". I do suggest that you look at the man page for tar, as their are masses of possible options with this powerful tool.
 Extraction Example
 Once you have your .tar file, you retrieve the files with the following command.
 tar xvf nameoffile.tar
 The command switches used this time are xvf these are "extract", "verbose" and "file". I do suggest that you look at the man page for tar, as their are masses of possible options with this powerful tool.
.tar.Z
 As I talked about above, the tar does not on its own compress anything, the compression comes from external tools, which in this case is compress.
 Compress Example
 This example will take all files in the specified directory and copy them in compressed format into the specified file. The files in the specified directory are not removed. The command switches cvfZ are, copy, verbose, files, and filter through compress.
 tar cvfZ nameoffile.tar.Z directoryname/
 Decompress Example 1
 This example will decompress the specified file, extracting all files, leaving the compressed file unchanged. The command switches xvfZ are extract, verbose, files, and filter through compress.
 tar xvfZ nameoffile.tar.Z directoryname/
 Decompress Example 2
 This next example does exactly the same as the last one, with exception that it will work with versions of tar that do not support the Z switch (if such a beast still exists). It works by first running compress with the d and c command switches which will decompress the specified file and write the decompressed data to stdout. Its then piped into tar which extracts the files. The original compressed file is unchanged.
 compress -dc nameoffile.tar.Z | tar -xvf -
.tar.z .tar.gz .tgz
 As I talked about above, the tar does not on its own compress anything, the compression comes from external tools, which in this case is gzip.
 Compress Example
 This example will take all the files in the specified directory, compress them then write them into the single file specified. The command switches cvfz are, copy, verbose, files, and filter through gzip.
 tar cvfz nameoff`ile.tar.z directoryname/
 or
 tar cvfz nameoffile.tar.gz directoryname/
 or
 tar cvfz nameoffile.tgz directoryname/
 Decompress Example 1
 This example will decompress the specified file, extracting all files and any directories into the current directory, leaving the compressed file unchanged. The command switches xvfz are extract, verbose, files, and filter through gzip.
 tar xvfz nameoffile.tar.z
 or
 tar xvfz nameoffile.tar.gz
 or
 tar xvfz nameoffile.tgz
 Decompress Example 2
 This next example does exactly the same as the last one, with exception that it will work with versions of tar that do not support the z switch (if such a beast still exists). It works by first running gzip with the d and c command switches which will decompress the specified file and write the decompressed data to stdout. Its then piped into tar which extracts the files. The original compressed file is unchanged.
 gzip -dc nameoffile.tar.z | tar -xvf -
 or
 gzip -dc nameoffile.tar.gz | tar -xvf-
 or
 gzip -dc nameoffile.tgz | tar -xvf -
.tar.bz2
 As I talked about above, the tar does not on its own compress anything, the compression comes from external tools, which in this case is bzip2. Now with this format their is a gotcha, in that one of the command switches you need has been changed between versions of tar. This is annoying, so I have included additional examples to help regardless of the version of tar you have.
 How to find the version of tar you have
 This is not normally a issue, but as I said above, some genius decided to change the command switch that causes tar to filter though bzip2. The version of tar you are using is identified like this.
 tar --version
 The two versions that I know of are 1.13 and on the RH box, 1.13.19. The interesting thing is that the latest version on the gnu web site is 1.13, so I am not sure where the later 1.13.19 came from.
 Compress Example 1. (tar version 1.13)
 This example will take all the files in the specified directory, compress them using bzip2 then write them into the single file specified. The command switches cvfy are, copy, verbose, files, and filter through bzip2. As with all the tar examples the original files are unaltered.
 tar cvfy nameoffile.tar.bz2
 Compress Example 2. (tar version 1.13.19)
 This example will take all the files in the specified directory, compress them using bzip2 then write them into the single file specified. The command switches cvfj are, copy, verbose, files, and filter through bzip2. As with all the tar examples the original files are unaltered.
 tar cvfj nameoffile.tar.bz2
 TIP: OSX 10.3 or later users can use the above compression example, while those with older versions of OSX need to use the following.
 Compress Example 3. (All versions)
 In this final compression example, the functionality of both the previous ones is replicated, but inplace of using the -y or -j switches, we are going to use UNIX pipes and the bzip2 to compress.
 tar cvf - nameoffile | bzip2 -c >compressedfile.tar.bz2
 Now this may look like lots of work, but actually its quite simple. What it does is; create a new archive containing nameoffile then write the archive to stdout which in then piped (the |) into the bzip2 tool which compresses it again to stdout where its redirected into compressedfile.tar.bz2.
 TIP: The nameoffile can be a single file or a directory.
 Decompress Example 1. (tar version 1.13)
 This example will decompress the specified file, extracting all files and any directories into the current directory, leaving the compressed file unchanged. The command switches xvfy are extract, verbose, files, and filter through bzip2.
 tar xvfy nameoffile.tar.bz2
 Decompress Example 2. (tar version 1.13.19)
 This example will decompress the specified file, extracting all files and any directories into the current directory, leaving the compressed file unchanged. The command switches xvfj are extract, verbose, files, and filter through bzip2.
 tar xvfj nameoffile.tar.bz2
 TIP: OSX 10.3 or later users can use the above example, while those with older versions need to use the next one.
 Decompress Example 3. (All versions)
 This next example does exactly the same as the last two, with exception that it will work with both versions of tarand should also work with those that do not support filtering though bzip2. It works by first running bzip2 with the d and c command switches which will decompress the specified file and write the decompressed data to stdout. Its then piped into tar which extracts the files and any directories into the current directory, leaving the original compressed file unchanged.
 bzip2 -dc nameoffile.tar.bz2 | tar -xvf -
zip/unzip [.zip]
 Yes this is the same as the .zip files you use in the windows world, these files are handled either by pkzip for UNIX or more commonly by zip and unzip, which are included in every Linux I have seen, and may also be part of other OS such as Solaris or BSD. Just in case you do not have these tools, or just want to learn more, here are links for downloads etc.
 For those of you who miss the old DOS pkzip tool, all is not lost their is a port to Linux although its binary only, and shareware, even with those problems, its included in the links.
 Compress Example
 This example will compress the specified file, writing the results to the specified zip file. The original file is retained.
 zip zipfilename.zip filetocompress
 Decompress Example
 This example will decompress the specified zip file, the contents of this compressed file including any directories are written to the current directory. The compressed zip file is unchanged.
 unzip nameoffile.zip

LEAVE A REPLY

Please enter your comment!
Please enter your name here