base64 Command in Linux with Examples

Written by: Yonghua Peng   |   Last updated: October 24, 2023

As with all binary-to-text encoding schemes, base64 is designed to carry data stored in binary formats across channels that only reliably support text content. It is particularly prevalent on the World Wide Web where one of its uses is the ability to embed image files or other binary assets inside textual assets such as HTML and CSS files.

The base64 command in Linux is a utility for encoding and decoding files or standard input/output data in the Base64 format. It is widely used for sending e-mail attachments. This is required because SMTP - in its original form - was designed to transport 7-bit ASCII characters only. This encoding causes an overhead of 33–37% (33% by the encoding itself; up to 4% more by the inserted line breaks).

Most modern Linux systems come with the pre-installed base64 command, which uses different parameters to perform encode and decode operations.

Syntax

The basic syntax for the base64 command is as follows:

base64 [OPTION]... [FILE]

Base64 has two useful operations in practical work. One is encoding (the default behavior); another is decoding by providing ‘-d’ parameter.

Encode examples

1. Encode a string

In your Linux, just open a terminal and run:

$ base64

It will prompt you to input something.  Now let’s input a string following with a new line, then input “ctrl+D” for termination.

$ base64
abc111

YWJjMTExCg==

As you see above, “YWJjMTExCg==” is an encoded string by base64 for the original string “abc111”.

We can use HereDoc to input strings as well. See the following sample.

$ base64 <<EOF
> Hello
> welcome to linux world.
> EOF

SGVsbG8Kd2VsY29tZSB0byBsaW51eCB3b3JsZC4K

We can also input a string from the shell pipe like the following.

$ echo "I love linux" |base64
SSBsb3ZlIGxpbnV4Cg==

2. Encode text files

We can encode a text file directly by providing the filename as the unique parameter. For example,

$ cat sample.txt 
123
abc
xyz

$ base64 sample.txt 
MTIzCmFiYwp4eXoK

The content in the file ‘sample.txt’ was encoded into a string “MTIzCmFiYwp4eXoK”.

3. Encode an image

We can encode an image by providing the image name directly. For example,

$ base64  test.png 
iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAMAAAAJbSJIAAAAn1BMVEX/zgD///8AAAD/0QDOpgDT
qwD/ywDwwgD/ygAYEwDtvwD/zwDzxAAMCgASDgAUEAAIBgD/8sr/6aX//fUbFgD/78MXEwD/+eT/
1gD/883/9dT/6qj/4YH/2Vb/9tn//O8fGQD/44//1UX/0R7/77w8MQD/1Dj/3GT/3nb/6J7/3m0l
…

Here we can use ‘-w 0’ as arguments to disable line wrapping. It will become:

$ base64 -w 0 test.png 
iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAMAAAAJbSJIAAAAn1BMVEX/zgD///8AAAD/0QDOpgDTqwD/ywDwwgD/ygAYEwDtvwD/zwDzxAAMCgASDgAUEAAIBgD/8sr/6aX//fUbFgD/78MXEwD/+eT/1gD/883/9dT/6qj/4YH/2Vb/9tn//O8fGQD/44//1UX/0R7/77w8MQD/1Dj/3GT/3nb/6J7/3m0lHgC+mQD…

Now let’s write a bash CGI script to display an image which is base64 encoded.

#!/bin/bash
echo "Content-type: text/html"
echo

IMAGE_BASE64="data:image/png;base64,$(base64 -w 0 /home/pyh/test.png)"

cat <<EOF
<!DOCTYPE html>
<html>
  <head>
    <title>Display Image</title>
  </head>
  <body>
    <img src="$IMAGE_BASE64">
  </body>
</html>
EOF

As you see above, the image “/home/pyh/test.png” was encoded to the base64 string for HTML output.

Decode examples

1. Decode a string

Decoding a base64 string is as simple as providing a ‘-d’ parameter. For example,

$ base64 -d
YWJjMTExCg==

abc111

As you see above, the input string “YWJjMTExCg==” is now decoded to “abc111”, which is the original string.

We can input the string from a shell pipe as well. For example,

$ echo YWJjMTExCg== |base64 -d
abc111

2. Decode text files

Given the case we have a text file “base64.txt” which has base64 encoded strings. Now we decode it back to the original text as follows.

$ cat base64.txt 
MTIzCmFiYwp4eXoK

$ base64 -d base64.txt 
123
abc
xyz

As you see above, providing the command with ‘-d’ parameter and filename will decode the content in the file easily.

3. Decode an image

Given the case you have encoded an image with base64 as follows.

$ base64 test.png > image.base64

Now you can decode it back to the original image by following.

$ base64 -d image.base64 > test2.png

$ diff test.png test2.png

As you see above, first we decode the file back to image. Then we use diff to check if the newly generated “test2.png” has the same content as the original “test.png”.

About The Author

Yonghua Peng

Yonghua Peng Profile picture

Yonghua Peng is a tech lead focusing on cloud computing, big data and DevOps. He have 15 years of experience in Linux development and operations. He is also the author of three technical books.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment