TechEarl

How to Base64-Encode or Decode a File From the Command Line

Base64-encode and decode a file from the terminal with the built-in base64 command. The macOS-vs-GNU gotchas (macOS wants -i or stdin instead of a bare filename, and the -D vs -d decode flag on older Macs), the line-wrapping difference, and building a data URI from an image.

Ishan KarunaratnePublished ⏱️ 7 min readUpdated
Share thisCopied
Encode and decode a file with the base64 command. The macOS vs GNU gotchas (bare filename vs -i, the -D vs -d decode flag), 76-column wrapping, and an image data URI in one line.

The quickest way to Base64-encode a file from the command line is the built-in base64 tool, which ships with both macOS and every Linux distribution. Encode a file to a text blob, decode it back, or one-line it straight into a data URI:

bash
# Encode a file to Base64, GNU/Linux (the file is a plain argument):
base64 in.png > out.txt

# Encode a file to Base64, macOS (the file needs -i):
base64 -i in.png > out.txt

# Decode it back to the original binary (works on both):
base64 --decode < out.txt > in.png

That filename difference is the thing that trips people up, so I am putting it up front: macOS base64 rejects a bare filename with base64: invalid argument in.png and only reads from -i or stdin. Everything else below is the detail: the decode flag (-d vs -D) and how it changed on macOS, the line-wrapping difference between the two base64 implementations, building a complete data: URI from an image, encoding a short string instead of a file, and why none of this is encryption.

Base64 is encoding, not encryption. It scrambles nothing. Anyone with the blob can run base64 -d and get your file back. It exists to carry binary data safely through text-only channels (email, JSON, a CSS file, an env var), not to hide it. If you need secrecy, encrypt first (gpg, age, openssl) and Base64 the ciphertext if you also need it text-safe.

The macOS vs GNU decode flag (the cross-platform gotcha)

There are two base64 programs in the wild, and they disagree on how you name the input file and, on older Macs, on which letter means decode:

TaskGNU coreutils (Linux)BSD (macOS)
Encode a filebase64 in.pngbase64 -i in.png (a bare base64 in.png is an error)
Decodebase64 -d (or --decode)base64 -D, -d, or --decode on macOS 14 and later; -D or --decode on older macOS
Read from stdinbase64 < in.pngbase64 < in.png
One unwrapped linebase64 -w 0 in.pngwrapping is off by default

The input file is the difference that still bites. GNU base64 takes the file as a plain argument. The macOS base64 does not: base64 in.png stops with base64: invalid argument in.png and prints the usage text, because the macOS tool reads only from -i in_file or from stdin. That is the behaviour on macOS 26.7, and Apple's source for the tool rejects any leftover argument the same way. So a Linux command with the filename on the end fails on a Mac whatever flag it uses.

The decode flag used to be the trap, and on current macOS it no longer is. On Linux decode is a lowercase -d. The base64 that ships with macOS 14 Sonoma and later accepts -d, -D, and --decode, and all three decode. On older macOS the man page lists uppercase -D as decode and lowercase -d as --debug (verbose log messages), so a -d copied from Linux did not decode there. GNU base64 has never accepted -D. The good news: --decode (the long form) and stdin work on both, so if you want one command that runs unchanged on a Mac and a Linux box, write it with the long flag and redirect the file in:

bash
# Portable encode and decode, work on macOS and GNU/Linux:
base64 < in.png > out.txt
base64 --decode < out.txt > in.png

macOS also gives you explicit input and output flags, which read nicely in a script and avoid shell redirection:

bash
# macOS only: -i input file, -o output file
base64 -i in.png -o out.txt          # encode
base64 -D -i out.txt -o in.png       # decode

Those -i/-o flags are a macOS thing. GNU base64 has no -o, and its -i means --ignore-garbage rather than "input file", so keep them out of anything you need to run on Linux.

The line-wrapping difference

Run the same encode on a Mac and on a Linux box and you can get visibly different output. GNU base64 wraps the encoded text at 76 columns by default, inserting a newline every 76 characters. BSD base64 on macOS does not wrap at all by default, so you get one long line.

The wrapping matters when you feed the Base64 somewhere that hates newlines, such as a data URI, an HTTP header, a Kubernetes secret, or an env var. To force a single unbroken line:

bash
# GNU/Linux: disable wrapping with -w 0
base64 -w 0 in.png

# macOS BSD: already one line by default; if you want to be explicit, -b 0
base64 -b 0 -i in.png

Note the flag letters differ again: GNU uses -w (wrap), BSD uses -b (break). Current macOS also accepts -w for GNU compatibility, but older releases do not. Going the other way, if you have a wrapped blob and need it on one line, strip the newlines with tr:

bash
base64 < in.png | tr -d '\n'

That tr -d '\n' trick is the portable equalizer. It works regardless of which base64 produced the input, so I reach for it in scripts that have to run on both platforms.

Build a data URI from an image

A data URI inlines an image directly into HTML or CSS as data:<mime>;base64,<data>, which saves a request for small assets. The whole thing is one line once you have the unwrapped Base64:

bash
# GNU/Linux:
echo "data:image/png;base64,$(base64 -w 0 in.png)"

