LinuxCommandLibrary

basenc

Encode/decode data using various base encodings

TLDR

Encode a file with base64 encoding

$ basenc --base64 [path/to/file]
copy

Decode a file with base64 encoding
$ basenc [[-d|--decode]] --base64 [path/to/file]
copy

Encode from stdin with base32 encoding with 42 columns
$ [command] | basenc --base32 [[-w|--wrap]] 42
copy

Encode from stdin with base32 encoding
$ [command] | basenc --base32
copy

SYNOPSIS

basenc [OPTION]... [FILE]
Reads from FILE or stdin, writes to stdout.

PARAMETERS

-d, --decode
    Operate in decode mode

-f, --format=FORMAT
    Select format: base64 (default), base64url, base32, base32hex, base16, z85

-i, --ignore-garbage
    Ignore non-alphabet characters when decoding

-w, --wrap=COLS
    Wrap lines after COLS chars (default 76; 0 to disable)

--zero
    Input ends with NUL byte

--help
    Display help and exit

--version
    Output version info and exit

DESCRIPTION

basenc is a versatile GNU coreutils utility for encoding and decoding binary data into text using various base encodings. It supports base64 (default), base64url, base32, base32hex, base16 (hexadecimal), and z85 formats.

Primarily used for safely embedding binary data in text-based protocols like email (MIME), JSON, URLs, or configuration files. It reads from standard input or files and outputs to standard output, making it ideal for piping in scripts.

In encode mode (default), it converts bytes to an ASCII-safe string. Decode mode (-d) reverses this. Features include line wrapping for readability (-w), ignoring invalid characters during decoding (-i), and handling NUL-terminated input (--zero).

Unlike older tools like base64, basenc unifies multiple formats in one command, reducing toolbox clutter. It's efficient for cryptographic data, file transfers, and data serialization.

CAVEATS

Treats input as raw bytes; no automatic charset conversion. Decode fails strictly without -i on malformed input.

EXAMPLES

Encode file to base64: basenc file.bin
Decode: basenc -d encoded.txt
Base32 with no wrap: basenc -f base32 -w 0 data
Pipe binary: cat image.png | basenc | basenc -d > image.png

HISTORY

Introduced in GNU coreutils 8.21 (2014) to consolidate base encoding tools like base64 into a multi-format command.

SEE ALSO

base64(1), uuencode(1), hexdump(1)

Copied to clipboard