Dieser Beitrag ist derzeit nur in Englisch verfügbar.

Have you ever been in a pinch where you needed to transfer a file between two servers, but didn't want to resort to the usual methods like scp? Well, you're in luck! Today, we're diving into a clever and efficient way to move files using gzip and base64 within SSH sessions. Perfect for those small, but crucial files.

Update 2024: I tweaked the first step with an additional echo to make it easier to select the output for copy-pasting.

Let's break down the two step process:

1. Compress, Convert, Copy

On your source server, the magic begins with a simple line:

# Note: macOS does not support "-w 0" and it's not needed there. Leave it off.
gzip -c yourfile.txt | base64 -w 0 ; echo
# or if you are on a modern system, use xz
xz -c yourfile.txt | base64 -w 0; echo

This neat one-liner compresses your file (it doesn't have to be a txt file!) and encodes it into a base64 string, all in a continuous line for easy copying. Clipboard, get ready!

On more modern systems, you could also use xz instead of gzip. The example uses gzip because that is more commonly found on a lot of systems.

2. Paste, Decode, Decompress

Over on your destination server, the reversal is just as elegant. Run:

echo "PASTED_BASE64_STRING" | base64 --decode | gunzip > yourfile.txt
# or if you were using xz
echo "PASTED_BASE64_STRING" | base64 --decode | unxz > yourfile.txt

Behold! The file reassembles itself, ready for action. That's it.

Bonus: Verifying File Integrity

After transferring your file, it's a good practice to verify that it remains unchanged. We can do this using checksums.

On your source and on your destination run the following:

sha256sum yourfile.txt
# Not all systems have all tools, here are some alternatives:
shasum -a 256 yourfile.txt  # macOS 14+
sha1sum yourfile.txt  # macOS <14 or Linux, If sha256sum is unavailable
openssl sha256 yourfile.txt  # A universal option where OpenSSL is installed

Then compare the outputs. If the checksums match, congrats! Your file was transferred intact.

This step, though simple, adds an extra layer of confidence in the reliability of your file transfer process.

Andere Beiträge

  • Create wagtail pages programmatically

    Programmatically creating the Wagtail page tree can save you and your team a lot of time in the long run.

  • pylibmc and the dreaded "memcached.h file not found" error

    You are installing pylibmc but it always fails with an error message like "'libmemcached/memcached.h' file not found". Here I'll show you likely causes and how to fix them on macOS and Linux (Ubuntu, Debian)