Created
February 20, 2021 13:42
-
-
Save meehatpa/d174a7dd7f075af2c60f99c292d7ba01 to your computer and use it in GitHub Desktop.
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 | |
set -e | |
prefix=/mnt/usb/ | |
# delete dir with size < 5 MB | |
#DIRRM=`find /mnt/usb -mindepth 1 -maxdepth 1 -type d -exec du -ks {} + | awk '$1 <= 50000' | cut -f 2-` | |
#IFS=' | |
#' | |
#for i in $DIRRM; do | |
# rm -rf $i | |
#done | |
#find /mnt/usb/ -type d -empty -delete | |
shopt -s nullglob | |
dir_array=(/mnt/usb/*) | |
shopt -u nullglob # Turn off nullglob to make sure it doesn't interfere with anything later | |
#printf "dir_array=%s\n" "${dir_array[@]}" # Note double-quotes to avoid extra parsing of funny characters in filenames | |
IFS=$'\n' read -r -d '' -a tr_array < <( transmission-remote -l | sed '1d;$d' && printf '\0' ) | |
#printf "tr_array=%s\n" "${tr_array[@]}" # Note double-quotes to avoid extra parsing of funny characters in filenames | |
# Haven't figured out how to not include /mnt/usb in the above glob output, and so have to prefix the same below | |
tr_names_array=() | |
for i in "${tr_array[@]}"; do | |
name=$(echo "$i" | cut -c 71-) | |
tr_names_array+=($prefix"$name") | |
done | |
#printf "tr_names_array=%s\n" "${tr_names_array[@]}" # Note double-quotes to avoid extra parsing of funny characters in filenames | |
# Without this, this file also would get deleted in the loop | |
tr_names_array+=('/mnt/usb/'`basename "$0"`) | |
# delete dirs/files which are not in transmission torrent list | |
for i in "${dir_array[@]}"; do | |
if ! [[ "${tr_names_array[*]}" =~ "$i" ]]; then | |
rm -rf "$i" | |
else | |
dir_size=`du -sm "$i" | cut -f1` | |
[[ dir_size -lt 5 ]] && printf "%s has size < 5 MB, consider deleting it. Actual size is %d MB.\n" "$i" $dir_size | |
fi | |
done | |
# delete transmission torrent not in dirs/files in /mnt/usb | |
for i in "${tr_array[@]}"; do | |
name=$(echo "$i" | cut -c 71-) | |
if ! [[ "${dir_array[*]}" =~ "$name" ]]; then | |
# get torrent id | |
[[ "$i" =~ ^[^0-9]*([0-9]+)\* ]] && torrent="${BASH_REMATCH[1]}" | |
transmission-remote --torrent "$torrent" --remove-and-delete | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment