修改Linux下rm命令爲mv命令

Linux OS下替換rmmv, 防止文件誤刪

Linux下command操作,一個迷糊就可能遇到刪庫跑路的可能,比如殺手rm -fr *, 這裏提供一種方法替換Linux下的rmmv, 實現刪除文件備份的功能

操作方法

/root/.bashrc中添加如下內容

function rm_mv(){
	curr_date=$(date +%Y_%m_%d)
	resp=""
	if [ ! -e /tmp/${curr_date} ];then
		mkdir -p /tmp/${curr_date}
	fi
	if echo $1 | grep -q ^'-';then
		echo "Will delete those file : [31m$(echo $@ | cut -d ' ' -f 2-)[0m forcely !"
		resp="y"
	else
		read -p "Will delete those file : [31m$@[0m, right ? [Y|y]|[N|n] > " resp
	fi
	case ${resp} in
		"Y"|"y")
			true
			;;
		*)
			return
			;;
	esac
	
	if echo $1 | grep -q ^'-';then
		file_num=$(echo $@ | cut -d ' ' -f 2- | wc -w)
		flag=0
		for file in $(echo $@ | cut -d ' ' -f 2-)
		do
			if [ -e /tmp/${curr_date}/${file} ];then
				mv -f ${file} /tmp/${curr_date}/${file}_$(date +%s)
				if [ $? -eq 0 ];then
					let flag+=1
				fi
			else
				mv -f ${file} /tmp/${curr_date}
				if [ $? -eq 0 ];then
					let flag+=1
				fi
			fi	
		done
	else
		file_num=$(echo $@ | wc -w)
		flag=0
		for file in $(echo $@)
		do
			if [ -e /tmp/${curr_date}/${file} ];then
				mv -f ${file} /tmp/${curr_date}/${file}_$(date +%s)
				if [ $? -eq 0 ];then
					let flag+=1
				fi
			else
				mv -f ${file} /tmp/${curr_date}
				if [ $? -eq 0 ];then
					let flag+=1
				fi
			fi	
		done
	fi
	if [ ${flag} -eq ${file_num} ];then
		echo "[32mSuccessfully[0m !"
	else
		echo "[31mBUG - Unsuccessfully, [${flag}/${file_num} finished !][0m !"
	fi
}
alias rm='rm_mv'

定義一個新的函數alias到原來的系統命令rm。當執行rm文件的時候會提示用戶確認刪除,刪除成功會有返回。同時rm -fr還是可以用的,只是不會提示用戶確認。遇到刪除相同的文件名的時候,會在文件名後+日期備份。每天會按照日期在/tmp下生成備份文件夾。

注意,由於markdown無法正確地顯示部分字符code中的是CTRL+v+ESC組合鍵出來的, UTF-8是<0x1b>

/tmp爲備份文件夾:

刪除的時候的操作:

📌轉載請註明來源,版權歸作者**@hualong1009**所有, 謝謝

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章