Bash is a Unix shell and command language. These bash commands are widely used in Mac OS X and Unix-like operating systems for interacting with the system via commands. Bash also has a utility command, Base64
, for encoding and decoding data.
Base64 is an encoding scheme that represents binary files in a textual format.
This tutorial teaches you the different methods to decode Base64 data into a file in Unix-like operating systems using commands and online tools.
The base64 command, with the flag --decode
, decodes the base64 data into a string.
It is available since the Mac OS X Version 10.7 and all UNIX-like operating systems. If you’re using a Mac OS X version older than 10.7, check the OpenSSL Base64 explained in the next section of this tutorial.
To decode base64 data into a file,
-d
parameter. It’ll decode base64 encoded data>
and pass the file name with an extension. You need to pass the correct extension of the file equivalent to the Base64 string. Otherwise, the output file will be corrupted due to the incorrect extension
Important: Ensure you have write access to the current working directory needed to create the new output file.
Code
The following code demonstrates creating an image file from the Base64 encoded string.
base64 -d <<< "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" > image.png
OpenSSL is a software library that enables secure network communication applications. Base64 is commonly used as a primary encoding when transmitting information over a network. OpenSSL offers a command-line tool to facilitate the process of encoding or decoding strings using Base64 encoding.
Use this method when your system doesn’t have the Base64 command line utility. For example, Mac OS versions earlier than 10.7.
To decode a base64 string into a file using the OpenSSL base64 command line utility,
openssl base64
command with the -d
parameter-out
parameter and pass the file name with an extension. You need to pass the correct extension of the file equivalent to the Base64 string
Important: Ensure you have write access to the current working directory needed to create the new output file.
Code
$ openssl base64 -d <<< "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" -out image.png
You can use the Online Base64 Decoder Tool in the browser to decode the Base64 string into a file.
To decode a Base64 string into a file using the tool,
.txt
file and upload it to the decoder tool
The main advantage of using the online Base64 decoder tool is you do not need to know the extension of the file types. For example, the image file can be in different formats such as .jpg
, .png
or a .gif
. The online tool will detect the file extension automatically based on the input string and create a file equivalent to the string with the proper file extension.
You’ve learned to decode a Base64 string into a file using different methods. Each method is applicable in different use cases.
For example, the command line will be helpful when you want to write a script to decode a Base64 string into a file multiple times programmatically. For one-time use, you can use the Online tool and easily download the decoded file. You can select the method to decode based on your use case of the application.