Compress and encrypt a directory with tar and gpg command

  1. Compress and encrypt a directory
tar -czf - myfolder/ | gpg --symmetric --cipher-algo AES256 -o myfolder.tar.gz.gpg


tar -czf - — compresses the directory and sends the archive to stdout.
gpg --symmetric — encrypts using a password.
--cipher-algo AES256 — uses AES-256 encryption.
-o — specifies the output filename.

2. Decrypt and extract

gpg --decrypt myfolder.tar.gz.gpg | tar -xzf -


#GPG will request the password if it isn't already cached by gpg-agent.
@home:~$ tar -czf - bluetooth-status.sh | gpg --symmetric --cipher-algo AES256 -o  blue.tar.gz

@home:~$ gpg --decrypt blue.tar.gz | tar -tzf -
gpg: AES256.CFB encrypted data
gpg: encrypted with 1 passphrase
bluetooth-status.sh

@home:~$ gpg --decrypt blue.tar.gz 
gpg: AES256.CFB encrypted data
gpg: encrypted with 1 passphrase
��AK�0��)ު��P��v���	��7;��)�F��!�Ûѱ]"��������u:�\������	/��C�Y*��A2�L}�I�I�'牐�����?wK�>��;��O]�fe��ʢ��N׆'��y�V6$���zM��j�J
                                                                                                                                   )��Z~�����UD�a6"���b���\��d9��;E����$m6C���
                                                                                                                                                                               �V�R�C�~E�p*?�r�k_g��v����G�Ҵ\To��9{$�hܶ�go�����wtbto(
  • Or you can just encrypt the file using gpg command
gpg --symmetric --cipher-algo AES256 myfile.txt

gpg --output myfile.txt --decrypt myfile.txt.gpg

Published by

Leave a Reply

Your email address will not be published. Required fields are marked *