mirror of
https://github.com/Matir/skel.git
synced 2026-05-25 21:19:09 -07:00
Cleanup skel
This commit is contained in:
46
bin/prune-broken-symlinks.sh
Executable file
46
bin/prune-broken-symlinks.sh
Executable file
@@ -0,0 +1,46 @@
|
||||
#!/bin/sh
|
||||
# shellcheck disable=SC2039,SC2086
|
||||
|
||||
set -o nounset
|
||||
|
||||
prune_broken_symlinks() {
|
||||
ask=1
|
||||
dir="."
|
||||
|
||||
if [ "${1:-}" = "-y" ]; then
|
||||
ask=0
|
||||
shift
|
||||
fi
|
||||
|
||||
if [ -n "${1:-}" ]; then
|
||||
dir="$1"
|
||||
fi
|
||||
|
||||
# Check if there are any broken symlinks first
|
||||
broken_links=$(find -L "$dir" -xdev -type l -print 2>/dev/null)
|
||||
if [ -z "$broken_links" ]; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
if [ "$ask" -eq 1 ]; then
|
||||
# Print broken links
|
||||
echo "$broken_links"
|
||||
|
||||
printf "Delete these links? [y/N] "
|
||||
read -r reply
|
||||
case "$reply" in
|
||||
[yY]*)
|
||||
;;
|
||||
*)
|
||||
echo "Aborted."
|
||||
return 0
|
||||
;;
|
||||
esac
|
||||
fi
|
||||
|
||||
# Perform deletion
|
||||
find -L "$dir" -xdev -type l -exec rm -- {} + 2>/dev/null
|
||||
}
|
||||
|
||||
# Execute the function
|
||||
prune_broken_symlinks "$@"
|
||||
Reference in New Issue
Block a user