morristech
6/29/2019 - 1:04 AM

Script to quickly review (and potentially remove) files in a cache directory (read your Downloads folder)

Script to quickly review (and potentially remove) files in a cache directory (read your Downloads folder)

#!/usr/bin/env bash

for FILE in ${*:-*}
do
	echo ">>> $FILE"

	while true
	do
		read -r -n 1 -p '[s]kip, [r]emove, [i]nspect or [q]uit? '
		echo
		case "$REPLY" in
			s)
				echo "(skipped)"
				echo
				break
				;;
			r)
				read -r -n 1 -p 'Are you sure [y|n]? '
				echo
				case "$REPLY" in
					y)
						echo rm -rf "$FILE" && {
							echo "$FILE removed"
							echo
						}
						break
						;;
					*)
						echo "(cancelled)"
						;;
				esac
				;;
			i)
				if [ -d "$FILE" ]
				then
					ls "$FILE"
				else
					case "${FILE##*.}" in
						png|gif|jpg|pdf|html|doc|docx|svg|mov|wmv|mpg)
							open "$FILE"
							;;
						zip)
							zip -sf "$FILE" | less
							;;
						*)
							less "$FILE"
					esac
				fi
				;;
			q)
				exit 0
				;;
		esac
	done
done