Base64 Encode Decode Cheatsheet: Windows, Linux, Mac Command Examples

Encode and decode files to Base64 on Windows, Linux, and Mac.

Page content

This cheatsheet shows how to encode and decode files and strings to Base64 from the command line on Windows, Linux, and macOS — no extra tools required.

bearded man is doing some advanced coding at night

This guide is part of Developer Tools: The Complete Guide to Modern Development Workflows.

Base64 converts binary data into ASCII text using 64 characters (A–Z, a–z, 0–9, +, /). It is widely used for encoding credentials in HTTP Basic Auth, JWT payloads (see Decode and print JWT token), Kubernetes secrets, data URIs in HTML/CSS, and embedding binary data in JSON or environment variables. Using the command line keeps sensitive data local and is scriptable in CI/CD pipelines.

Base64 Encode Decode on Windows

Windows does not ship a dedicated base64 command, but you can use built-in tools.

Using Command Prompt with Certutil

  1. Open Command Prompt.

  2. Encode a file to Base64:

    certutil -encode input.bin output.b64
    

    Replace input.bin with your file path and output.b64 with the desired output filename.

  3. To print Base64 to the console without saving a temporary file:

    certutil -encode input.bin tmp.b64 && findstr /v /c:- tmp.b64 && del tmp.b64
    

    findstr /v /c:- removes the -----BEGIN CERTIFICATE----- and -----END CERTIFICATE----- lines that certutil adds.

Decode Base64 on Windows (Certutil)

certutil -decode encoded.b64 decoded.bin

Using PowerShell to Encode and Decode

Encode a file:

[Convert]::ToBase64String([System.IO.File]::ReadAllBytes("C:\path\to\file.bin"))

Encode a string (UTF-8):

[Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("Hello, World!"))
# Output: SGVsbG8sIFdvcmxkIQ==

Decode Base64 to string:

[System.Text.Encoding]::UTF8.GetString([Convert]::FromBase64String("SGVsbG8sIFdvcmxkIQ=="))
# Output: Hello, World!

Decode Base64 to file:

[System.IO.File]::WriteAllBytes("decoded.bin", [Convert]::FromBase64String((Get-Content encoded.b64 -Raw)))

These methods work without installing additional software. For more shell automation, see the PowerShell Cheatsheet.

Base64 Encode Decode on Linux

The base64 command is pre-installed on most Linux distributions (GNU coreutils).

Basic Syntax

base64 [OPTIONS] [FILE]

With no file or when file is -, it reads from standard input.

Encode a File to Base64

base64 input_file > output_file

Example:

base64 myfile.txt > encoded.txt

To print encoded content to the terminal:

base64 myfile.txt

Encode a String to Base64

echo -n "Hello, World!" | base64
# Output: SGVsbG8sIFdvcmxkIQ==

Use -n with echo to avoid a trailing newline. For decoding strings in JWTs, see Decode and print JWT token.

Disable Line Wrapping (-w 0)

By default, base64 wraps output at 76 characters. For JSON, environment variables, URLs, or Terraform base64encode usage, disable wrapping:

base64 -w 0 input_file > output_file

Decode Base64 on Linux

base64 -d encoded_file > decoded_file
# or
base64 --decode encoded_file > decoded_file

Example:

echo "SGVsbG8sIFdvcmxkIQ==" | base64 -d
# Output: Hello, World!

Alternative: OpenSSL

echo -n "data" | openssl base64 -e
echo "ZGF0YQ==" | openssl base64 -d

For more command-line workflows, the Bash Cheat Sheet covers piping and scripting.

Base64 Encode Decode on macOS

macOS includes a base64 command similar to Linux, but it requires the -i flag for file input.

Encode a File to Base64

base64 -i input_file > output_file

Example:

base64 -i myfile.txt > encoded.txt

To print to the terminal:

base64 -i myfile.txt

Encode and Copy to Clipboard

base64 -i input_file | pbcopy

Decode Base64 on Mac

base64 -D -i encoded_file > decoded_file

The -D flag enables decode mode; -i specifies the input file. These commands work on all modern macOS versions starting from 10.7.

Linux base64 Command Reference

Full help output for the GNU base64 tool:

$ base64 --help

Usage: base64 [OPTION]... [FILE]
Base64 encode or decode FILE, or standard input, to standard output.

With no FILE, or when FILE is -, read standard input.

Mandatory arguments to long options are mandatory for short options too.
  -d, --decode          decode data
  -i, --ignore-garbage  when decoding, ignore non-alphabet characters
  -w, --wrap=COLS       wrap encoded lines after COLS character (default 76).
                          Use 0 to disable line wrapping
      --help        display this help and exit
      --version     output version information and exit

The data are encoded as described for the base64 alphabet in RFC 4648.
When decoding, the input may contain newlines in addition to the bytes of
the formal base64 alphabet.  Use --ignore-garbage to attempt to recover
from any other non-alphabet bytes in the encoded stream.
...

Common Use Cases

Use case Command / approach
JWT header/payload Decode with base64 -d or jq — see Decode and print JWT token
HTTP Basic Auth echo -n "user:pass" | base64 -w 0 for Authorization: Basic <result>
Kubernetes secret kubectl create secret generic mysecret --from-literal=key=$(base64 -w 0 file.bin)
cURL with Basic Auth Use -u user:pass or -H "Authorization: Basic $(echo -n user:pass | base64 -w 0)" — see cURL Cheatsheet
Data URI in HTML data:image/png;base64,$(base64 -w 0 image.png)