Support glob expansion in trash() (#46287)
Enhance the trash() implementation to detect glob patterns in the target path, expand them, and move each matched file into the user's .Trash with a timestamp and random suffix. If no matches are found the function logs that the pattern doesn't exist. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Trash utility now supports glob patterns (`*`, `?`, `[]`) to remove multiple matching files at once. * Each trashed item receives a unique suffix to avoid filename collisions. * Per-file removal messages shown; a clear notification is printed when a pattern matches no files. <!-- review_stack_entry_start --> [](https://app.coderabbit.ai/change-stack/fleetdm/fleet/pull/46287?utm_source=github_walkthrough&utm_medium=github&utm_campaign=change_stack) <!-- review_stack_entry_end --> <!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
@@ -680,6 +680,31 @@ const trashFunc = `trash() {
|
||||
fi
|
||||
|
||||
local trash="/Users/$logged_in_user/.Trash"
|
||||
|
||||
# If the target contains glob characters, expand it and move each match.
|
||||
if [[ "$target_file" == *[*?[]* ]]; then
|
||||
local file file_name
|
||||
local matched=false
|
||||
local i=0
|
||||
# compgen -G expands the (quoted) pattern itself, so paths containing
|
||||
# spaces glob correctly; reading line by line keeps each match intact.
|
||||
while IFS= read -r file; do
|
||||
[[ -n "$file" ]] || continue
|
||||
[[ -e "$file" || -L "$file" ]] || continue
|
||||
matched=true
|
||||
i=$((i + 1))
|
||||
file_name="$(basename "$file")"
|
||||
echo "removing $file."
|
||||
# The per-match counter keeps matches that share a basename from
|
||||
# overwriting each other in the trash.
|
||||
mv -f "$file" "$trash/${file_name}_${timestamp}_${rand}_${i}"
|
||||
done < <(compgen -G "$target_file" 2>/dev/null)
|
||||
if [[ "$matched" == false ]]; then
|
||||
echo "$target_file doesn't exist."
|
||||
fi
|
||||
return
|
||||
fi
|
||||
|
||||
local file_name="$(basename "${target_file}")"
|
||||
|
||||
if [[ -e "$target_file" ]]; then
|
||||
|
||||
Reference in New Issue
Block a user