10

Is there any way to encrypt a directory using gpg? It seems to only accept files as arguments.

  • There are also ways to encrypt a whole directory or drive, but they are different in scope: they offer transparent encryption (you just type a password to mount the drive), but the result can't be read by GPG or PGP. – Gilles 'SO- stop being evil' Jan 18 '11 at 21:21
11

Why not tar the files to be encrypted and then encrypt the tarball?

| improve this answer | |
  • I found this to be the solution. It is not flexible and requires a lot of resources to tar and gpg date. Thanks for the answer! – gladimdim Jan 20 '11 at 6:57
  • @gladimdim: thanks for accepting, but what do you mean by "not flexible" and "lot of resources"? Or was there a typo? – alex Jan 20 '11 at 11:59
  • by "not flexible" I meant that I needed manually tar directory then gpg it. I have about 60Gb of pics which are now tarballed and gpged. It took about 2 hours and 4 concurrent terminal windows to speed up this process. Now I just tarball and gpg every new directory with pics which I create. – gladimdim Jan 27 '11 at 21:51
  • Hm, that seems pretty reasonable time for that size of data. Did you expect to run it a lot faster? What's you plan when you need one those pics--decrypt/untar all the 60Gigs? – alex Jan 27 '11 at 22:01
2

I just saw the option --multifile on the manpage:

This modifies certain other commands to accept multiple files for processing on the command line or read from STDIN with each filename on a separate line. This allows for many files to be processed at once. --multi‐ file may currently be used along with --verify, --encrypt, and --decrypt. Note that --multifile --verify may not be used with detached signatures.

What you are specifically looking for is --encrypt-files and, again the manpage:

Identical to --multifile --encrypt.

| improve this answer | |
  • 1
    Thanks, this works good except that gpg does not support --multiple key with --symmetric option (I want to encrypt only using passphrase). – gladimdim Jan 20 '11 at 6:56
1

Hey I read the comments on the answer that has been marked as excepted; looks like you should be made aware of the magic of | (anonymous pipes) check the answer I just posted on superuser and you'll find that tar & gpg can be joined together such that your output is compressed and encrypted before being output. Note it'll still use significant system resources so check the man pages for nice command for limiting a commands' ability to eat up only a certain percentage of resources. Also while I'm in the mood to suggest topics that may make your life easier on the command line in general; look into file descriptors and named pipes for passing data around.

If you wish to see what kind of magic can be performed when the above subjects are understood, then check the Travis-CI build logs and related scripts for solutions related to automating GnuPG on the command line.

---- Updates

As requested, an example for dealing with directories can be found within the previously mentioned script at line 680 and a more generalized example would be...

#!/usr/bin/env bash
dir_path="${1:?${0##*/} needs a directory path as the first argument}"
default_gpg_email="your-email@host.domain"
gpg_email="${2:-$default_gpg_email}"
_dir_name="${dir_path##*/}"
_dir_name="${_dir_name%/*}"
Var_star_date="$(date -u +%s)"

if [ -d "${dir_path}" ]; then
    tar -cz - "${dir_path}" | gpg --always-trust --armor --batch --no-tty --encrypt --recipient ${gpg_email} > /tmp/${Var_star_date}_${_dir_name}.tgz.gpg"
else
    echo "${0##*/} operates on directories"
    exit 1
fi

... which maybe run with script-name.sh /path/to/dir or script-name.sh /path/to/dir new-email@host.domain and should output to the /tmp directory a file <current-date>_<top-dir-name>.tgz.gpg

| improve this answer | |
  • Can you write an example ? – Nikana Reklawyks Sep 22 '17 at 5:45
  • Yep, though it may take me a bit :-D – S0AndS0 Jan 23 '18 at 9:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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