Skip to content

Instantly share code, notes, and snippets.

@zaxbux
Created March 28, 2024 00:11
Show Gist options
  • Save zaxbux/6d226483fb9cbf98915efb4f1ed85cf1 to your computer and use it in GitHub Desktop.
Save zaxbux/6d226483fb9cbf98915efb4f1ed85cf1 to your computer and use it in GitHub Desktop.
Delete user downloads that are older than 28 days (macOS)
#!/bin/bash
sudo -u $(stat -f "%Su" /dev/console) /bin/bash <<'END'
# Move files to user's trash
function trash() {
if [[ $# ]]; then
a=()
for f in "$@"; do a+=("$(realpath "$f")"); done
f=$(printf "\",POSIX file \"%s" "${a[@]}")\"
osascript -ss -e"tell app \"Finder\" to delete {${f:2}}" 1>/dev/null
fi
}
files=()
# List all files
while IFS= read -r -d '' file; do files+=("$file"); done < <(find ~/"Downloads/"* -mtime +28 -print0)
# Delete files in chunks to avoid timeouts
k=64;
for ((i = 0; i < ${#files[@]}; i += k))
do
trash "${files[@]:i:k}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment