summary refs log tree commit diff
path: root/bin
diff options
context:
space:
mode:
Diffstat (limited to 'bin')
-rwxr-xr-xbin/saferm49
1 files changed, 35 insertions, 14 deletions
diff --git a/bin/saferm b/bin/saferm
index 467fec2..10c59a7 100755
--- a/bin/saferm
+++ b/bin/saferm
@@ -1,5 +1,8 @@
 #!/bin/bash
 
+set -e
+[ -n "$DEBUG" ] && set -x
+
 if [ ! -t 1 ] || [ -n "$UNSAFE_RM" ]; then
 	# shellcheck disable=SC2068
 	exec /bin/rm "$@"
@@ -33,22 +36,40 @@ if [ -n "${force}" ] && [ -n "${recurse}" ]; then
 	exit 1
 fi
 
-tput setaf 1
-(
-	for ((idx = OPTIND; idx <= $#; idx++)); do
+rm_prompt_arg=-I
+files=()
+for ((idx = OPTIND; idx <= $#; idx++)); do
+	if [ -e "${!idx}" ]; then
 		if [ -d "${!idx}" ]; then
-			exa --color=never --all --tree "${!idx}" >&2
-			echo "!"
-			echo "!" "$(du -sh "${!idx}")"
-			echo "!" "$(fd --no-ignore --type file . "${!idx}" | wc -l)" "files"
-			echo "!" "$(fd --no-ignore --type directory . "${!idx}" | wc -l)" "directories"
-			echo "!"
+			tput setaf 1
+			(
+				exa --color=never --all --tree "${!idx}" >&2
+				echo "!"
+				echo "!" "$(du -sh --total "${!idx}" | grep total)"
+				echo "!" "$(fd --no-ignore --type file . "${!idx}" | wc -l)" "files"
+				echo "!" "$(fd --no-ignore --type directory . "${!idx}" | wc -l)" "directories"
+				echo "!"
+			) >&2
+			tput sgr0
 		else
-			echo "${!idx}"
+			tput setaf 1
+			echo "${!idx}" >&2
+			files+=("${!idx}")
 		fi
-	done
-) >&2
-tput sgr0
+	fi
+done
+
+if [ ${#files[@]} -gt 0 ]; then
+	rm_prompt_arg=-i
+	tput setaf 1
+	(
+		echo "!"
+		echo "!" "$(du -sh --total "${files[@]}" | grep total)"
+		echo "!" "${#files[@]}" "files"
+		echo "!"
+	) >&2
+	tput sgr0
+fi
 
 # shellcheck disable=SC2068
-exec /bin/rm -I --one-file-system "$@"
+exec /bin/rm ${rm_prompt_arg} --one-file-system "$@"