How to use the command base64 (with examples)
Base64 is a command-line tool that allows encoding or decoding files or standard input using the Base64 algorithm. It is a widely used encoding technique that converts binary data into a readable format, mainly used in email attachments, image transfer on the web, and cryptography.
Use case 1: Encoding the contents of a file and writing the result to stdout
Code:
base64 path/to/file
Motivation: Encoding a file as Base64 is useful when you want to transfer binary data over a text-based protocol, such as email or HTTP. By encoding the file, you ensure that the data remains intact during transportation, regardless of the encoding scheme used by the transport layer.
Explanation: The command base64
is followed by the path to the file you want to encode. The encoded result will be printed to the standard output (stdout).
Example output:
SGVsbG8gd29ybGQh
Use case 2: Decoding the base64 contents of a file and writing the result to stdout
Code:
base64 --decode path/to/file
Motivation: Decoding a base64-encoded file is useful when you receive a file as a base64-encoded string and want to convert it back to its original binary format. This is commonly done when handling files transferred over the network or stored in a database.
Explanation: The base64
command with the --decode
option allows you to decode a file that is encoded in base64. The path to the file you want to decode is provided after the --decode
option. The decoded result will be printed to the standard output (stdout).
Example output:
Hello world!
Use case 3: Encoding from stdin
Code:
somecommand | base64
Motivation: Encoding from stdin is useful when the data you want to encode is generated by another command or process. By piping the output of another command to base64
, you can directly encode the data without the need to store it in a file first.
Explanation: The output of somecommand
is piped into the base64
command using the |
(pipe) symbol. The data from somecommand
is passed through base64
, and the encoded result is printed to the standard output (stdout).
Example output:
JTIzJTVE
Use case 4: Decoding from stdin
Code:
somecommand | base64 --decode
Motivation: Decoding from stdin is useful when you receive a base64-encoded string as input from another command or process. By piping the encoded data to base64 --decode
, you can directly decode the data without the need to store it in a file first.
Explanation: The output of somecommand
is piped into the base64
command with the --decode
option. The encoded data from somecommand
is passed through base64
, and the decoded result is printed to the standard output (stdout).
Example output:
Hello world!
Conclusion:
The base64
command is a versatile tool that allows you to encode or decode files or standard input using the Base64 algorithm. It is particularly useful when working with binary data in a text-based environment, such as when transferring files over email or HTTP. By understanding the different use cases provided in this article, you can effectively use the base64
command to encode and decode data as needed.