Skip to content

Instantly share code, notes, and snippets.

@zaferayan
Last active August 12, 2020 12:33
Show Gist options
  • Select an option

  • Save zaferayan/2acc16a873e7467eefc44bcce353aa71 to your computer and use it in GitHub Desktop.

Select an option

Save zaferayan/2acc16a873e7467eefc44bcce353aa71 to your computer and use it in GitHub Desktop.
How to bulk shorten a list of URLs with bash commands via bit.ly API
#!/bin/sh
# Author: Zafer AYAN
# Variables
apiURL="https://api-ssl.bitly.com/v3/shorten"
access_token="$(cat access_token.txt)"
format="txt" # json
inputFileName="links.txt"
outputFileName="outputLinks.txt"
# Deleting file if exists
rm "$outputFileName"
# Loop through file
while IFS= read -r longUrl;do
echo "Shortening $longUrl"
# Create request url
url="$apiURL?access_token=$access_token&format=$format&longUrl=$longUrl"
# Remove \r char from url
url=${url%$'\r'}
# Make request to bit.ly and get shortened url
shortenedLink=$(curl $url)
# Write shortened url into output file
echo $shortenedLink
echo $shortenedLink>>$outputFileName
done < "$inputFileName"
echo "Completed"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment