Skip to content

Instantly share code, notes, and snippets.

@zhasm
Created March 22, 2012 08:27
Show Gist options
  • Save zhasm/2157145 to your computer and use it in GitHub Desktop.
Save zhasm/2157145 to your computer and use it in GitHub Desktop.
safer rm command
logger ()
{
time=`TZ="Asia/Shanghai" date +"%Y-%m-%d %T"`;
echo "[$time] $*"
}
rm ()
{
local limit=50;
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 [[ -n ` echo "$base" |grep "\." ` ]]; then
new=`echo "$base" |sed -e "s/\([^.]*$\)/$RANDOM.\1/" `;
else
new="$base.$RANDOM";
fi;
trash_file="$trash/$new";
local fs=`du -BM -s "$full" |awk -FM '{print $1}'`;
if [ "$fs" -gt "$limit" ]; then
read -p "File/Folder is ${fs}Mb, too large. rm it permanently? [Y/n]" answer;
case "$answer" in
"Y" | "" | "y")
/bin/rm -rv "$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