#!/bin/bash set -e function helpexit() { echo Delete files in background, by moving the to a temp folder first. echo This command is always recursive! echo ' passing -i will ask first' exit } [[ -z "$1" ]] && helpexit [[ "$1" = "-h" ]] && helpexit CONFIRM=0 for k in "$@"; do if [[ "$k" = "-i" ]]; then CONFIRM=1 fi done tempfolders=() for f in "$@"; do if [[ ! -e "$f" ]]; then if [[ "$f" = "-i" ]]; then continue; fi echo "$f" does not exist continue fi d=$( abs-path $( dirname "$f" ) )/.rm_bg.$$ if [[ $CONFIRM -eq 1 ]]; then echo "Delete '$f'?" read -r -p "Are you sure? [y/N] " response case "$response" in [yY][eE][sS]|[yY]) true ;; *) continue ;; esac fi mkdir -p "$d" mv "$f" "$d"/ tempfolders+=( "$d" ) done ( for (( i=0; $i<${#tempfolders[@]}; i++ )); do rm -rf "${tempfolders[$i]}" done ) &