Created
May 20, 2017 20:17
-
-
Save kwhitefoot/7ae4eaaf30ac5c2e58141faab6e2ef50 to your computer and use it in GitHub Desktop.
Delete files from neocities that do not exist in your local copy.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Neocities provides an API for deleting files from your site. | |
# | |
# Example Usage: | |
# $ USERNAME=username PASS=secret_pass bash delete_not_found_neocities.sh path_to_local_web_root | |
# or | |
# $ export USERNAME=username | |
# $ export PASS=secret_pass | |
# $ ./delete_not_found_neocities.sh path_to_local_web_root | |
# All files that exist on the server but not locally will be removed | |
# from the server | |
# From: | |
# Fail immediately on error | |
set -e | |
# Name the source directory | |
src=$1 | |
# Note that this is not a very secure way to delete because the | |
# password and username are part of the URL and also visible to anyone | |
# who can see the curl command line in the process list. | |
function delete_file { | |
shortName=$1 | |
echo "Deleting: ${shortName}" | |
curl -d "filenames[]=$shortName" https://$USERNAME:[email protected]/api/delete | |
} | |
# Change directory to the source so that we can easily get names | |
# relative to that directory and compare them. | |
cd "$src" | |
deleted=0 | |
# Now find all the files on the server and delet those that are not | |
# also found in the source. | |
curl "https://$USERNAME:[email protected]/api/list" | jq '.files | .[] | .path' | while read file | |
do | |
# Remove the quotation marks. | |
name="${file:1:-1}" | |
if [ ! -f "$name" ] && [ ! -d "$name" ] | |
then | |
delete_file "$name" | |
((deleted++)) | |
echo "Deleted ${deleted} files" | |
fi | |
done | |
echo "Deleted ${deleted} files" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment