Securely Transferring a Directory

Posted on by Miles Steele

Have a directory that you would like to you would like to securely share with someone else? This post describes how to encrypt a directory and protect the file with a password for transfer using tar and gpg.

To find this useful you:

You will need:

If you don’t have gpg set up already, check out How To Use GPG. You may not need to set up a private key for this task.

Encrypting and uploading the directory

Let’s say you have some secrets in ./secrets/ and want to share that directory. We will use tar to bundle the directory into a single file and gpg to secure it with a password.

Create the (unencrypted) archive:

$ tar -cvzf secrets.tar.gz secrets

Encrypt the tarball using gpg:

$ gpg -c secrets.tar.gz

This will create the password-encrypted file secrets.tar.gz.gpg.

Now upload the file to your server:

$ scp secrets.tar.gz

Don’t forget to let the recipient of your files know the password over some other secure channel.

Downloading and decrypting the directory

Receiving the file is a bit easier. The receiver still needs gpg to decrypt the file.

Download the encrypted file:

$ wget

Decrypt and pipe the output through the tar to unpack.

$ gpg --output - secrets.tar.gz.gpg | tar -xvz

The secrets should now be in ./secrets/

Note that tar will replace existing files with the version from the tarball. So you may want to be cautious and do this from inside a new directory.