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.

perl -MMIME::Base64 -i -0777ne 'print decode_base64($_)' ~/dog/1.jpg

Will avoid having to use any intermediary (be it a variable or a temp file) and stores the decoded file back into the original jpeg file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.