- Unix Commands Reference
- Unix Commands - Home
base64 Command in Linux
Base64 is a handy command-line utility in Linux that you can use to encode and decode binary data (such as text, string, or files). You can also this command line tool to securely transfer data over text-based protocols (such as URLs and emails) or store binary data in a text format.
If an ASCII file is transferred over the network and not decoded correctly, it can lead to corruption. This occurs because ASCII files are initially converted from strings to bytes. If those bytes are then incorrectly decoded back to ASCII, your data becomes corrupted.
Base64 was introduced as a solution to convert ASCII data into arbitrary bytes, allowing secure transfer and decoding. In short, base64 encoding ensures data integrity during transmission over the network.
To make it simple, base64 is a technique that converts any data into a random string of characters. It transforms binary data into a secure textual character sequence. This random string of characters is then transmitted over the network.
Table of Contents
Here is a comprehensive guide to the options available with the base64 command −
- Syntax of base64 Command
- base64 Command Options
- Examples of base64 Command in Linux
- Various base64 Modules for Encoding and Decoding
- Resolving Common Problems with the Base64 Command
Syntax of base64 Command
Base64 provides various syntaxes to encode and decode files, strings, text, and more. For instance −
To encode a text file, run the following command in your terminal −
base64 file.txt
To decode the text file, run −
base64 --decode file.txt
This first command encodes the contents of file1.txt using base64 encoding. It reads the data from file.txt, converts it to base64, and prints the encoded output to the terminal.
On the other hand, the second command decodes the contents of encodedfile.txt. It reads the base64-encoded data from encodedfile.txt, decodes it, and prints the original binary data to the terminal. The -d flag specifies the decode operation.
base64 Command Options
The following are options you can include when working with the base64 command −
Options | Description |
---|---|
w, --wrap=COLS |
This option allows you to control line wrapping when encoding data. By default, base64 wraps encoded lines after 76 characters. You can specify a different value for COLS. To disable line wrapping entirely, use base64 "-w" flaw with 0 argument. |
d, --decode | When you use this option, base64 switches to decoding mode. It takes base64-encoded data as input and decodes it back to its original binary form. |
-i, --ignore-garbage |
When decoding, this option tells base64 to ignore non-alphabet characters (characters not part of the base64 encoding). It is useful when dealing with data that might contain additional characters or formatting. |
--version | It outputs version information for the base64 utility. |
Examples of base64 Command in Linux
The base64 command-line utility already comes pre-installed on popular Linux distributions such as Ubuntu, Debian, CentOS, and others. You only need to have sudo or root privileges. You can perform the following operations with the base64 command-line tool.
- Encoding a String with Base64
- Decoding a String with Base64
- Encoding a File with Base64
- Decoding a file with Base64
- Encode with Line Wrapping
- Ignoring Non-Alphabet Characters
Encoding a String with Base64
To encode a string, pipe an echo command into the base64 command-line utility. To ensure also there are no extra hidden characters added, you can include the "-n" flag.
If this flag is not present, you might collect hidden characters that will corrupt your base64 encoding, such as line returns or spaces.
Decoding a String with Base64
To decode a base64-encoded string back to its original form, run the following command in your terminal −
echo -n 'bXktc3RyaW5n' | base64 --decode
If the encoding was not corrupted, this should give you the original string.
Encoding a File with Base64
You can efficiently use the base64 command to encode a file. To encode the content of a file and save the encoded text on another file, run the following command replacing "file1.txt" with your actual path −
Decoding a file with Base64
To decode the encoded text inside a file and keep the decoded text inside another file, use the following syntax −
Encode with Line Wrapping
By default, the base64 command wraps the encoded text after 76 characters. To encode text that contains more than 76 characters, use the "-w" flag with 0 argument. This will encode a long single string without any line break.
Ignoring Non-Alphabet Characters
You can use the base64 command to ignore non-alphabet characters in the input data by including the "-i" or --ignore-garbage flag. When working with data that may contain unexpected or unwanted characters, this can be helpful −
In this example, we will encode a string containing non-alphabet characters −
echo 'This is a text with some &%$@ non-alphabet characters.' | base64 -i
Various base64 Modules for Encoding and Decoding
Various modules like Openssl, Python, and Perl allow you to use base64 to encrypt and decode data. We have highlighted them here with some real-world instances.
Using Python for Encoding and Decoding
Python offers the base64 module for encoding and decoding data. Here is how you can get started −
import base64 # Input string input_string = 'base64 commandline-tool' # Convert the string to bytes using UTF-8 encoding input_bytes = input_string.encode('utf-8') # Encode the bytes to base64 encoded_string = base64.b64encode(input_bytes) # decoding encoded_string_str = encoded_string.decode('utf-8') print(encoded_string_str)
Using Openssl for Encoding and Decoding
OpenSSL is a robust cryptographic software library that improves communication security over computer networks.
To encode a file or string in Base64 using OpenSSL, you can use the following syntax −
To decode an encoded file or string, use the "-d" flag with the base64 command −
Using Perl for Encoding and Decoding
Perl has a core module, MIME::Base64, which provides functions for encoding and decoding base64 strings. To get started with Perl, use the following syntax −
"-ne": This flag tells Perl to read input line by line (-n) and execute the provided command (-e) enclosed in single quotes for each line.
"printf "%s",encode_base64($_)": This command prints the Base64-encoded version of each input line. The encode_base64($_) function encodes the input line ($_) as Base64.
To decode the base64 string, you can use the -d flag with the base64 command or the decode_base64 function in Perl −
Resolving Common Problems with the Base64 Command
While the base64 command is typically easy to use, you may encounter common issues or errors. Let’s explore these problems and learn how to troubleshoot them.
Handling Invalid Input Errors
One specific issue is the "invalid input" error, which occurs when attempting to decode a string that wasn’t correctly base64 encoded1.
If you encounter this error, we’ll discuss how to address it.
To fix this, ensure the input string has the correct padding (usually "=" characters at the end). Let’s add padding to the string −
Now, decode the padded file −
Handling Large Files
You may face performance challenges or memory limitations when working with large files. To solve this issue, consider using the "-w" flag with a value of 0 to disable line wrapping. It reduces memory usage and improves performance.
For instance, here the -w 0 argument tells base64 to disable line wrapping, enhancing performance when encoding large files.
Conclusion
In this tutorial, we explained in detail the different viewpoints regarding encoding and decoding text strings and files using the base64 command. We hope this tutorial helped you in understanding base64 encoding and decoding.