Skip to main content
edited tags
Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 263
Source Link

Base64 decode and overwrite file

I have a file which contains base64 encoded image data: ~/dog/1.jpg

I put it there via SSH, and base 64 encoded the data so it would be safe to transfer in the terminal. Assume the base64 image data is not corrupted.

Now that I have the image data passed to the other server I need to decode the file contents and then overwrite the encoded file with the decoded file.

And I can't use the base64 command as it's not available on the remote server.

How can I base64 decode file contents and then overwrite the encoded file contents with the decoded contents, preferably using perl?

This is what I have now...

# Decode image data
poo=$(cat ~/dog/1.jpg | perl -MMIME::Base64 -ne 'printf "%s\n",decode_base64($_)'); 

# Write decoded data to file
echo $poo >| ~/dog/1.jpg

This overwrites the file but does not properly decode the image.