Convert image to base64 in terminal


The following command converts a file to base64 and writes the result to a text file and removes all newlines (parameter "-w 0").

base64 /path/to/file > /path/to/out.txt -w 0

If a file contains the base64 string, let’s assume from a PDF file, the following command prints and decodes it into a PDF file.

cat test.txt | base64 --decode > test.pdf

It’s also possible to encode or decode a string directly in the terminal and print the result using the pipe operator:

echo "Hello, Worlds!" | base64
# Output: SGVsbG8sIFdvcmxkcyEK

echo "SGVsbG8sIFdvcmxkcyEK" | base64 --decode
# Output: Hello, Worlds!

That’s all Folks!