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, 2011 at 21:21
4 Answers
Why not tar
the files to be encrypted and then encrypt the tarball?
-
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! Jan 20, 2011 at 6:57
-
@gladimdim: thanks for accepting, but what do you mean by "not flexible" and "lot of resources"? Or was there a typo?– alexJan 20, 2011 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. Jan 27, 2011 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?– alexJan 27, 2011 at 22:01
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.
-
1Thanks, this works good except that gpg does not support --multiple key with --symmetric option (I want to encrypt only using passphrase). Jan 20, 2011 at 6:56
I've read the comments on the answer that has been marked as accepted.
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 so that your output is compressed and encrypted.
Note that it'll still use significant system resources so consideri using nice
.
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, check 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="[email protected]"
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 [email protected]
and should output to the /tmp
directory a file <current-date>_<top-dir-name>.tgz.gpg
So I don't know of a way to encrypt a whole directory but here is a command that will encrypt all of the files within a directory ending with .csv.
find . -name "*.csv" -exec gpg --cipher-algo AES256 --symmetric --passphrase-file .passphrase --batch --yes {} ;