7.18. Encrypting Directories

Problem

You want to encrypt an entire directory tree.

Solution

To produce a single encrypted file containing all files in the directory, with symmetric encryption:

$ tar cf - name_of_directory | gpg -c > files.tar.gpg

or key-based encryption:

$ tar cf - name_of_directory | gpg -e > files.tar.gpg

To encrypt each file separately:

$ find name_of_directory -type f -exec gpg -e '{}' \;

Discussion

Notice the find method uses public-key encryption, not symmetric. If you need a symmetric cipher [Recipe 7.4] or to sign the files [Recipe 7.13], avoid this method, as you’d be prompted for your password/passphrase for each file processed.

See Also

gpg(1), find(1), tar(1).

Get Linux Security Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.