Skip to content
Related Articles

Related Articles

Convert Text File Strings into Base64 encoding

Improve Article
Save Article
  • Difficulty Level : Expert
  • Last Updated : 21 Apr, 2022
Improve Article
Save Article

In the Linux system, you have a command base64. This helps you to convert text into base64 encoding but how can you change multiple lines of text into base64 encoding. We will understand some common Linux command in this article. That helps us to solve our problems. We will discuss piping, echo, and how to get user input from the keyboard.

Example:

 

Echo Command

In Linux System, you have an echo command that printing strings and numerical values in the terminal 

echo  "Your String"

Example:

 

Encoding and Decoding Text

Syntax base64:

base64 options filename

Convert Text into base64 encoding

echo Team | base64

Output

 

Decode base64 text into plain text

echo base64_text | base64 -d

Output:

 

Encoding Text File into base64 encoding

Use this Syntax:

base64 filename

Output:

Encoding Text file

Decoding base64 File into plain Text

base64 -d filename

Encoding any user Defined Text

First, we create a shell script for this task read input from users, and  convert text into base64 encoding

Use this Shell Script

#! bin/bash
echo Enter you Text
read data
output=`echo -n $data | base64` 
echo "Encoding Data $output"

Piping

Piping is reading input from one command and serving to another command. Now let’s Combine all of these commands and solve our problem. Suppose, we have a text file name of the file name.txt

Example:

$ cat names.txt
Ahmedabad
Taj Mahal
India Gate
Qutub Minar 

So we can change these strings to base64 encoding in following way:-

cat names.txt | while read names do;  echo $names | base64 ; done

Output:

 

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!