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:
# 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.pngThat 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 -dand 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:
| Task | GNU coreutils (Linux) | BSD (macOS) |
|---|---|---|
| Encode a file | base64 in.png | base64 -i in.png (a bare base64 in.png is an error) |
| Decode | base64 -d (or --decode) | base64 -D, -d, or --decode on macOS 14 and later; -D or --decode on older macOS |
| Read from stdin | base64 < in.png | base64 < in.png |
| One unwrapped line | base64 -w 0 in.png | wrapping 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:
# Portable encode and decode, work on macOS and GNU/Linux:
base64 < in.png > out.txt
base64 --decode < out.txt > in.pngmacOS also gives you explicit input and output flags, which read nicely in a script and avoid shell redirection:
# macOS only: -i input file, -o output file
base64 -i in.png -o out.txt # encode
base64 -D -i out.txt -o in.png # decodeThose -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:
# 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.pngNote 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:
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:
# 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:
# 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:
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:
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.pngIf 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
- Copy to the clipboard from the command line with pbcopy and pbpaste: pipe Base64 straight onto the clipboard, plus the Linux and Windows equivalents.
- Verify a file checksum on macOS: confirm a Base64 round-trip is byte-for-byte identical with shasum.
- Convert an image to WebP from the command line: shrink an image before you inline it as a data URI.
Sources
Authoritative references this article was fact-checked against.
- base64(1): Mac OS X 10.7 man page (older macOS: -D decode, -d debug, -b break)unix.com
- base64, macOS command reference (ss64)ss64.com
- base64 invocation, GNU coreutils manual (-d decode, -w wrap)gnu.org
- base64(1): Linux man page (man7.org)man7.org
- bintrans(1), base64: current macOS man page (-d, -D, --decode all decode)keith.github.io
- Apple text_cmds: bintrans.c, the macOS base64 option handlinggithub.com



![Set the Date and Time From the macOS Command Line (BSD date) Set the macOS clock from Terminal with the BSD date command: MMDDhhmm[[CC]YY], not date -I (an ISO 8601 output flag that sets nothing). Turn off network time first or macOS resyncs.](https://images.techearl.com/mac-set-date-command-line/mac-set-date-command-line-1536.webp?v=2026-11-25T10%3A00%3A00-05%3A00)

![Convert SVG to PNG (and PSD to PNG) From the Command Line 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.](https://images.techearl.com/convert-svg-to-png-command-line/convert-svg-to-png-command-line-1536.webp?v=2026-11-25T10%3A00%3A00-05%3A00)