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. Commented Jan 18, 2011 at 21:21

4 Answers 4


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!
    – gladimdim
    Commented 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?
    – alex
    Commented Jan 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.
    – gladimdim
    Commented 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?
    – alex
    Commented Jan 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.

  • 1
    Thanks, this works good except that gpg does not support --multiple key with --symmetric option (I want to encrypt only using passphrase).
    – gladimdim
    Commented 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]"
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"
    echo "${0##*/} operates on directories"
    exit 1

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

  • Can you write an example ? Commented Sep 22, 2017 at 5:45
  • Yep, though it may take me a bit :-D
    – S0AndS0
    Commented Jan 23, 2018 at 9:35

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 {} ;

You must log in to answer this question.

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