Created
March 28, 2024 00:11
-
-
Save zaxbux/6d226483fb9cbf98915efb4f1ed85cf1 to your computer and use it in GitHub Desktop.
Delete user downloads that are older than 28 days (macOS)
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 | |
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