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]
basenc --base16 [OPTION]... [FILE]
basenc --base32 [OPTION]... [FILE]
basenc --base64 [OPTION]... [FILE]
basenc --base85 [OPTION]... [FILE]
basenc --z85 [OPTION]... [FILE]

PARAMETERS

--base16, -A
    Selects base16 encoding/decoding. Base16 uses hexadecimal digits (0-9, A-F).

--base32, -b
    Selects base32 encoding/decoding. Base32 uses a 32-character alphabet, commonly for case-insensitive names.

--base64, -c
    Selects base64 encoding/decoding. This is the default encoding if no other base option is specified.

--base85, -Z
    Selects base85 encoding/decoding, also known as Ascii85. More compact than base64 for arbitrary binary data.

--z85, -z
    Selects Z85 encoding/decoding, a specific variant of base85 with a different alphabet and checksum property.

-d, --decode
    Decodes data. By default, basenc encodes. The input is assumed to be encoded in the specified base format.

-i, --ignore-garbage
    When decoding, ignores non-alphabet characters. This is useful for handling input that may contain formatting or extraneous characters.

-w, --wrap=COLS
    Wrap encoded output lines after COLS characters. Use 0 to disable wrapping. The default for base64 is 76 characters.

-u, --url
    Use URL-safe alphabet for base64 encoding/decoding. This replaces '+' with '-' and '/' with '_', and omits trailing '=' padding for compactness and URL compatibility.

--help
    Displays a help message and exits.

--version
    Outputs version information and exits.

FILE
    The input file to process. If omitted or specified as -, basenc reads from standard input.

DESCRIPTION

basenc is a versatile utility for encoding and decoding binary data into a printable ASCII string format, and vice-versa. Unlike its predecessor base64, basenc offers robust support for multiple base encodings, including base16, base32, base64 (the default), base85, and Z85. This makes it an invaluable tool for tasks such as transmitting binary data over mediums that primarily handle text (like email or URLs), embedding data in configuration files, or simply converting data formats for interoperability. It can read input from standard input or specified files and write to standard output. When decoding, it can optionally ignore non-alphabet characters, which is useful for handling corrupted or formatted input. Its flexibility in choosing encoding schemes makes it a powerful addition to the standard Linux command-line toolkit.

CAVEATS

basenc is a GNU Coreutils extension and might not be available on all Unix-like operating systems (e.g., some BSD variants), although base64 is more widely available. The default wrapping for base64 (76 characters) may need to be adjusted with --wrap=0 for specific applications (e.g., embedding in JSON or XML where newlines are problematic). Different base encodings have varying overheads and character sets; choose the appropriate one based on your specific requirements (e.g., base16 is human-readable but has high overhead, base64 is common, base85/Z85 are more compact for certain binary data).

ENCODING EXAMPLES

To encode a file 'data.bin' using base64:
basenc --base64 data.bin > data.b64

To encode a string using base32:
echo 'hello world' | basenc --base32

To decode base64 data from a file:
basenc --decode --base64 data.b64 > data.bin

USE CASES

basenc is ideal for embedding binary data in text-based formats like JSON, XML, or configuration files where direct binary content is not allowed. It is also useful for transmitting data over channels that may corrupt binary information (e.g., some legacy email systems or HTTP headers). The --url option is specifically designed for passing binary data within URLs without requiring additional URL encoding for reserved characters.

HISTORY

basenc was introduced as part of GNU Coreutils to provide a more unified and flexible tool for various base encodings. It effectively extends the functionality previously found only in base64 (which was also part of Coreutils) by consolidating support for other common base formats like base16, base32, base85, and Z85 into a single command. Its development aimed to simplify data encoding/decoding workflows and reduce the need for external, less standardized tools for these specific tasks, making it a more comprehensive solution within the standard Linux environment.

SEE ALSO

base64(1), xxd(1), uuencode(1), uudecode(1)

Copied to clipboard