zsh refactor function cleanup-directory
This commit is contained in:
parent
4213b395ea
commit
c2d4b02cb6
@ -87,29 +87,51 @@ showtips() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cleanup-directory() {
|
cleanup-directory() {
|
||||||
local -r directory="${1}"
|
local -r directory="$(realpath "${1}")"
|
||||||
|
local -r age="+21" # Notation: +n => "At least n days"
|
||||||
|
|
||||||
if [ ! -d "${directory}" ]; then
|
if [ ! -d "${directory}" ]; then
|
||||||
echo "Directory '${directory}' not found" >&2
|
echo "Directory '${directory}' not found" >&2
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
age="+21" # Notation: +n => "At least n days"
|
if [ -n "${DRYRUN}" ]; then
|
||||||
echo "Deleting files not accessed for a ${age} days:"
|
echo "Deleting files not accessed for a ${age} days:"
|
||||||
find "${directory}" -atime "${age}" -exec rm -fv {} \;
|
find "${directory}" -atime "${age}"
|
||||||
|
|
||||||
echo "Deleting empty directories:"
|
echo "Deleting empty directories:"
|
||||||
find "${directory}" -type d -empty -print -delete
|
find "${directory}" -type d -empty -print
|
||||||
|
else
|
||||||
|
echo "Deleting files not accessed for a ${age} days:"
|
||||||
|
find "${directory}" -atime "${age}" | xargs -r rm -fv
|
||||||
|
|
||||||
|
echo "Deleting empty directories:"
|
||||||
|
find "${directory}" -type d -empty -print | xargs -r rmdir
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
cleanup-downloads() {
|
cleanup-directory-log() {
|
||||||
local -r downloads_directory="${XDG_DOWNLOAD_DIR}"
|
local -r directory="$(realpath "${1}")"
|
||||||
local -r log_file="${downloads_directory}/cleanup.log"
|
if [ ! -d "${directory}" ]; then
|
||||||
|
echo "Directory '${directory}' not found" >&2
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
local log_file="${directory}/cleanup.log"
|
||||||
|
|
||||||
|
if [ -n "${DRYRUN}" ]; then
|
||||||
|
log_file="/dev/null"
|
||||||
|
fi
|
||||||
|
|
||||||
touch "${log_file}"
|
touch "${log_file}"
|
||||||
|
|
||||||
echo "$(date -Iseconds) : Start cleanup" >> "${log_file}"
|
echo "$(date -Iseconds) : Start cleanup" >> "${log_file}"
|
||||||
cleanup-directory "${downloads_directory}" | tee -a "${log_file}"
|
cleanup-directory "${directory}" | tee -a "${log_file}"
|
||||||
|
}
|
||||||
|
|
||||||
|
cleanup-downloads() {
|
||||||
|
local -r downloads_directory="${XDG_DOWNLOAD_DIR}"
|
||||||
|
cleanup-directory-log "${downloads_directory}"
|
||||||
}
|
}
|
||||||
|
|
||||||
_bak() {
|
_bak() {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user