先将shell脚本放在某个全局路径下,如/usr/local/bin
#!/bin/sh
# safe rm
# Don't remove the file, just move them to a temporary directory.
# Files are grouped by remove time.
# e.g.
# # pwd => /home/work/
# > rm -r -f aa
# 'aa' will move to ~/.TrashHistory/20141018/aa@120111@_home_work_aa
RM_BACKUP_PATH=~/.Trashhistory
if [ ! -d "$RM_BACKUP_PATH" ]; then
mkdir "$RM_BACKUP_PATH"
fi
function safe_rm() {
# skip cmd option, e.g. '-rf' in 'rm -rf a b' or '-r/-f' in 'rm -r -f a b'
if [ $# -lt 1 ]; then
echo 'usage: rm -f [-dPRrvW] file ...'
exit 1
fi
first_char=${1:0:1}
until [ ! "$first_char" = "-" ]
do
shift
first_char=${1:0:1}
done
# check param
if [ $# -lt 1 ]; then
echo 'usage: rm -f [-dPRrvW] file ...'
exit 1
fi
today=`date +"%Y%m%d"`
mvpath=${RM_BACKUP_PATH}/$today
# support for multi version
timestamp=`date +"%H%M%S"`
# create dir if path non-exist
if [ ! -d $mvpath ]; then
mkdir $mvpath
fi
until [ $# -eq 0 ]
do
# fetch absolute path of the file
fpath=$1
fchar=`echo "${fpath:0:1}"`
if [ "$fchar" = "/" ]; then
dist_path="_${fpath}"
else
abs_fpath=`pwd`/$fpath
dist_path="${fpath}@${timestamp}@${abs_fpath}"
fi
# substitue '/' to '_'
final_dist_path=${dist_path////_}
# mv to temp trash
mv $fpath $mvpath/$final_dist_path
# next file
shift
done
}
然后alias
alias rm=". /usr/local/bin/safe_rm.sh && safe_rm "
并source