Skip to content

Instantly share code, notes, and snippets.

@greird
Last active January 27, 2025 21:43
Show Gist options
  • Save greird/94dbea010540022dca6010d9fd74d9d4 to your computer and use it in GitHub Desktop.
Save greird/94dbea010540022dca6010d9fd74d9d4 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
while getopts "os" flag
do
case $flag in
o) overwrite=true;;
s) silent=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
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/')
filename="${filename##*/}"
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
@myself514
Copy link

This iteration will rename files if the same name exist

while getopts "os" flag
do
case $flag in
o) overwrite=true;;
s) silent=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
            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/')
                filename="${filename##*/}"
                
                if [[ ! -z $filename_raw ]] && [[ $filename_raw != "null" ]]
                then
                    if [ -f "export/$channel/$filename" ] && [[ $overwrite != true ]]
                    then
                        base="${filename%.*}"
                        extension="${filename##*.}"
                        counter=1
                        new_filename="${base}$(printf "%04d" $counter).${extension}"

                        while [ -f "export/$channel/$new_filename" ]; do
                            counter=$((counter + 1))
                            new_filename="${base}$(printf "%04d" $counter).${extension}"
                        done

                        filename=$new_filename
                    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

@v01pe
Copy link

v01pe commented Dec 5, 2024

Thanks a lot for this script! Mostly worked as I hoped, but I used @adamwulf's fork, that adds sub-folders for year and month.

Two suggestions (see my fork):

  • Use .name instead of .title as base for $filename, b/c the title field sometimes had the file extension omitted in our export (e.g. files uploaded from iOS).
  • Check for .is_external, to see if the file can be downloaded from notion, or is hosted somewhere else (e.g. Google Docs have this set). The script threw errors, b/c .url_private_download is empty in that case, which I solved by adding, .external_url and moving both url fields to the end of the initial filter. This way when null entries are removed, the last entry will either contain the Notion download url, or the external url – check against the external flag, to know which one it is.

@v01pe
Copy link

v01pe commented Jan 16, 2025

Attention: I just realized, that if there are multiple files per message, only the first one will be downloaded! Instead of filtering '.[].files[0]', you can filter '.[].files[]?', which will iterate all files, if any are available. This also is much faster for channels with many messages, that don't contain files!

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