Home Command line utilities An Easy Way To Encrypt And Decrypt Files From Commandline In Linux

An Easy Way To Encrypt And Decrypt Files From Commandline In Linux

By sk
Published: Last Updated on 6.7k views

Today, we are going to see how to encrypt and decrypt files from command line in Linux using a free utility named GNU Privacy Guard (shortly GPG or GnuPG). It is a command line encryption and signing tool to secure files. We can easily encrypt the important and confidential files and documents using GPG and send/receive them over Internet. There are, of course, plethora of methods, applications, tools, and utilities are available to encrypt and decrypt files in Linux. But, I find this method is the easiest one.

Encrypt And Decrypt Files Using GnuPG From Commandline In Linux

GnuPG should have come pre-installed by default in most Linux distributions. If it is not, you can install it as shown below.

On Arch Linux and its derivatives:

$ sudo pacman -S gnupg

On Debian, Ubuntu, Linux Mint:

$ sudo apt-get install gnupg

On RHEL, CentOS:

$ sudo yum install gnupg

On Fedora:

$ sudo dnf install gnupg


$ sudo zypper in gnupg

You can check the installed GPG version at any time using command:

$ gpg --version

Sample output:

gpg (GnuPG) 2.2.12
libgcrypt 1.8.4
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Home: /home/sk/.gnupg
Supported algorithms:
Hash: SHA1, RIPEMD160, SHA256, SHA384, SHA512, SHA224
Compression: Uncompressed, ZIP, ZLIB, BZIP2

Once it is installed, we can encrypt a file as shown below.

$ gpg -c Inter.txt

Enter your passphrase twice and click OK to encrypt the file. Replace Inter.txt file with your own filename.

Let us verify it using ls command:

$ ls

Sample output:

dde-dock_1000 expandstate Inter.txt.gpg syndaemon.pid
dde-launcher_1000 Inter.txt pulse-QiMoBZPcQcby wpa_ctrl_439-1

As you noticed the Inter.txt file has been encrypted and saved in the current directory as Inter.txt.gpg.

Once a file encrypted, You can't view the contents of thst file without knowing the passphrase.

If you try to open it using any editor, the contents of the GPG file should look like some random numbers and symbols.

AZ^Hb\215z̬\256D\243\202\365\362\326V\272^HX ^X\265\224h\262f\225ȹ< /\266\306
^YMJ\200\2030f?^Q\244Q\3376\341.\274\361Ⱦ(i\345,\304\315%#\370/\232/j a^T\205`E\

To decrypt the file, just run:

$ gpg -o Inter.txt -d Inter.txt.gpg

It will ask you to enter the passphrase. Just enter the correct passphrase, select Ok and hit Enter..

Decrypt files using GnuPG

That's it, Now you can view the decrypted file's content using any editor of your choice.

As you can see, encrypting and decrypting files using GnuPG is a piece of cake. You don't need to be a command line ninja or an experienced Linux admin to do this.

To know more details about GnuPG, run:

$ gpg -h

Or, refer the man pages:

$ man gpg

Hope this helps.


Thanks for stopping by!

Help us to help you:

Have a Good day!!

You May Also Like

1 comment

art July 2, 2020 - 2:10 pm

Thanks. I want to become a ninja someday.


Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More