49 lines
1.1 KiB
Bash
Raw Normal View History

2021-03-30 01:09:01 +02:00
#!/bin/bash
set -euo pipefail
# Try removing as many empty directories as possible with a single
# `rmdir` call to avoid or at least speed up the loop below.
if /bin/rmdir -- "${@}" &>/dev/null
then
2021-03-30 01:09:01 +02:00
exit
fi
for path in "${@}"
do
2021-03-30 01:09:01 +02:00
symlink=true
[[ -L "${path}" ]] || symlink=false
directory=false
if [[ -d "${path}" ]]
then
2021-03-30 01:09:01 +02:00
directory=true
if [[ -e "${path}/.DS_Store" ]]
then
2021-03-30 01:09:01 +02:00
/bin/rm -- "${path}/.DS_Store"
fi
# Some packages leave broken symlinks around; we clean them out before
# attempting to `rmdir` to prevent extra cruft from accumulating.
/usr/bin/find -f "${path}" -- -mindepth 1 -maxdepth 1 -type l ! -exec /bin/test -e {} \; -delete
elif ! ${symlink} && [[ ! -e "${path}" ]]
then
2021-03-30 01:09:01 +02:00
# Skip paths that don't exists and aren't a broken symlink.
continue
fi
if ${symlink}
then
2021-03-30 01:09:01 +02:00
# Delete directory symlink.
/bin/rm -- "${path}"
elif ${directory}
then
2021-03-30 01:09:01 +02:00
# Delete directory if empty.
/usr/bin/find -f "${path}" -- -maxdepth 0 -type d -empty -exec /bin/rmdir -- {} \;
else
# Try `rmdir` anyways to show a proper error.
/bin/rmdir -- "${path}"
fi
done