Skip to content

Instantly share code, notes, and snippets.

@zQueal
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save zQueal/dfa4f7301c932f4f084a to your computer and use it in GitHub Desktop.

Select an option

Save zQueal/dfa4f7301c932f4f084a to your computer and use it in GitHub Desktop.
A simple trash system for UNIX.
TRASH_DIR=~/.Trash # Set the Trash directory
mkdir -p "$TRASH_DIR" # Create it if it's not already created
trash() {
# list trash dir if no arguments given
if [[ -z $1 ]]; then
if _bash_trash_is_empty; then # call _bash_trash_is_empty function, then
echo "$TRASH_DIR: trash is empty" # return that the trash is empty
else
du -csh "$TRASH_DIR"/* # else list the contents of the trash directory
fi
return # return the output
fi
local f
for f in "$@"; do # for 'f' in $@ (working dir) do
local b=$(basename "$f") # set base file name
local fname=$b # set name to $b
local i=1 # set $i to 1
while [[ -e $TRASH_DIR/$fname ]]; do
fname=$b.$i
((i++)) # count the number of files to be moved
done
mv "$f" "$TRASH_DIR/$fname" # execute 'mv' on $f based on $i
done
}
emptytrash() {
command rm -rf "$TRASH_DIR"/* # remove all files in $TRASH_DIR with 'rm'
}
_bash_trash_is_empty() {
(
shopt -s nullglob
files=("$TRASH_DIR"/*)
[[ -z ${files[0]} ]] # no files found, so trash is empty
)
return $?
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment