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.


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


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.


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

$ echo "I love linux" |base64

2. Encode text files

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

$ cat sample.txt 

$ base64 sample.txt 

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 

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

$ base64 -w 0 test.png 

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

echo "Content-type: text/html"

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

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

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


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

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 

$ base64 -d base64.txt 

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.



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

Leave a Reply

Leave a Comment