Skip to content

Instantly share code, notes, and snippets.

@ys-qb
Forked from greird/slack-files-downloader.sh
Last active August 31, 2023 12:41
Show Gist options
  • Save ys-qb/2fd99d6c7c4766e4036a1f213efd3cac to your computer and use it in GitHub Desktop.
Save ys-qb/2fd99d6c7c4766e4036a1f213efd3cac to your computer and use it in GitHub Desktop.
Download all files from a Slack workspace export folder.
#!/bin/bash
#
# This script will browse a Slack export folder and download all files in a new /export folder
#
# HOW TO:
# 1. As a Workspace admin, download an export of your Slack history (https://www.slack.com/services/export)
# 2. Make sure you have jq installed (https://stedolan.github.io/jq/)
# 3. Place this file at the root of your Slack export folder, next to channels.json
# 4. Run `bash slack-files-downloader.sh` in your terminal
#
# OPTIONS
# -o Overwrite files if they already exist in destination folder, otherwise skip them.
# -s Do not show message when a file is skipped
# -p Add date prefix(YYYY-MM-DD) from json filename
while getopts "osp" flag
do
case $flag in
o) overwrite=true;;
s) silent=true;;
p) prefix=true;;
esac
done
printf "\nSelect one specific file type to download or leave empty for any (e.g. mp3, binary, jpg, png):\n"
read usertype
printf "\nSelect a channel to look into or leave empty for all channels:\n"
read userchannel
for channel in $(cat channels.json | jq -rc '.[].name')
do
if [[ $channel == $userchannel ]] || [[ -z $userchannel ]]
then
printf "\n============================================\nLooking into #$channel...\n============================================\n"
for file in "$channel"/*.json
do
for a in $(cat $file | jq -c '.[].files[0] | [.title, .url_private_download, .filetype] | del(..|nulls)' | sed 's/ //g')
do
if [ "$a" == '[]' ]
then
continue
fi
filetype=$(echo $a | jq -r '.[2]')
if [[ $filetype == $usertype ]] || [[ -z $usertype ]] || [[ -z $filetype ]]
then
filename_raw=$(echo $a | jq -r '.[0]')
filename=$(echo $filename_raw | sed -e 'y/āáǎàçēéěèīíǐìōóǒòūúǔùǖǘǚǜüĀÁǍÀĒÉĚÈĪÍǏÌŌÓǑÒŪÚǓÙǕǗǙǛÜ/aaaaceeeeiiiioooouuuuuuuuuAAAAEEEEIIIIOOOOUUUUUUUUU/')
if [[ $prefix != true ]]
then
filename="${filename##*/}"
else
filename_prefix=$file
filename_prefix="${filename_prefix##*/}"
filename_prefix="${filename_prefix%.*}"
filename="${filename_prefix}-${filename##*/}"
fi
if [[ ! -z $filename_raw ]] && [[ $filename_raw != "null" ]]
then
if [ -f "export/$channel/$filename" ] && [[ $overwrite != true ]]
then
if [[ $silent != true ]]
then
printf "$filename already exists in destination folder. Skipping!\n"
fi
continue
fi
printf "Downloading $filename...\n"
mkdir -p export/$channel
url=$(echo $a | jq -rc '.[1]')
curl --progress-bar $url -o "export/$channel/$filename"
fi
fi
done
done
fi
done
@ys-qb
Copy link
Author

ys-qb commented Aug 31, 2023

adding -p option

-p Add date prefix(YYYY-MM-DD) from json filename

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment