Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using the Unix tar command as follows to tar up a directory and its files:

tar cvzf fileToTar.tgz directoryToTar

Is there a way to password protect the .tgz file? I've created password-protected ZIP files on Windows so I would assume Unix has the same capability. Any ideas?

share|improve this question

migrated from Dec 21 '11 at 21:00

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

up vote 17 down vote accepted

Use crypt or gpg on the file.

Simple examples:

cat filename | crypt > filename.crypt

gpg -c filename –o filename.gpg

share|improve this answer
A simple example within the answer would be more helpful. – mtk Jun 20 '12 at 8:07
If you click the blue-ish text in my answer that says "crypt" or "gpg", your browser will display simple examples for you. – Christopher Neylan Jun 20 '12 at 14:40
a simple example would have been suffice instead of wasting out time clicking on something. – duckx Jan 24 '14 at 4:18
crypt is considered weak. Better to use mcrypt or ccrypt – michaelbn Aug 12 '15 at 6:44

You can use command:

zip -P password file

Or better:

zip -e file

man zip
share|improve this answer

Neither the tar format nor the gz format has built-in support for password-protecting files.

The Windows zip format combines several different piece of functionality: compression (e.g. gzip), archiving multiple files into one (e.g. tar), encryption (e.g. gnupg), and probably others. Unix tends to have individual tools, each of which does one thing well, and lets you combine them.

The Unix equivalent of a password-protected .zip file would probably be called something like foo.tar.gz.gpg or foo.tgz.gpg.

And there are open-source zip and unzip tools for Unix, though they may not provide all the capabilities of the Windows versions (I'm fairly sure the newer .zipx format isn't supported).

share|improve this answer

You can use ccrypt.

Things can be encrypted by a pipe:

tar cvvjf - /path/to/files | ccrypt > backup.tar.bz2.cpt

Or in place:

ccrypt backup.tar.bz2

For automating, you can save a passkey into a file and use this passkey to encrypt:

ccrypt -k ~/.passkey backup.tar.bz2
share|improve this answer

You can use gpg (=GnuPG):

gpg -o fileToTar.tgz.gpg --symmetric fileToTar.tgz

This will prompt you for a passphrase.

To decrypt the file later on, just do a:

gpg fileToTar.tgz.gpg

This will prompt you, again, for the passphrase.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .