In this article, I will take you through different examples to perform base64 Encoding and Decoding Using Linux Command Line but before that let me ask you a quick question - Do you know where exactly base64 encoding and decoding is required ? Well, the first and foremost thing you need to understand is that encoding and decoding is not the same as encryption and decryption. Base64 encoding are mostly used to encode data into ASCII format so that it can be stored or transferred very easily. At the receiving end, it can then be decoded back to the original data. We will see some real world examples to perform base64 Encoding and decoding in below section. More about base64 utility.
base64 [OPTION]... [FILE]
How to Perform base64 Encoding and Decoding Using Linux Command Line
Example 1: How to do base64 Encoding of Data
If you have some sample data to encode then you can pass it to base64 utility using
echo "cyberithub.com" | base64 command and get the encoded output as shown below.
root@localhost:~# echo "cyberithub.com" | base64 Y3liZXJpdGh1Yi5jb20K
Example 2: How to do base64 Encoding of a File Data
If you have a text file with some data like below then you can pass this file as an argument to base64 utility to encode the contents.
root@localhost:~# nano example.txt Hi, This is from CyberITHub
You just need to use
base64 example.txt command to encode the contents of
example.txt file as shown below.
root@localhost:~# base64 example.txt SGksIFRoaXMgaXMgZnJvbSBDeWJlcklUSHViCg==
Example 3: How to do base64 Decoding of Data
If you have sent the data or stored it somewhere in encoded format and now you want to decode it back to the original data then you need to pass that encoded data to base64 utility with
--decode option enabled as shown below. You will be able to see the original data on the output.
root@localhost:~# echo Y3liZXJpdGh1Yi5jb20K | base64 --decode cyberithub.com
Example 4: How to do base64 Decoding of a File Data
Similarly, if you have sent any file or stored it somewhere in encoded format and now you are looking to decode it back to the original data then you need to pass the encoded data to base64 utility as shown below.
root@localhost:~# echo "SGksIFRoaXMgaXMgZnJvbSBDeWJlcklUSHViCg==" | base64 --decode Hi, This is from CyberITHub
Example 5: How to check base64 version
If you want to check the current installed version of base64 utility then you need to use
base64 --version command. As you can see from the below output, current version is
root@localhost:~# base64 --version base64 (GNU coreutils) 8.30 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. Written by Simon Josefsson.
Example 6: How to Ignore Non-Alphabet Characters
If you want to ignore Non-Alphabet Characters during decoding then you need to use
-i option with base64 utility as shown below.
root@localhost:~# echo "SGksIFRoaXMgaXMgZnJvbSBDeWJlcklUSHViCg==" | base64 --decode -i Hi, This is from CyberITHub
You can also check all the options available with base64 command using
base64 --help command.
root@localhost:~# base64 --help Usage: base64 [OPTION]... [FILE] Base64 encode or decode FILE, or standard input, to standard output. With no FILE, or when FILE is -, read standard input. Mandatory arguments to long options are mandatory for short options too. -d, --decode decode data -i, --ignore-garbage when decoding, ignore non-alphabet characters -w, --wrap=COLS wrap encoded lines after COLS character (default 76). Use 0 to disable line wrapping --help display this help and exit --version output version information and exit The data are encoded as described for the base64 alphabet in RFC 4648. When decoding, the input may contain newlines in addition to the bytes of the formal base64 alphabet. Use --ignore-garbage to attempt to recover from any other non-alphabet bytes in the encoded stream. GNU coreutils online help: <https://www.gnu.org/software/coreutils/> Full documentation at: <https://www.gnu.org/software/coreutils/base64> or available locally via: info '(coreutils) base64 invocation'