how to encrypt and decrypt a tar, one-liner version
I do love one-liners that I can do on the command line in Linux, and this one has to do with TAR archives.
TAR, on its own, doesn't encrypt anything. This being true, a separate utility has to be used to encrypt it. The easiest one is GPG.
GPG is GNU Privacy Guard, and should already be installed on your Linux system. You can check this with which gpg. If you get a result of something like /usr/bin/gpg, it's already installed. If you get a blank result back, then you need to install it. Installing GPG should readily be in the main repository for your Linux distribution.
Let's say I have a folder called my-folder on the Desktop where I want to put all the files in that folder into a TAR and encrypt it with a password. I launch a Terminal and do this first:
cd ~/Desktop
After that, this:
tar -cvf - my-folder | gpg -c --batch --passphrase mypassword > my-folder.tar.gpg
The above will create the compressed archive my-folder.tar.gpg, and encrypt it using the password mypassword.
If you DO NOT want the archive compressed (which results in faster creation but a larger archive), --compress-algo none needs to be added in, like this:
tar -cvf - my-folder | gpg -c --compress-algo none --batch --passphrase mypassword > my-folder.tar.gpg
Obviously, change mypassword to a password of your choice.
To decrypt and extract the archive, put my-folder.tar.gpg on the Desktop and run this:
cd ~/Desktop;mkdir extract;cat my-folder.tar.gpg | gpg -d --batch --passphrase mypassword | tar -C extract -xvf -
A folder called extract will be created on the Desktop, and the contents of my-folder.tar.gpg will be decrypted and extracted to that folder.
Doing the same thing but with progress bars
When creating/encrypting, change -cvf to -cf and add --checkpoint=.1000 like this:
tar --checkpoint=.1000 -cf - my-folder | gpg -c --batch --passphrase mypassword > my-folder.tar.gpg
The result of this is that while the archive is being created, dots (as in periods) will show indicating progress until completed.
For extraction, pv will need to be installed, which means pipe viewer. This utility should be available in the main repository for your Linux distribution.
Change cat to pv and -xvf to -xf, like this:
cd ~/Desktop;mkdir extract;pv my-folder.tar.gpg | gpg -d --batch --passphrase mypassword | tar -C extract -xf -
During extraction, you'll see the progress bar.
Published 2025 Apr 3