Anypaste: Command-line file sharing for hackers.

Anypaste is a command line pastebin tool that’s not just for plain text. Anypaste supports dozens of hosting sites that accept a variety of different file types, such as images, audio, and video, and has a plugin system to add your favorite hosting site if you find it’s missing. Anypaste is great for sharing content via Reddit, IRC, Discord, Email, forums, or anywhere else that links can go.

Documentation

The complete guide to installing, using, and making plugins for Anypaste.

IntroductionBack to Top

Anypaste is a tool for uploading files to public-facing websites. Ideally, you can run anypaste /path/to/file and several things will happen:

  1. Anypaste will use file and other utilities to determine the type of the file.
  2. Anypaste will filter the list of plugins (supported hosting sites) to the ones which report they are compatible with the file.
  3. Anypaste will pass control to the “best” supported plugin, which will upload the file to the site and send the link to your terminal.

Anypaste is written in Bash, and was designed to support Linux and macOS. It should also work on other *nix-y systems, like FreeBSD or the Windows Subsystem for Linux, but some desktop integration features (namely the --copy and --notify command line parameters) will probably have issues.

InstallationBack to Top

Anypaste is just a single, cross-platform executable! That’s because it’s a shell script. You can download it from https://anypaste.xyz/sh, which will just redirect you to the raw file hosted on GitHub.

sudo mkdir -p /usr/local/bin # only needed on macOS
sudo curl -Lo /usr/local/bin/anypaste https://anypaste.xyz/sh
sudo chmod +x /usr/local/bin/anypaste

Dependencies

Bash 4+, curl, and POSIX core utils are all you need. ffprobe is optional but can provide more accurate compatibility checking for media file types; typically it comes inside of an ffmpeg package. I recommend installing through Brew on macOS. Without ffprobe, Anypaste will still identify files as best it can using file.

Command-line optionsBack to Top

All command-line options are documented in anypaste -h as well as here.

ConfigurationBack to Top

Anypaste’s configuration file is a small shell script which sets environment variables. As such, if you know how to write Bash, you know how to write Anypaste config. Here’s a cheat sheet:

The export preceding variable names tells Bash to pass these variables to subprocesses. The export is only necessary when configuring external plugins, i.e plugins that aren’t built-in to Anypaste.

To create your configuration file, run anypaste -C. It will tell you where the config file was put. It will typically be ~/.config/anypaste.conf on Linux and ~/.anypaste.conf on Mac. Everything is commented out by default.

Option Description Default
ap_plugins List of enabled plugins, in order of precedence See config file
ap_hooks List of hooks that are enabled by default Empty
ap_hook_policy If set to “greedy”, hooks will be run after every file uploaded. If set to “lazy”, they will only be run just before Anypaste exits. lazy

Post-Upload HooksBack to Top

Hooks run after Anypaste finishes uploading a file. Anypaste has two built-in hooks:

You can change how hooks work when multiple files are uploaded using ap_hook_policy. If it is set to lazy (the default), hooks will only run after the last file in a batch. If set to greedy, they will be run after every file.

Custom hooks can be added to the ap_hooks array in the config file. They have access to the same environment variables as plugins, but additionally can see the output of the last plugin in ap_last_stdout. Custom hooks are able to work in combination with built-in plugins; both will run. Example: Adding the following to your config file will save every link uploaded with Anypaste to ~/Other/anypaste-links.txt:

ap_hook_policy=greedy
ap_hooks=('echo -n "$ap_last_stdout" | grep ^Link: | head -n 1 >> ~/Other/anypaste-links.txt')

PluginsBack to Top

Anypaste plugins are each single executable files (except for the built-in ones, which are bundled inside the main Anypaste executable). The recommended way to “install” a plugin is to download it from its author’s website (hopefully a GitHub release), mark it as executable (chmod +x plugin-file), then put it in ~/.anypaste-plugins (you may need to create this folder). Alternatively, you could put it anywhere in your $PATH. After that, you must enable the plugin by adding it to the ap_plugins array in your config file. Remember that this array is in order of precedence – if you put it at the end, it will almost never be used!

