Just use the base64 program from the coreutils package:

echo QWxhZGRpbjpvcGVuIHNlc2FtZQ== | base64 --decode

Or, to include the newline character

echo `echo QWxhZGRpbjpvcGVuIHNlc2FtZQ== | base64 --decode`

output (includes newline):

Aladdin:open sesame
Answer from January on askubuntu.com
Discussions

How to decode Base64 in Linux?
Accidentally encoded a file in Base64 and deleted the original. Need steps to decode it back on a Linux system. Any help is appreciated! More on mepis.org
🌐 mepis.org
0
0
September 18, 2024
Converting a base64 string generated on a Linux server by PowerShell on Windows
This works:[System.Text.Encoding]::ASCII.GetString([System.Convert]::FromBase64String('dGVzdCBpcyBhIHRlc3QK')) [System.Text.Encoding]::ASCII instead of unicode More on reddit.com
🌐 r/PowerShell
8
3
February 8, 2022
shell - How to encode and decode data in base64 and base64URL by using unix commands? - Stack Overflow
I took it as a challenge to come ... order to decode: stackoverflow.com/questions/58957358/… 2021-08-25T10:31:22.26Z+00:00 ... Never echo to base64, use printf instead. echo -n is undefined and unsafe. 2025-11-25T17:47:35.447Z+00:00 ... Adding on to the answer by Kaplan Ilya, here is a command using standard linux/unix commands ... More on stackoverflow.com
🌐 stackoverflow.com
Stupid Linux tricks - use base64 to perfectly preserve formatting when copy/pasting between terminals, ssh sessions, serial connections, etc.
🌐 r/linux
85
379
April 29, 2023
🌐
nixCraft
cyberciti.biz › nixcraft › howto › linux › how to decode base64 string in linux and unix
How to decode BASE64 string in Linux and Unix - nixCraft
January 22, 2024 - The syntax for decoding a Base64 ... -d /path/to/encoded_file.txt As you can guess, the -d or --decode option tells the base64 command to decode the input string or a file....
🌐
Splunk
research.splunk.com › endpoint › 637b603e-1799-40fd-bf87-47ecbd551b66
Detection: Linux Decode Base64 to Shell | Splunk Security Content
May 13, 2026 - Additionally, it mitigates the potential damage and protects the organization's systems and data.The detection is made by searching for specific commands in the Splunk query, namely "base64 -d" and "base64 --decode", within the Endpoint.Processes data model. The analytic also includes a filter for Linux shells.
Find elsewhere
🌐
Robert Elder
blog.robertelder.org › intro-to-base64-command
Intro To 'base64' Command In Linux
September 21, 2023 - I use the 'base64' command to encode or decode from base64 encoding: · If I pipe this hello world statement into the 'base64' command, I'll see the following string of base 64 characters:
🌐
Linux Man Pages
man7.org › linux › man-pages › man1 › base64.1.html
base64(1) - Linux manual page
Base64 encode or decode FILE, or standard input, to standard output. With no FILE, or when FILE is -, read standard input. Mandatory arguments to long options are mandatory for short options too. -d, --decode decode data -i, --ignore-garbage when decoding, ignore non-alphabet characters -w, ...
🌐
Red Hat
redhat.com › en › blog › base64-encoding
Base64 encoding: What sysadmins need to know
November 20, 2025 - This article walks you through the theoretical aspects of Base64 encoding. Once I explain this simple encoding format, I will introduce the base64 command-line utility that you can use to encode and decode data.
🌐
YouTube
youtube.com › watch
Mastering the base64 Command in Linux: Encode & Decode Files Like a Pro! 🔥 (Step-by-Step Guide) - YouTube
Unlock the power of the base64 command in Linux! Whether you're encoding credentials, handling file transfers, or working with binary data, base64 is an esse...
Published: February 28, 2025
🌐
Base64 Decode
base64decode.org
Base64 Decode and Encode - Online
Decode each line separately: The encoded data usually consists of continuous text, so even newline characters are converted into their Base64-encoded forms. Prior to decoding, all non-encoded whitespaces are stripped from the input to safeguard the input's integrity.
🌐
Mepis
mepis.org › linux
How to decode Base64 in Linux? - Linux - Mepis Forum
September 18, 2024 - Accidentally encoded a file in Base64 and deleted the original. Need steps to decode it back on a Linux system. Any help is appreciated!
🌐
Reddit
reddit.com › r/powershell › converting a base64 string generated on a linux server by powershell on windows
r/PowerShell on Reddit: Converting a base64 string generated on a Linux server by PowerShell on Windows
February 8, 2022 -

Hey all,I have a base64 value that I generated on Linux like so:

echo 'test is a test' | base64
dGVzdCBpcyBhIHRlc3QK
echo 'dGVzdCBpcyBhIHRlc3QK' | base64 --decode
test is a test

But when I try to decode the string on Windows using PowerShell I'm getting a different result:

[System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String('dGVzdCBpcyBhIHRlc3QK'))
整瑳椠⁳⁡整瑳�

Suggestions?

