Here’s a script to track Twitter unfollowers. It it based on @sferik‘s excellent command-line Ruby Twitter client ‘t’. Visit its GitHub page for installation instructions; they’re pretty straightforward.

The script is quick and dirty but easy to follow: it compares a new followers list to the previous list followers list and e-mails you the difference.

While it is technically very easy to automatically unfollow back I highly recommend against it, following my recent screw-up when I accidentally unfollowed over two thousand followers. E-mail notifications are the way to go.

Make sure you’re server is set up to send out mail and change that part accordingly. If it isn’t I recommend a Google search for “ssmtp gmail”. That’s an easy way to have your server send out mail using your Gmail account.

Note that Twitter queries use api calls. I recommend agains running the script too often. Once an our or once a day.

The script was written for Bash on Debian 8. Should work on any major Linux distro.

#!/bin/bash

# Script to track Twitter unfollowers.
# @Vorkbaard, 2017

# Change to script directory for cron jobs
cd "$(dirname "$0")";

OLD=followers.old
NEW=followers.new
FROMADDR=your-servers@email.address.here
TOADDR=your@email.address

if [ ! -e $OLD ]; then
  # Old list does not exist. Create one.
  t followers > followers.old
  echo "Created initial followers file. Nothing to compare; ciao!"
  exit
fi

# Get current list of followers
t followers > $NEW

# Compare lists
UNFOLLOWERS=$(diff -y --suppress-common-lines $OLD $NEW | grep "<" | cut -d' ' -f1)

# Comparison done; make the new list the old list for the next run
mv $NEW $OLD

# If there are unfollowers, mail them.
if [ -n "$UNFOLLOWERS" ]; then

  # Create a mail
  for UNFOLLOWER in $UNFOLLOWERS; do
    echo "<a target=_blank href=https://twitter.com/$UNFOLLOWER>$UNFOLLOWER</a><br />" >> mail
  done

  # Create subject
  SUBJ="$(wc -l mail | cut -d' ' -f1) Unfollower(s)"

  # Send the mail
  cat mail | mailx -a "Content-type: text/html" -a "From: Unfollower tracker <$FROMADDR>" -s "$SUBJ" $TOADDR
  rm mail

fi