Base64 Linux commands: encode, decode & files
From a quick string to a binary file: copy a command, run it locally, and keep the original bytes intact.
These examples target GNU coreutils on Linux. macOS, BSD and BusyBox can use different options. Check base64 --help on your machine, or see our macOS, JavaScript and Python cheat sheet. The commands below are static examples; this website does not run them or insert your converter input into them.
Everyday commands
Encode text, one line
printf '%s' 'Hello, world!' | base64 --wrap=0printf adds no newline to the input. --wrap=0 keeps the encoded output on one line.
Output: SGVsbG8sIHdvcmxkIQ==
Decode Base64 text
printf '%s' 'SGVsbG8sIHdvcmxkIQ==' | base64 --decodeFor known text input. A prompt may appear on the same line because the decoded bytes contain no trailing newline.
Output: Hello, world!
Encode a file
base64 --wrap=0 sample.bin > sample.b64Reads the original bytes, including binary data. Replace sample.bin with your local filename.
Decode to a file
base64 --decode sample.b64 > restored.binWrites original bytes to disk. Use a different output filename; > replaces any existing output file. A failed decode can leave partial output.
Preserve UTF-8 text
printf '%s' 'Hello, 世界 🌍' | base64 --wrap=0In a UTF-8 terminal, non-Latin text and emoji are encoded as UTF-8 bytes. Base64 does not convert character encodings.
Output: SGVsbG8sIOS4lueVjCDwn4yN
Encode padded Base64URL
printf '%s' 'Hello?!!' | basenc --base64url --wrap=0Requires GNU basenc. Uses - and _ instead of + and /, and retains required = padding.
Output: SGVsbG8_ISE=
Decode Base64URL
printf '%s' 'SGVsbG8_ISE=' | basenc --base64url --decodeUse padding when required by the byte length. An unpadded JWT segment may need padding restored before GNU basenc can decode it.
Output: Hello?!!
Verify a file round trip
base64 --wrap=0 sample.bin > sample.b64 &&
base64 --decode sample.b64 > restored.bin &&
cmp sample.bin restored.binEach step runs only if the previous command succeeds. cmp exits with status 0 and prints nothing when the files match. Use disposable output filenames.
Why does my output differ?
- An unexpected ending, such as Cg==
echonormally appends a newline. That newline becomes part of the encoded bytes. Useprintf '%s' 'your text'when you want the text alone. Base64 endings depend on the entire input length, so a newline will not always produce the same ending.- Output is split into lines
- GNU base64 wraps encoded output at 76 characters by default. Use
--wrap=0for a single line. GNU decoding accepts line breaks; our Standard preset is stricter. Choose appropriate custom whitespace rules when pasting wrapped output into the converter. - “invalid input” when decoding
- Check the alphabet, required padding and whether you copied a data URL prefix or a whole JWT. Use
basenc --base64url --decodefor the URL-safe alphabet. Do not hide malformed input with--ignore-garbageas a default fix. A nonzero exit status means decoding failed, even if some bytes were already printed. - JWT segments have no = padding
- JWS uses unpadded Base64URL. Remove trailing
=only when the receiving protocol calls for it. When restoring padding, divide the segment length by 4. A remainder of 2 needs two equals signs, 3 needs one, and 0 needs none; remainder 1 is invalid. The JWT segment preset handles unpadded segments. Decoding does not verify a signature or claims. - Decoded output looks garbled
- Base64 represents bytes, which may be an image, archive or non-UTF-8 text. Decode to a file rather than printing unknown binary data into your terminal. Encoding is not encryption.
Handle files and secrets carefully
Quote filenames containing spaces, such as base64 --wrap=0 'my file.bin' > 'my file.b64'. Never use the same input and output path: the shell can truncate the file before the command reads it. Avoid putting real credentials in shell history, command arguments or logs. For sensitive files, use restrictive permissions and an approved local workflow.
Official references
These recipes follow the GNU base64 manual and GNU basenc manual. For MIME, PEM and protocol-specific rules, see Base64 standards and presets.