Third party plugins

Want to see your plugin on this list? Open an issue!

Name Description Link
ThinImg A simple plugin created mainly to serve as an example about how to make third-party plugins. See Making Plugins for more information. GitHub

Making PluginsBack to Top

An Anypaste plugin is just an executable file (Bash will be used orr all examples, but you can use any language you wish). This executable file is called with a single argument, which instructs the plugin on what to do. Additional data is not passed on the command line, but rather supplied through environment variables which the plugin can read at will. Here’s a list of “commands” (the command line arguments) that can be passed to your plugin, and what your plugin should do in each situation:

check_eligibility

The plugin should determine whether the file being uploaded is compatible with this plugin. Typically, this involves checking that $ap_mime is a supported MIME type and that $ap_size is not over the site’s size limit. The plugin should exit with an exit code of 0 if it’s compatible, or non-zero if it is incompatible. This step should not involve any sort of network communications.

upload

The plugin should perform the actual upload. Typically this involves using curl to upload the file at $ap_path, using whatever API the site provides. It might also use $ap_human_name to set the name of the file on the site being uploaded to, if the site supports that. Any warnings, errors, or progress bars should be outputted to stderr. If the upload completes successfully, the plugin should output a blank line to stdout, followed by the links, followed by another blank line. Each “link” should consist of some sort of label, followed by :, then followed by the actual link. Here’s an example output:


Link: https://imgur.com/abcdef
Direct: https://i.imgur.com/abcdef.png
Edit: https://imgur.com/edit/abcdef/secret
Delete: https://imgur.com/delete/abcdef/secret

Your plugin should try to use “standard” names before the : whenever possible. This makes Anypaste’s behavior more consistent, and makes it easier to use inside of other scripts. If there’s something special about a link that a user should know, it’s better to output it to stderr before the links rather than stdout with the links. When in doubt, look at the built-in plugins and try to act as similarly to them as possible.

Exit with a code of 0 if the upload was successful, or non-zero otherwise.

get_info

The plugin should output “static” information about itself (metadata). Most of the environment variables will not be available during this step. Your plugin should output its information in an ini-like format. Each “section” should start with [section-name], then any lines after that until the next section are the contents. Blank lines are ignored. For example:

[description]
This is a cool plugin for uploading to imgur.com
[type]
Image
[tags]
permanent
editable
deletable

[required_config]
imgur_api_public:Public API key
imgur_api_secret:Private API secret

List of possible sections:

Aside: Config options

Config options are just environment variables which are set in the config file. You don’t have to do anything special to get access to them. Keep in mind that they are in a “global” scope, so name them in a way that they don’t interfere with other config options (e.g, don’t name an option “api_key”; instead, use “imgur_api_key”)

Example plugins

The official example plugin for ThinImg is a great learning resource. However, ThinImg is very simple, and we don’t make use of some of the features plugins can have. If you want to see more fleshed-out plugins, look in the first several hundred lines of the main Anypaste source code – that’s where all the built-in plugins are.

Environment VariablesBack to Top

Plugins and hooks have access to a number of environment variables. This is the primary way to get information about the currently uploading file.

Variable Description
ap_path The absolute path to the uploading file on-disk.
ap_human_name The name specified using -n or inferred from the file name.
ap_file_info Output of file "$ap_path", contains lots of useful file metadata.
ap_mime Mime type of the file (from file --mime-type --brief "$ap_path").
ap_ffprobe Output of ffprobe -show_streams -show_format "$ap_path". Null if ffprobe is not installed. Often provides more accurate and reliable info that file can for audio and video.
ap_size Size of the file, in bytes (from wc -c)
ap_plugin The name of the plugin as it appears in ap_plugins (or -p if -x is set).
ap_version Current Anypaste version.
ap_mac true if running on Mac, false otherwise.


There are many other variables set by Anypaste, but they are meant for internal use only and may change without warning.