Skip to content

Instantly share code, notes, and snippets.

@Sebb767
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sebb767/5231c01ee80bcd83e010 to your computer and use it in GitHub Desktop.
Save Sebb767/5231c01ee80bcd83e010 to your computer and use it in GitHub Desktop.
Simple trash script for console
#!/bin/bash
# trash
#
# Script by (c) Sebb767, 2015
# A simple helper tool to move things to a
# specific directory instead of deleting
# them instantly.
# Usage: ./trash <files/directories>
#
# The files will be moved to $TRASHDIR (if set) or /tmp/trash-$(id -u)
trashdir=${TRASHDIR-"/tmp/trash-$(id -u)"}
mkdir -p "$trashdir"
# move all given files
for file in "$@"
do
path="$(readlink -f $file)"
parent="$(readlink -f $(dirname "$file"))"
if [[ -d "$file" ]]; then # it's a directory
mkdir -p "$trashdir$parent" # create the parent directory
mv "${path}" "$trashdir$parent" # move the actual dir
elif [[ -f "$file" ]]; then # it's a file
mkdir -p "$trashdir$parent" # create the containing directory
mv "$file" "$trashdir$path" # move the file
else
echo "$file doesn't exist!"
continue;
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment