#!/usr/bin/env bash
# a command or alias to replace 'rm' with a safe and easy to use, safe remove files or directories
# See also:
# safe-rm - wrapper around the rm command to prevent accidental deletions - https://github.com/kaelzhang/shell-safe-rm
# trash-cli - Command line interface to FreeDesktop.org Trash - https://pypi.python.org/pypi/trash-cli/
# debug option
DEBUG=false # DEBUG=true
if ${DEBUG} ; then
old_PS4=$PS4
# export PS4='+${BASH_SOURCE}:${LINENO}:${FUNCNAME[0]}: '
export PS4='+${LINENO}: ${FUNCNAME[0]}: ' # if there is only one bash script, do not display ${BASH_SOURCE}
_XTRACE_FUNCTIONS=$(set +o | grep xtrace)
set -o xtrace
fi
# set an empty function using for location this line quickly in PyCharm editor on purpose.
function _empty() { return; }
# Public header
# =============================================================================================================================
# resolve links - $0 may be a symbolic link
# learn from apache-tomcat-6.x.xx/bin/catalina.sh
PRG="$0"
while [ -h "$PRG" ]; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
else
PRG=`dirname "$PRG"`/"$link"
fi
done
# Get standard environment variables
PRGDIR=`dirname "$PRG"`
# echo color function, smarter, learn from lnmp.org lnmp install.sh
function echo_r (){
# Color red: Error, Failed
[ $# -ne 1 ] && return 1
echo -e "\033[31m$1\033[0m"
}
function echo_g (){
# Color green: Success
[ $# -ne 1 ] && return 1
echo -e "\033[32m$1\033[0m"
}
function echo_y (){
# Color yellow: Warning
[ $# -ne 1 ] && return 1
echo -e "\033[33m$1\033[0m"
}
function echo_b (){
# Color blue: Debug Level 1
[ $# -ne 1 ] && return 1
echo -e "\033[34m$1\033[0m"
}
function echo_p (){
# Color purple,magenta: Debug Level 2
[ $# -ne 1 ] && return 1
echo -e "\033[35m$1\033[0m"
}
function echo_c (){
# Color cyan: friendly prompt, Level 1
[ $# -ne 1 ] && return 1
echo -e "\033[36m$1\033[0m"
}
# end echo color function, smarter
#WORKDIR="`realpath ${WORKDIR}`"
WORKDIR="`readlink -f ${PRGDIR}`"
# end public header
# =============================================================================================================================
real_rm='/bin/rm'
trash_dir="$HOME/.trash" # if do not use "$HOME" or "~" to resolve permission problem, should use "chmod o+t $trash_dir" .chmod --help: Each MODE is of the form `[ugoa]*([-+=]([rwxXst]*|[ugo]))+'.
log_dir="$trash_dir"
log_file="$log_dir/operation.log"
trash_save_days=3
function real_rm() {
if [[ ! -f ${real_rm} ]]; then
echo 'safe-rm cannot find the real "rm" binary'
exit 1
fi
save_days=${trash_save_days:-10}
test $(find -L /tmp/.delete/ -type d ! -name "^." -a ! -wholename "/tmp/.delete/" -mtime +${save_days} -exec echo '{}' \; | wc -l ) -gt 0
found_old_files=$?
if [[ ${found_old_files} -eq 0 ]]; then
echo_b "old files found, cleaning"
#find -L ${trash_dir}/ -maxdepth 1 -type d ! -name "^." -mtime +${save_days} -exec rm -rf '{}' \;
find -L ${trash_dir}/ -maxdepth 1 -type d ! -wholename "$trash_dir/" ! -name "^." -mtime +${save_days} -exec rm -rf '{}' \;
echo_g "old files cleaned successfully"
else
echo_g "old files in standby state, passed"
fi
}
function safe_rm() {
if [[ "$1x" = 'x' ]]; then
${real_rm} --help
exit 1
fi
if [[ ! -d ${trash_dir} ]]; then
mkdir -p ${trash_dir}
fi
# date +%Y%m%d%H%M%S.%N | shasum | awk '{print $1}' | cat - -A
uniq_trash_dir="$trash_dir/$(date +%Y%m%d%H%M%S.%N | shasum | awk '{print $1}')"
mkdir -p ${uniq_trash_dir}
if [[ $# -eq 1 ]];then
if [[ -f $1 ]] || [[ -d $1 ]]; then # ignore rm -f|-r|-rf|-fr, etc
mv $1 ${uniq_trash_dir}
retval=$?
fi
else
# alternative impl of 'rm FILE...'
parameter_array="$@"
# IFS=' '$'\t'$'\n', IFS=$' \t\n', If IFS is unset, or its value is exactly
old_IFS=$IFS
IFS=' '$'\t'$'\n'
for parameter in ${parameter_array}; do
if [[ -f ${parameter} ]] || [[ -d ${parameter} ]]; then # ignore rm -f|-r|-rf|-fr, etc
mv ${parameter} ${uniq_trash_dir}
fi
done
retval=$?
IFS="$old_IFS"
fi
log_operation $@
exit ${retval}
}
function log_operation(){
tee -a ${log_file}
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com