How to encode/decode text in Linux with base64 command?
On Unix-like operating systems, the base64
command is used for the base64 encoding scheme; the base64
command converts binary strings into text representations. Base64 is a collection of binary-to-text encoding techniques that can convert sequences of 8-bit bytes, which make up binary data, into sequences of 24 bits, which can be represented by four 6-bit Base64 digits.
You can provide a file name as an argument, otherwise it will read standard input. When decoding, the input may contain newlines in addition to the bytes of the formal base64
alphabet.
Mandatory arguments to long options are mandatory for short options too. Use --ignore-garbage
to attempt to recover from any other non-alphabet bytes in the encoded stream.
base64
command syntax
base64 [OPTION]... [FILE]
base64
command options
-d , --decode |
decode base64 data provided as input and print to standard output. |
-i , --ignore-garbage |
when decoding, ignore non-alphabet characters provided as input. |
-w , --wrap=COLS |
wrap encoded lines after the COLS character (default 76). Use 0 to disable line wrapping. |
--help |
display this help and exit. |
--version |
output version information and exit. |
Examples of base64
command:
1. Base64 encodes a file’s content provided as an argument and write the result to standard output:
$ base64 filename
Here, we have a file named file1.txt
:
$ cat file1.txt
Welcome to tutorialspoint.com
This is an article about the base64 command.
Let’s run the base64
command with the file name as argument:
$ base64 file1.txt
V2VsY29tZSB0byB0dXRvcmlhbHNwb2ludC5jb20KVGhpcyBpcyBhbiBhcnRpY2xlIGFib3V0IHRo
ZSBiYXNlNjQgY29tbWFuZC4K
2. Decode a file containing Base64 content and write the result to stdout:
base64 --decode filename
We have a file with some Base64 encoded text in it.
$ cat file2.txt
WW91IGZvdW5kIHRoZSBzdXBlciBzZWNyZXQgYmFzZTY0IGNvbnRlbnQuIFNlZSB5b3UgaW4gdGhl
IG5leHQgZXhhbXBsZQo=
Let’s run the base64
command with the -d
option to decode the contents of the file provided as an argument.
$ base64 -d file2.txt
You found the super secret base64 content. See you in the next example
3. Base64 encodes the data provided through standard input with base64 command.
$ somecommand | base64
Let’s use the echo
command’s output for this example.
$ echo -e "example3:\npipes are amazing because we can decide where\n\n input and output goes"
example3:
pipes are amazing because we can decide where
input and output goes
Use |
to use echo command’s output as input for base64
.
$ echo -e "example3:\npipes are amazing because we can decide where\n\n input and output goes" | base64
ZXhhbXBsZTM6CnBpcGVzIGFyZSBhbWF6aW5nIGJlY2F1c2Ugd2UgY2FuIGRlY2lkZSB3aGVyZQoK
IGlucHV0IGFuZCBvdXRwdXQgZ29lcwo=
4. Decode Base64 encoded text with the base64 command.
$ somecommand | base64 --decode
Let’s use the content of the file from example 2.
$ cat file2.txt
WW91IGZvdW5kIHRoZSBzdXBlciBzZWNyZXQgYmFzZTY0IGNvbnRlbnQuIFNlZSB5b3UgaW4gdGhl
IG5leHQgZXhhbXBsZQo=
Again use the standard output of cat
command as standard input for base64
command.
$ cat file2.txt | base64 -d
You found the super secret base64 content. See you in the next example
5. Line wrapping of Base64 encoded output of the base64 command. (Default COLS is 76)
base64 -w COLS filename
Or
base64 -wrap=COLS filename
Or
somecommand | base64 -w COLS
Or
somecommand | base64 -wrap=COLS
You can disable line wrapping by using COLS equal to 0.
6. Ignore non-alphabet characters provided in input.
Let’s again use the Base64 encoded file from example 2 and add some non alphabet characters with the help of the echo command.
$ echo -e `cat file2.txt` "\r\n"
WW91IGZvdW5kIHRoZSBzdXBlciBzZWNyZXQgYmFzZTY0IGNvbnRlbnQuIFNlZSB5b3UgaW4gdGhl IG5leHQgZXhhbXBsZQo=
Now use this mixture of Base64 encoded text and non-alphabet characters and try to decode it with the help of the base64 -d command.
$ echo -e `cat file2.txt` "\r\n" | base64 -d
You found the super secret base64 content. See you in thebase64: invalid input
You can see that it gives errors while dealing with non-alphabet characters.
To remove this error we need to ignore non-alphabet characters as follows:
$ echo -e `cat file2.txt` "\r\n" | base64 -d -i
You found the super secret base64 content. See you in the next example
Conclusion
In this article, we explored a basic Linux command base64
. It is used to Base64 encode or decode data provided as input. We have gone through all use cases of options available in the base64
command.