Kindly help to zip,gz,tar,.... a normal text file and recamand to use a one which reduces the size .
Including some classic but now obsolete commands, the pure compression utilities are:
- pack (unpack, pcat) - extension .z
- compress (uncompress) - extension .Z
- gzip (gunzip, gzcat, ...) - extension .gz
- bzip2 (bunzip2, bzcat, ...) - extension .bz2
- xz (unxz, xzcat, ...) - extension .xz
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:
- ar - archive
- tar - tape archive
- cpio - Unix System III archiver
- pax - POSIX hybrid of cpio and tar
And then there's the hybrid program originating on Windows:
- zip - archive and compress
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.
find . -name '*.[ch]' -print | cpio -pdmB /some/where/else
The equivalent in GNU Tar is:
find . -name '*.ch' -print | tar -cf - -T - | tar -xf - -C /some/where/else
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:
tar -czf /tmp/wotnot.tar.gz some-sub-directory cd /some/other/place tar -xf /tmp/wotnot.tar.gz
This uses gzip compression. The extension .tgz is also used to indicate gzipped tar files:
tar -czf /tmp/wotnot.tgz some-sub-directory cd /some/other/place tar -xf /tmp/wotnot.tgz
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:
tar -cjf /tmp/wotnot.tar.bz2 some-sub-directory cd /some/other/place tar -xf /tmp/wotnot.tar.bz2
To use xz compression:
tar -cf /tmp/wotnot.tar.xz --use-compress-program=xz some-sub-directory cd /some/other/place tar -xf /tmp/wotnot.tar.xz --use-compress-program=xz
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 '
--use-compress-program' option as long as it obeys the same generic interface as gzip, bzip2 - and xz does obey it. For a hypothetical compression progam 'zipper':
zipper -c -- compresses standard input to standard output zipper -d -- decompresses zipper -c -d -- decompresses standard input to standard output
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 '
tar cvf /tmp/tarfile .' or equivalent without the dash.
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.
zip can compress several files and put them in one
.zip-file leaving the original files alone.
gzip on the other creates one
.gz per file and replaces the original keeping permissions/ownership the same.
tar was just a file-bundler. It takes the input and makes one
.tar-file containing the lot retaining directory structure and permissions. Later they added compression compression, normally the
-zoption is used (that's gzip)
I suggest you use zip if you don't have any compelling reason not to. It's supported on most (all?) OS.