Kindly help to zip,gz,tar,.... a normal text file and recamand to use a one which reduces the size .
migrated from stackoverflow.com Sep 26 '10 at 7:32
This question came from our site for professional and enthusiast programmers.
I suggest you use zip if you don't have any compelling reason not to. It's supported on most (all?) OS.
Including some classic but now obsolete commands, the pure compression utilities are:
Separately, there are archivers - programs that collect several files together into a a single output (which might be a tape file, but usually isn't any more). These include:
And then there's the hybrid program originating on Windows:
Nowadays, the GNU Tar can handle compression as well as archiving, so it is generally the tool of choice for archiving.
Comparing compression programs
I've listed the compressors in order of appearance, which is also the order of increasing efficiency. The original pack was rather feeble by modern standards; compress could out-compress pack by a fair margin. However, gzip out-compressed compress by a considerable margin, and for quite a long time was used almost all the time because it was about as good as it got. It is the standard compression used in many software archives - CPAN (Comprehensive Perl Archive Network) uses gzipped tar files, for example, and so did GNU until recently, but they now sometimes provide xz or bzip2 files too. Bzip2 out-compresses gzip - a new compressor has to be better to become accepted, and direct support for it is built into the latest versions of GNU Tar. Finally, xz is a relatively new entrant (last 2-3 years, perhaps), but it out-compresses everything else.
Zip fits into the order of compression at about the same level as gzip or bzip2; it is a pretty good compression, but it originated on Windows and was standard there. However, you can get zip and unzip for Unix (just as you can get the other programs for Windows). So, zip files tend to indicate a Windows orientation.
Both pack and compress are functionally obsolete - but they are still recognized by gzip and can be decompressed by it. Their existence explains where the 'z' notation came from, and why plain 'z' is not used.
In some respects, the cpio program was superior to the tar format tapes (and there are, in fact, multiple formats for tar, but you are unlikely ever to have to worry about those details). The most notable difference was that cpio read the names of the files to be backed up from standard input - one name per line. This made it possible to be selective when backing up a directory. By contrast, tar is given the name of a directory and backs up everything underneath it (unless it is GNU Tar and is told to exclude stuff). You can also use cpio in 'pass' mode (cpio -p) to copy material from one place to another. However, GNU Tar has subsumed the read-files-from-file functionality and is easier to use and more widely available.
One other advantage of cpio was that it used variable length file headers whereas tar used fixed length file headers. These days, when everything is compressed, the wasted space in the tar header is a non-issue - it ends up being at most a couple of bytes on disk. One disadvantage of the cpio formats was that there were two of them - and the binary one was platform specific. As long as you always used the portable character format, there was no problem.
These days, you probably won't ever use cpio, but its pass-through mode is still useful if you're familiar with it.
The equivalent in GNU Tar is:
The original (1988) POSIX standard was produced at a time when there was a war going on over tar vs cpio, and pax was the POSIX archive format and program that was a compromise. However, it was never widely used - it suffered from not being tar or cpio. It was intended to bring peace between the warring camps; it didn't succeed.
GNU Tar supports the various pax formats (two versions).
GNU Tar allows you to specify the compression algorithm to use when creating an archive and automatically detects (known) formats when extracting from an archive. Hence:
This uses gzip compression. The extension .tgz is also used to indicate gzipped tar files:
If you use gunzip on a .tgz file, you will get a .tar file left behind; if you then gzip the file, you will get a .tar.gz file, though.
To use bzip2 compression:
To use xz compression:
AFAIK, there isn't a GNU Tar option to do this (xz compression) automatically. (The '-j' option is a relatively recent addition to GNU Tar.) You can designate any compression program via the '
One quirk of GNU Tar: the original tar program was written in the very early days of Unix, before the '-option' notation was standardized. Consequently, the dash in front of the first option to tar is optional to this day. You will see people use '
Which command to use?
It depends on your target audience. Those on Windows like zip. Material aimed for Windows should use this format.
Those who work primarily on Unix and derivatives aren't keen on zip: use GNU Tar instead. For the most widespread reliable compression, use gzip. If your audience is mostly up to date, consider using bzip2 instead. If you can dictate to them, use xz because it reduces files enormously - better than any of the others.
If you are providing files for others to download, providing several formats is a courtesy; people can choose what they like.
For simply compressing a single file, I'd use xz or bzip2.