Skip to content

Instantly share code, notes, and snippets.

@zhouji
Forked from zhasm/rm.sh
Created March 22, 2012 09:49
Show Gist options
  • Save zhouji/2157409 to your computer and use it in GitHub Desktop.
Save zhouji/2157409 to your computer and use it in GitHub Desktop.
safe rm command
logger ()
{
time=`TZ="Asia/Shanghai" date +"%Y-%m-%d %T"`;
echo "[$time] $*"
}
filesize ()
{
/bin/ls -alF "$1" | awk '{print $5}'
}
function rm(){
local limit=52428800 #50M.
if [ -d $HOME/.local/share/Trash/files ];then
trash="$HOME/.local/share/Trash/files"
else
trash="$HOME/.Trash"
fi
local log="/var/log/trash.log"
while [[ ! -z "$1" ]]
do
if [[ ! -d "$1" ]]; then
if [[ ! -f "$1" ]] ; then
shift
continue
fi
fi
full=`readlink -f "$1"`
base=`basename "$full"`
if [[ ` echo "$base" |grep "\." ` ]]; then
new=`echo "$base" |sed -e "s/\([^.]*$\)/$RANDOM.\1/" `
else
new="$base.$RANDOM"
fi
trash_file="$trash/$new"
local fs=`filesize "$full"`
if [ "$fs" -gt "$limit" ]; then
read -p "File is ${fs}c, too large. rm it permanently? [Y/n]" answer
case "$answer" in
"Y" | "" | "y" )
/bin/rm -v "$full"
logger "'$full' removed permanently";;
*)
logger "aborted from deleting $full";;
esac
shift
continue
fi
mv "$full" "$trash_file"
if [ $? -eq 0 ];then
if [ -w "$log" ]; then
logger "$full => [$trash_file]" |tee -a "$log"
else
logger "$full => [$trash_file]"
fi
else
logger "Error deleting $full"
fi
shift
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment