What are the commands for compressing files in linux and what are the difference between them? (for example: tar, gzip, rar).
I'm working on Linux Mint 11 and doing a script for compressing files. I need:

    1. support for password encryption (and decryption)
    2. support for division of file
    3. support for level of compression

Which type of compressing can I use in Linux Mint 11 (14 eventually) and is the best for me?

migrated from stackoverflow.com May 3 '13 at 10:20

This question came from our site for professional and enthusiast programmers.

  • tar is not a compressor (but may invoke gzip, bzip2, ....). – Basile Starynkevitch May 3 '13 at 10:20
up vote 2 down vote accepted

First of all, some of the commands are used to make archives - ie. a file that contain other files, often with directory-structure, ownership, permissions and dates. The most common of these are ar (used when creating libraries), tar (TapeARchive) and cpio (CoPyInOut).

Then we have some commands that are used to compress files - ie. making the file take-up less space on the disk. Compressing can also be done to archives. The most common compression-programs are gzip, bzip2, xz, 7zip... as well as the two older ones, compress and compact.

This is very much in accordance to the "Unix-philosophy", where each command does only one thing (but does it well)... here, either creates archives or compress a file - but not both. Of course one command may call another, as tar may call gzip or bzip2.

For Windows it's more common to make programs that combine related tasks - or rather, for the user seemingly related tasks... Like making and archive and compressing it - even though these are very different tasks. Consequently we also have some tools under Unix/Linux to handle - and sometimes create - such archives. zip, rar and arj are examples of such archives, that are more or less supported under Linux. These typically do both compressing and archiving at once (e.g. zip first compress the files, then makes an archive of the compressed files).

rar and zip do AFAIK know support some sort of "encryption" and password, but these are rather poor. You probably would be better off (as well as more correctly Unix-wise) to use a separate tool for encrypting and/or password-protecting the files - perhaps gnupg or truecrypt?

At least zip, rar and tar support creation of archives of multiple parts. However an other acceptable "Unix-way" to do it; would be to use split either on the uncompressed archive (tar or cpio) and compress each part separately, or on the compressed (eg. with bzip2) file - and later using cat to merge the parts. Splitting the compressed archive would perhaps be best, as after re-merging, the compress-program (eg. bzip2) would detect any errors. Although compressing each part individually, would ensure that the other parts could be recovered (with most of their files) if one part was damaged.

  • While I agree with your TrueCrypt recommendation, AFAIK all modern ZIP and RAR versions/variants use 128/256-bit AES (although PKWare's SecureZip supports other algorithms as well), so can you elaborate further or provide reliable sources to back up your claim that their encryption/security is "poor", especially if sufficiently lengthy and complex passwords/phrases are used? – Karan May 4 '13 at 1:31

In general, bzip is better in compressing than (g)zip, and 7zip is better than bzip. Tar is for bundling files into one big file and does not compress itself. It's often used together with a compression tool. AFAIK all formats can handle encryption, division and the level of compression, but not all tools can use these options.

This will tar and compress. I don't know in what order, but I think the file is first compressed, then added to the archive. The original files stay where they are.

tar -czvf test.tgz test

Another option is gzip, but this only works on single files

gzip test

This will zip the file and add it to a new archive. When all is done, the test-file is deleted I believe.

  • So you suggest to tar firstly the file and then compress it? I can use only commands that are standard in Linux. Can 7zip or bzip be used without installing additional packages? – Nojas May 3 '13 at 11:01
  • @Nojas yes you can. As for standard, tar and gzip and almost certainly bzip will be installed by default, as will compress. – terdon May 3 '13 at 11:47
  • 7zip is not standard I believe. Use gzip or tar as explained in my edited answer. – SPRBRN May 3 '13 at 12:10
  • 1
    "In general, bzip is better than (g)zip, 7zip is better than bzip." In terms of compression level this is true. In terms of speed and memory usage it's the reverse. BTW, your gzip example is flat-out wrong, because gzip only compresses, it's not an archiver. it makes a foo.gz file for every input foo (and then deletes the uncompressed version). gzip test just performs that process for 'test'. If you wanted to you could tar up (without compression) a bunch of gz's generated that way though. – kampu May 21 '13 at 7:20
  • You're right about gzip. I updated my answer. – SPRBRN May 21 '13 at 15:23

Your Answer


By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.