Thanks!

EDIT: This solution in the comments worked for me:

[System.Text.Encoding]::ASCII.GetString([System.Convert]::FromBase64String('dGVzdCBpcyBhIHRlc3QK'))

🌐
How-To Geek
howtogeek.com › home › linux › how to encode or decode a base64 string on linux
How to Encode or Decode a base64 String on Linux
January 31, 2024 - Python has a base64 module that you can use for encoding and decoding strings. You can either use the python3 terminal command or write a full program. I'll show you both ways. The python3 command has a "-m" or module flag. You can use this flag to invoke the base64 module. Then you can pass your string with the help of the echo command or here-strings. Here's the full command: echo 'I love Linux' | python3 -m base64 # Using the echo commandpython3 -m base64 <<< 'I love Linux' # Using here-strings
🌐
LinuxConfig
linuxconfig.org › home › bash base64 decode and encode on linux
Use Base64 Command on Linux: Encode & Decode Guide
September 21, 2025 - Linux commonly uses base64 to encode and decode data. This method of encoding provides a reliable way for data to be transmitted and stored. The encoding process will convert binary data to ASCII characters, making it usable by a variety of ...
🌐
Scaler
scaler.com › home › topics › base64 command in linux
base64 command in linux - Scaler Topics
September 1, 2023 - The basic syntax of the base64 command in Linux is as follows: ... [OPTION]...: These are the options or flags which can be used with the base64 command. Examples include -d or --decode to decode data.
Top answer
1 of 4
39

tl;dr

Use basenc(1) from coreutils:

$ printf "xs?>>>" | basenc --base64
eHM/Pj4+
$ printf "xs?>>>" | basenc --base64url
eHM_Pj4-

As with base64(1), add the -d switch to decode.

A bit of explanation

Recent versions of coreutils include basenc(1) which supports several different encodings. From its help screen:

--base64          same as 'base64' program (RFC4648 section 4)
--base64url       file- and url-safe base64 (RFC4648 section 5)
--base32          same as 'base32' program (RFC4648 section 6)
--base32hex       extended hex alphabet base32 (RFC4648 section 7)
--base16          hex encoding (RFC4648 section 8)
--base2msbf       bit string with most significant bit (msb) first
--base2lsbf       bit string with least significant bit (lsb) first
--z85             ascii85-like encoding (ZeroMQ spec:32/Z85);
                  when encoding, input length must be a multiple of 4;
                  when decoding, input length must be a multiple of 5

Here is a string that illustrates the difference:

s="xs?>>>"

As binary:

$ printf "%s" "$s" | xxd -b -c1 | cut -d' ' -f2 | nl
     1  01111000
     2  01110011
     3  00111111
     4  00111110
     5  00111110
     6  00111110

And as 6 bit blocks (as base64 reads the data):

$ printf "%s" "$s" | xxd -b -c1 | cut -d' ' -f2 | tr -d '\n' | fold -w6 | nl
     1  011110
     2  000111
     3  001100
     4  111111
     5  001111
     6  100011
     7  111000
     8  111110

Note that block 4 and block 8 map to / and + respectively (Base64 table on Wikipedia):

2 of 4
22

This is the same suggestion as @jps but shorter. Also remember that echo by default always adds newline at the end, so when you want to encode it, you must add -n.

echo -n "Some_data_to_be_converted" | base64 | tr '/+' '_-' | tr -d '='

Decoding it back with bulit-in bash tools is more complicated as I didn't find an easy way to pad the string back with '=' so that the length will dividable by 4. Probably can be done with awk but I didn't dig deep enough. If you have local ruby it becomes trivial:

2.6.2 > require 'base64'
2.6.2 > Base64.urlsafe_encode64('test', padding: false)
 => "dGVzdA"
2.6.2 > Base64.urlsafe_decode64('dGVzdA')
 => "test"
🌐
Reddit
reddit.com › r/linux › stupid linux tricks - use base64 to perfectly preserve formatting when copy/pasting between terminals, ssh sessions, serial connections, etc.
r/linux on Reddit: Stupid Linux tricks - use base64 to perfectly preserve formatting when copy/pasting between terminals, ssh sessions, serial connections, etc.
April 29, 2023 -

Here's another example of "what's old is new again" - remember how a long time ago, you interacted with a modem by giving it textual commands, and then it connected you to distant machines, which you also spoke to in text, and when you wanted to send and receive binary files, you had to encode those as text too?

