How to password protect gzip files on the command line?

I want to create some tar.gz (and possibly tar.bz2) files, using the tar command on Ubuntu 10.04.

I want to password protect the file.

What is the command to do this (I have Googled, but found nothing that shows how to create and extract compressed files using a password).

Anyone knows how to do this?


you have to apply the unix-philosophy to this task: one tool for each task.

tarring and compression is a job for tar and gzip or bzip2, crypto is a job for either gpg or openssl:

Encrypt

 % tar cz folder_to_encrypt | \
      openssl enc -aes-256-cbc -e > out.tar.gz.enc

Decrypt

 % openssl enc -aes-256-cbc -d -in out.tar.gz.enc | tar xz

Or using gpg

 % gpg --encrypt out.tar.gz

the openssl-variant uses symetric encryption, you would have to tell the receiving party about the used 'password' (aka 'the key'). the gpg-variant uses a combination of symetric and asymetric encryption, you use the key of the receiving party (which means that you do not have to tell any password involved to anyone) to create a session key and crypt the content with that key.

if you go the zip (or 7z) route: essentially that is the same as the openssl-variant, you have to tell the receiving party about the password.


If your intent is to just password protect files, then use the hand zip utility through command line

zip -e <file_name>.zip <list_of_files>

-e asks the zip utility to encrypt the files mentioned in

Working example:

$ touch file_{0,1}.txt # creates blank files file_0 & file_1    
$ zip -e file.zip file_* # ask zip to encrypt
$ ENTER PASSWORD:
$ VERIFY PASSWORD:
$ ls file*