Base64 decode from the command linePosted in Linux/Unix/BSD -
I have some automated processes that receive updates via email attachments which are base64 encoded. The entire email is backed up into a text file, and very occasionally I need to check what was in an attachment. The command line base64 tool can help with this, either decoding a file or standard input.
Using base64 to decode a file
The -d or --decode flag tells base64 it's decoding data (on a Mac -d is a debugging flag, so it's -D and --decode instead). In the examples below, I've used --decode, but you can use -d or -D instead.
To decode the file at /tmp/encoded.txt do this:
base64 --decode /tmp/encoded.txt
And the decoded file will be written to standard output. If you wanted to write it to a file, you can redirect it like this, where it's written out to /tmp/decoded.txt
base64 --decode /tmp/encoded.txt > /tmp/decoded.txt
Using base64 to decode some text
If you run base64 --decode without a file, you can type text (or copy and paste it), hit return/enter, and then control+d / ctrl+d and it will be decoded.
So, for example, to decode VGhpcyBpcyBiYXNlNjQgZW5jb2RlZAo=, which is "This is base64 encoded" encoded as base64, do this:
base64 --decode VGhpcyBpcyBiYXNlNjQgZW5jb2RlZAo= [ctrl+d]
It will then be written to standard output. Again, you can redirect the output to a file:
base64 --decode > /tmp/decoded.txt VGhpcyBpcyBiYXNlNjQgZW5jb2RlZAo= [ctrl+d]
Using pbpaste on a Mac
If you have the encoded text in your clipboard, you can use pbpaste to pipe it through base64 instead of having to manually paste in the terminal. There are equivalent commands when using X (xclip) but I've never used these myself.
pbpaste | base64 --decode