Well, that still works, and the commands needed to encode/decode it are installed by default pretty much everywhere, so that means you can...

  • Suppose there's some system you connect to through a VPN and then two jump boxes. You've ssh'd all the way there, but were lazy and didn't bother port-forwarding (if that's even allowed), and now you need to get a copy of some config file. Instead of copy/pasting it a bit at a time, or trying to make your scrollback buffer and text wrapping cooperate (and still convert tabs to weird numbers of spaces...), you can:

on the sending side: cat file.conf | base64

Now you don't have to worry about formatting at all*! Just copy all the base64 text as a block, and on the receive side: base64 -d > file.conf_from_remote

now paste the text, press enter, then ctrl+d when you're done, and you have a binary-identical copy of the file on your local system, regardless of how many spaces, newlines, and messed up terminal wrapping you copied.

  • * The caveat: sometimes you'll run into this on decode: "base64: invalid input". In that case, try base64 -di as the decode command - for some weird reason, certain versions of the base64 utility can't even decode their own input by default, because they decide to insert newlines on encode, but barf immediately on any non-base64 character on decode...including newlines. I have seen this behaviour primarily on old Gentoo boxes, Solaris, and ancient versions of CentOS and Red Hat.

  • Doesn't even have to be a remote system of course. I use this sometimes when I can't be arsed to deal with sudo/chmod/chown when copying a file between sessions running as different restricted users, or across a chroot, container, VM, etc.

Next trick:

Suppose you're editing a file locally and you want to copy a piece of a remote file, and it's very important to exactly preserve the indenting and whitespace (because it's python, yaml, or you've forgotten about ":set paste" in vim and internalised the notion that auto-indent is forever...but "set paste" doesn't help you with tabs not surviving a terminal display anyway). You can do this:

shift+V to go to visual select line mode; select the block you want

type :! base64 <enter>

copy & paste the block into your other vim, then select the base64 text

type :! base64 -d <enter>

and there it is, in all its tabular/nonprinting/emoji/16-bit-big-endian-unicode-because-why-not glory. (You'll want to undo the encode step on the source system, obviously.)

Don't believe me that it's 100% binary identical? Select the text blocks on both sides and check:

:! md5sum

[Edit: Important note about md5sum - it is only useful as a casual check against random errors nowadays, it is not a secure or cryptographic hash by any means. Think of it like a "deluxe crc32"; using it in interactive contexts like this is fine, but do not use it in scripts, etc.]

(Incidentally, if the block of text you want is really small or your local one is very similar already, you can skip the base64 and just edit it manually and just use md5sum to confirm you got it right.)

If your file or block of text is longer than a screenful

Pipe it to gzip first:

cat file.txt | gzip -9 | base64

base64 -d | gunzip > file.txt_copy

(For very small inputs, gzip often produces slightly fewer bytes than xz and even zstd, plus it's available practically everywhere.)

You can also scrunch down the base64 a little more by setting the line-width to unlimited (base64 -w 0), but be aware that:

  • Some implementations are buggy when it comes to very long lines (the opposite problem of the earlier caveat).

  • Even if the base64 command is OK with it, sometimes the terminal program isn't.

  • 4096 bytes per line is a common threshold at which something barfs.

  • It can make the copy/pasting more error-prone, as it's easier to miss a single character somewhere (and if you accidentally paste it in the wrong place, it makes more of a mess... on the other hand, at least your shell history will only have one bogus entry on accidental paste instead of 150. Ask me how many times I've seen "-bash: H4sIAAAAAAACAxXJQQ6AIAxE0b2nmJu49RoVxmgiLaFFw+2V3X/5m71IooiTUAakWNeAHaBGszpm: No such file or directory -bash: ztn1etic2Iki7r/ugczUKM68Lh893ENmSgAAAA==: No such file or directory" :P).

Important note for sysadmins and especially network people

I mentioned serial connections at the beginning of this. I cannot believe how many times I've see people laboriously copy a few lines at a time, paste them into their terminal window, wait (9600 8 N 1 only goes so fast, y'all...), copy a few more... and then cross their fingers and pray that no characters got lost, and none of the accidental extra whitespace will matter, when restoring a switch configuration.

The civilised way to do this is to be in shell mode on the switch instead of config mode (and if your switches don't have a basic Linux-like shell, consider switching to some that do), and do a base64 copy/paste as described, and then compare checksums. Especially if gzip is available on the switch, this is much, much faster and more reliable, and then you can do a local "load config" and not have any terminal issues in config mode.

(Some may argue that transferring over tftp or some variant of DHCP-mediated auto-provision is "more civilised", but 1, you're in this situation because your network is buggered so that might not be an option, and 2, I bet if you held a race, the base64 person would be done long before the tftp person has even finished the "how the crap do I get this server listening again?! why is it not serving files?!" stage of cursing, never mind the "I fat-fingered a subnet mask" or "oh yeah, we block tftp at the firewall for this subnet now, don't we?" stages of cursing.)

If your remote system is weird and doesn't have a base64 command

Good chance it still does and it's just part of something else. Hint: openssl has it built in (openssl base64 is equivalent to base64) if that's available (e.g. Juniper switches I think). openssl md5 also works if you're missing md5sum, but also try just md5, because it's called that on some unixes (I want to say Juniper switches again? or Mac OS?).

🌐
Reddit
reddit.com › r/sysadminblogs › base64 command in linux: encode & decode files
r/SysAdminBlogs on Reddit: base64 Command in Linux: Encode & Decode Files
March 3, 2025 - In This Video, You'll Learn: What is base64, and why is it used? How to encode and decode text using base64 Encoding and decoding files, both text…