78

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?

vdd
  • 107
c12
  • 881

7 Answers7

62

Use crypt or gpg on the file.

Simple examples:

cat filename | crypt > filename.crypt

gpg -c –o filename.gpg filename

Tanky Woo
  • 185
59

You can use command:

zip -P password file.zip file

Or better:

zip -e file.zip file

man zip
mtk
  • 1,147
Panta
  • 691
27

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.

thiagowfx
  • 1,808
20

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).

Chris W. Rea
  • 10,978
10

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
c4baf058
  • 398
6

To zip a file with password run the following command:

zip -er name.zip folder/

It will show a prompt to enter a hidden password.


To unzip the file, run:

unzip name.zip

And enter the password you added before.

Ahmad Shbita
  • 101
  • 1
  • 3
0

With ccencrypt / ccdecrypt (installed with ccrypt, or use ccrypt -s / ccrypt -d), you can put the password in an environment variable and use the -E var CLI option, so you don't need to worry about storing the password in a file that might be leaked.

echo -n 'Enter the file password: '
read P1
echo -n 'Confirm file password: '
read P2
if [ "$P1" = "$P2" ]
then
   export P1
   ccencrypt -E P1 file.tgz
fi
unset P1 P2

then later

echo -n 'Enter file password: '
read P
export P
ccdecrypt -E P file.tgz.cpt
unset P
djb
  • 149