Last active
August 29, 2015 14:20
-
-
Save Sebb767/5231c01ee80bcd83e010 to your computer and use it in GitHub Desktop.
Simple trash script for console
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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