Mastering Base64 Encoding and Decoding in Ubuntu

Written by in linux on 7~11 minutes
Mastering Base64 Encoding and Decoding in Ubuntu

I used to ignore Base64. Thought it was some dusty old trick for email servers and hobbyist programmers still fiddling with Perl. Then one day, I hit a wall trying to jam a certificate into a YAML file for a Kubernetes setup. It didn’t want links or attachments—it wanted text. Clean, line-friendly, parser-happy text.

Suddenly Base64 wasn’t just useful—it was required.

That’s the thing about tools like this. You can skate by for years without touching them. Then one specific problem shows up, and now you’re encoding half your infrastructure through a command you barely remember exists.

Base64’s job is simple: it turns binary data into text. Not readable text. Not useful text. Just polite, well-mannered ASCII that won’t trip up a shell script or blow up a JSON payload.

But for all its friendliness, Base64 isn’t magical. It’s often misunderstood. And when you’re dealing with large files, or working under tight system constraints, it starts to show its teeth.

So let’s get real about what Base64 does, where it shines, and why it sometimes acts like a drama queen on a bad day.


How Base64 Works (and Why It’s Kinda Clumsy)

Base64 takes three bytes of raw data, chops them up into four chunks, and maps each chunk to a character. That’s it. The resulting characters live in a safe little playground of letters, digits, +, and /.

But because three doesn’t always divide evenly into the size of your file, it pads the leftover bytes with = signs. That’s its version of cleaning up before guests arrive.

Compared to alternatives like hexadecimal, Base64 is a space-saver. Hex doubles your file size. Base64 adds about 33 percent. Not great, but not a disaster either.

And raw binary? That’s pure gamble. It might fly between two programs, sure. But drop that file into a text-based config or a fragile upload form and watch the chaos roll in. You’ll get corrupted files, unreadable characters, or worse—silent failures.

Still, Base64 has this weird reputation. People often think it encrypts data. It doesn’t. Not even close. It’s like putting a note under a napkin and calling it hidden.

That said, it’s useful. It’s just… weird. Like the one friend who always helps you move apartments but never shows up on time.


Your Ubuntu Cheat Sheet (with a Few Booby Traps)

So here’s the quick way to encode:

base64 my_file.txt > my_file.b64

And to decode:

base64 -d my_file.b64 > my_file.txt

Easy. Until it’s not.

The problem? Line breaks. The default behavior is to wrap lines at 76 characters. This comes from email encoding standards, and while that made sense in 1995, it will 100% wreck your YAML file or API payload today.

So do this instead:

base64 -w 0 my_file.txt > my_file_clean.b64

No line breaks. No surprises. That -w 0 flag is the difference between “huh, why isn’t this working” and “oh, that’s what I forgot.”

Also, always verify that what you decoded matches the original. Just being paranoid isn’t enough. Use a checksum:

sha256sum original.txt decoded.txt

If they match, great. If not, well… start looking for missing padding or an accidental newline.


Big Files Turn Base64 Into a Slug

Try this: take a 5GB file and Base64 it on a mid-range laptop. Go ahead. I’ll wait.

Actually, don’t.

Ubuntu’s built-in base64 command loads the entire file into memory before it spits out the result. That’s fine for a couple hundred megabytes. But for anything bigger, you’re begging your system to throw a tantrum.

You’ll either get a freeze, a swap-file slugfest, or worst case, a kernel-level faceplant.

Here’s a rough idea of the memory tax: Base64 adds around 33 percent overhead. So a 5GB file turns into 6.65GB. If you’re already using half your RAM just to run your editor and browser, that file is not getting encoded anytime soon.

Moral of the story? Don’t treat Base64 like a sledgehammer. Use it like a scalpel.


Streaming, Chunking, and Other Lifesaving Tactics

If you’ve got a big file, don’t let base64 eat it all at once. Stream it.

cat big.iso | base64 -w 0 > big.iso.b64

Or better yet, go with openssl:

openssl base64 -in big.iso -out big.iso.b64

It handles large files with a bit more grace. And it doesn’t choke on system resources the way the default base64 sometimes does.

Even smarter: break your file into pieces and encode each one separately.

split -b 100M large_file.tar chunk_
for f in chunk_*; do base64 -w 0 "$f" > "$f.b64"; done

To reassemble:

cat chunk_*.b64 > full_file.b64

It’s old school. But it works. It’s like mailing someone a book in separate envelopes and telling them to re-staple it when it arrives.


Decoding in Chunks (And Not Losing Your Sanity)

If you’ve encoded in chunks, decoding takes patience—but not much skill.

for f in *.b64; do base64 -d "$f" > "${f%.b64}"; done
cat chunk_* > reassembled_file.tar

Whatever you do, don’t try to dump the decoded data straight into your terminal. Not unless you want a screen full of binary garbage and maybe a few audible beeps.

Redirect to a file. Always.

Also, if you’re decoding an executable script or binary, don’t forget permissions:

chmod +x my_script.sh

Otherwise, you’re left wondering why your tool won’t run—when it’s just missing the right flag.


One Trick: Compression Before Encoding

If your file is text-heavy, compressing it first can save you a ton of space.

gzip -c logs.txt | base64 > logs.txt.gz.b64

And to get it back:

base64 -d logs.txt.gz.b64 | gzip -d > logs.txt

This works great for logs, configs, or anything repetitive. You shrink it with gzip, then Base64 handles the conversion to text.

Want to bundle a folder? Do this:

tar czf - my_folder | base64 > my_folder_backup.b64

That’s your all-in-one portable archive right there.


The Quirks You’ll Probably Run Into

Some issues just keep showing up:

  • Out of memory? Use pipes or openssl. Don’t brute-force it.
  • Missing padding? If decoding fails, check the final line. You might need to manually tack on a =.
  • Line breaks messing up your JSON or YAML? Use -w 0. Seriously.
  • Lost permissions after decoding? Double-check with ls -l. It’s easy to miss.

These are the little things that turn a simple command into a rabbit hole. Especially when you’re staring at a failed deployment at 2 AM.


So… Is Base64 Even Worth It?

Base64 is like duct tape for data. It doesn’t fix deep structural problems, but it can hold things together when everything else fails.

It’s good for:

  • Embedding small data inside configs
  • Transporting binary files through text-only systems
  • Making something portable without triggering every parser in the toolchain

But it’s not a replacement for:

  • Actual encryption
  • File transfer tools
  • Anything remotely performance-sensitive

If you’re dealing with giant media files, or trying to move sensitive data securely, look elsewhere. But for weird edge cases, infrastructure hacks, or API shenanigans, Base64 still earns its keep.

Just know its limits. And don’t try to make it do more than it was built for.

Because sometimes, turning binary into ASCII is exactly what you need. Other times, it’s just a nice way to ruin your afternoon.

Written By

A FOSS guy. Currently, he works as a full stack developer. He writes about Linux and FOSS related content.

We use cookies in this website to give you a personalized experience. Read our Privacy Policy for more information. Accept

Privacy Preferences

We and our partners share information on your use of this website to help improve your experience. For more information, or to opt out click the Do Not Sell My Information button below.