DEV Community

Query Filter
Query Filter

Posted on

desktop-3

How to Turn Any File Into Text You Can Paste Anywhere (Linux & Windows)

Base64 encoding converts binary data (such as images, PDFs, audio clips, or zip archives) into plain text. This allows you to transport files through text-only channels like chat apps, terminal sessions, emails, or Markdown documentation.


🐧 Linux (X11 / Desktop Environment)

On standard Linux distributions, you can use base64 combined with clipboard utilities like xclip or xsel.

Prerequisites

Install a clipboard manager if you don't already have one:

# Ubuntu / Debian
sudo apt install xclip xsel

# Fedora / RHEL
sudo dnf install xclip xsel
Enter fullscreen mode Exit fullscreen mode

1. Encode a File and Copy Directly to Clipboard

Pipe the base64 string directly into your system clipboard without creating an intermediate text file:

# Using xclip (Recommended)
base64 image.png | xclip -selection clipboard

# Using xsel
base64 image.png | xsel --clipboard
Enter fullscreen mode Exit fullscreen mode

2. Decode Text from Clipboard Back Into a File

When receiving a Base64 text string, decode it directly from your clipboard to restore the original binary file:

# Using xclip
xclip -selection clipboard -o | base64 -d > restored_image.png

# Using xsel
xsel --clipboard -o | base64 -d > restored_image.png
Enter fullscreen mode Exit fullscreen mode

🪟 Windows (WSL / Windows Bash)

Tools like xclip and xsel are not natively available in WSL (Windows Subsystem for Linux) because it lacks an X11 display server. However, you can call native Windows tools directly from your Bash shell.

1. Encode a File and Copy to Windows Clipboard

Use the built-in Windows utility clip.exe directly from your WSL terminal:

base64 image.png | clip.exe
Enter fullscreen mode Exit fullscreen mode

You can now press Ctrl + V to paste the Base64 text into any Windows application.

2. Decode Text from Windows Clipboard Back Into a File

Use powershell.exe -command Get-Clipboard to pull the Base64 string out of your Windows clipboard and pipe it through base64 -d:

powershell.exe -command Get-Clipboard | base64 -d > restored_image.png
Enter fullscreen mode Exit fullscreen mode

💡 Pro Tip: Create macOS-style pbcopy and pbpaste Aliases

To keep your terminal workflow consistent across Linux and Windows, add these handy aliases to your ~/.bashrc or ~/.zshrc file:

# Open configuration file
nano ~/.bashrc

# For Windows / WSL add:
alias pbcopy='clip.exe'
alias pbpaste='powershell.exe -command Get-Clipboard'

# For Linux (X11) add:
# alias pbcopy='xclip -selection clipboard'
# alias pbpaste='xclip -selection clipboard -o'
Enter fullscreen mode Exit fullscreen mode

After running source ~/.bashrc, you can use identical short commands on any platform:

# Encode and copy to clipboard
base64 document.pdf | pbcopy

# Decode from clipboard to file
pbpaste | base64 -d > restored_document.pdf
Enter fullscreen mode Exit fullscreen mode

Top comments (0)