# macOS (already unwrapped, so no -w needed):
echo "data:image/png;base64,$(base64 -i in.png)"

Mind the MIME type: image/png for PNG, image/jpeg for JPEG, image/svg+xml for SVG, image/webp for WebP. The MIME prefix is not optional. A browser will not sniff it from the bytes inside a data URI, so data:image/png;... on a JPEG renders nothing.

Keep data URIs for genuinely small images. Base64 inflates the byte count by roughly 33 percent, and inlining a large image bloats the HTML or CSS that carries it (and that file usually is not cached as aggressively as a standalone image would be). For anything beyond an icon or a tiny background, ship the real file. If you are converting images on the command line anyway, convert an image to WebP from the command line first, then inline the smaller result.

Encode or decode a short string

For a quick string rather than a file, pipe echo into base64. There is one caveat that bites everyone at least once: echo appends a trailing newline, and base64 encodes that newline as part of the data. So echo "hello" | base64 is encoding hello\n, not hello. Use echo -n (or printf) to suppress it:

bash
# Wrong: encodes "hello\n" (the trailing newline is included)
echo "hello" | base64            # aGVsbG8K

# Right: encodes exactly "hello"
echo -n "hello" | base64         # aGVsbG8=
printf '%s' "hello" | base64     # aGVsbG8=  (printf adds no newline at all)

I prefer printf '%s' because echo -n is not portable to every shell, whereas printf behaves the same everywhere. Decoding a string uses the same --decode flag as files:

bash
echo "aGVsbG8=" | base64 --decode    # hello   (no trailing newline)

If you are encoding a string only to move it onto your clipboard, pipe it straight through: on macOS, printf '%s' "hello" | base64 | pbcopy puts the encoded text on the clipboard ready to paste. See copy to the clipboard from the command line with pbcopy and pbpaste for that workflow.

Verify a round-trip

Because Base64 is lossless, a decode of an encode should give you back the exact original bytes. After moving a binary through a text channel, confirm nothing got mangled by comparing checksums of the original and the decoded copy:

bash
base64 < in.png > out.txt
base64 --decode < out.txt > roundtrip.png
# Compare hashes; identical means a clean round-trip
shasum -a 256 in.png roundtrip.png

If the two hashes differ, something stripped or rewrote a character in transit (a common cause is a tool that wrapped or unwrapped the lines, or mangled the trailing = padding). See verify a file checksum on macOS for the full hashing flow.

FAQ

Because they are two different programs. Linux ships GNU coreutils base64, which takes the file as an argument. The macOS base64 does not accept a bare filename, so base64 -d out.txt stops with invalid argument out.txt; use base64 -d -i out.txt or redirect the file on stdin. The flag itself is fine on macOS 14 Sonoma and later, where -d, -D, and --decode all decode. On older macOS lowercase -d was the --debug flag and decode was uppercase -D. The long form --decode with the file on stdin works on both, so use that when you want one command that runs everywhere.

No. Base64 is a reversible encoding with no key and no secrecy. Anyone can run base64 --decode and recover the original. It exists to carry binary data through text-only channels, not to protect it. Encrypt first if you need confidentiality, then Base64 the ciphertext if it also has to be text-safe.

GNU base64 wraps at 76 columns by default; BSD base64 on macOS does not wrap. To force a single line, use base64 -w 0 on Linux or base64 -b 0 on macOS, or pipe the output through tr -d with a newline argument to strip line breaks regardless of platform.

You almost certainly encoded with echo rather than echo -n. Plain echo appends a trailing newline that gets encoded as part of the data, so decoding gives it back. Encode with printf '%s' or echo -n to encode exactly the bytes you intended.

Concatenate the MIME prefix with the unwrapped Base64: echo "data:image/png;base64,$(base64 -w 0 in.png)" on Linux, or echo "data:image/png;base64,$(base64 -i in.png)" on macOS, where output is already one line and the file needs -i. Match the MIME type to the file (image/png, image/jpeg, image/svg+xml). Keep this for small assets only, since Base64 adds about a third to the byte count.

See also

Sources

Authoritative references this article was fact-checked against.

Tagsbase64CLImacOSLinuxdata URIencodingterminal

Found this useful? Pass it on.

Copied

Ishan Karunaratne

Systems and Network Architect · Chief Technology Officer

Systems and network architect and Chief Technology Officer with more than two decades designing, building, and running production software, cloud and network architecture, Linux systems, and the bare metal underneath them, and lately working AI into the stack. A US Army veteran who served in Operation Iraqi Freedom. What I write here is drawn from the full arc of that work, across architecture, engineering, and operations, not any single job.

Keep reading

Related posts

Convert SVG to PNG from the command line with rsvg-convert, Inkscape, or ImageMagick (size, density, transparency), plus PSD to PNG with the [0] composite.

How to Convert SVG to PNG From the Command Line

Convert SVG to PNG from the command line with rsvg-convert, Inkscape, or ImageMagick. Set the output size explicitly, keep transparency, and avoid the blurry low-density trap. Plus PSD to PNG with ImageMagick's [0] composite trick.