dotfiles/home/user/.local/bin/cleanup-directory
2025-11-05 22:17:50 +03:00

27 lines
767 B
Bash
Executable File

#!/bin/sh
set -eu
directory="$(realpath "${1}")"
readonly directory
readonly age="+21" # Notation: +n => "At least n days"
if [ ! -d "${directory}" ]; then
echo "Directory '${directory}' not found" >&2
return 1
fi
if [ -n "${DRYRUN}" ]; then
echo "Deleting files not accessed for a ${age} days:"
find "${directory}" -mindepth 1 -atime "${age}" -not -type d -not -name cleanup.log
echo "Deleting empty directories:"
find "${directory}" -mindepth 1 -type d -empty
else
echo "Deleting files not accessed for a ${age} days:"
find "${directory}" -mindepth 1 -atime "${age}" -not -type d -not -name cleanup.log -print -delete
echo "Deleting empty directories:"
find "${directory}" -mindepth 1 -type d -empty -print -delete
fi