Skip to content

Instantly share code, notes, and snippets.

@loganhasson
Last active December 23, 2015 23:18
Show Gist options
  • Save loganhasson/6708664 to your computer and use it in GitHub Desktop.
Save loganhasson/6708664 to your computer and use it in GitHub Desktop.
Bash empty trash script
function empty () {
pushd ~/.Trash > /dev/null
tmp=$(rm -rfv * | wc -l | sed -e 's/^[ \t]*//')
if [ $tmp == "1" ]; then
echo "$tmp file was removed."
else
echo "$tmp files were removed."
fi
pushd > /dev/null
}
# Commented Version
# USE: empty
function empty () {
# push the CWD into the stack and change to the ~/.Trash directory. Pipe this output to /dev/null so it doesn't print to
# the console
pushd ~/.Trash > /dev/null
# assign a variable, tmp, to the following
# rm -rfv * deletes everything in the trash. the -v flag tells it to echo each file as it deletes
# this is then piped to wc -l, which counts the lines that were outputted
# that count gets a bunch of whitespace put in front of it for some reason, and is piped to the stream editor (sed), and the
# -e flag lets us pass a fancy regex to strip the leading whitespace
# I didn't write the regex...credit to a friendly StackOverflow user.
tmp=$(rm -rfv * | wc -l | sed -e 's/^[ \t]*//')
# check to see if $tmp is 1, if so we want to echo singular "file"
if [ $tmp == "1" ]; then
echo "$tmp file was removed."
else
# otherwise echo plural "files"
echo "$tmp files were removed."
fi
# pushd again brings us back to the directory we were in before running the command, and the output is again piped to
# /dev/null
pushd > /dev/null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment