Linux Learning Summary (67) RM command Limit script

Source: Internet
Author: User
Tags rsync

Requirements background:
The Linux system's RM-RF command is too dangerous to remove the system files in one step. Write a shell script to replace the command, requiring that when a file or directory is deleted, a backup is made and then deleted.
Here are two things to do in practice:
1 Simple:
Suppose there is a large partition/data/, each time you delete a file or directory, you must first create a hidden directory under/data/, named after the date/time, such as/data/201703271012/, and then synchronize all deleted files to the directory, you can use rsync- R synchronizes the file paths together.

#!/bin/bashfilname=$1big_filesystem=/data/if [ ! -e $1 ];then    echo "$1 不存在,请使用绝对路径"    exitfid=`date +%F-%T`read -p "你确定要删除该文件 $1 吗?y|n:" ccase $c iny|Y)    mkdir -p $big_filesystem/.$d && rsync -aR $1 $big_filesystem/.$d/  && /bin/rm -rf $1    ;;n|N)    exit 0    ;;*)echo "请输入‘y‘或者‘n‘.";;esac

2 Complexity:
Do not know which partition has the remaining space, before deleting the file or directory size to delete, and then compare the system's disk space, if enough to follow your rules above to create a hidden directory, and back up, if there is not enough space, to remind users that there is not enough space to back up and prompt whether to discard the backup, if the user chooses Y, Delete the file or directory directly, if you select N, the prompt is not deleted, and then exit the script.

Analysis: The main difficulty of this requirement is to calculate the size of the directory or file, and to calculate the maximum partition size for the remaining disk space in this machine. Both of us use the regular to find the corresponding value in kilobytes. And then you can do the comparison.

f_size=du -sk filename |awk ‘{print $1}‘ //文件大小disk_size=df -k |sed -n ‘2,$‘p |sort -n -k4 |awk ‘{print $4}‘ |tail -1  //磁盘剩余最大空间大小big_filesystem=df -k |sed -n ‘2,$‘p |sort -n -k4 |tail -1 |awk ‘{print $NF}‘  //磁盘最大剩余空间挂载点
#!/bin/bashfilename=$1if [ ! -e $1 ];thenecho "$1 不存在,请使用绝对路径。"exitfid=`date +%F-%T`f_size=`du -sk $1 |awk ‘{print $1}‘`disk_size=`df -k |sed -n ‘2,$‘p |sort -n -k4 |awk ‘{print $4}‘ |tail -1`big_filesystem=`df -k |sed -n ‘2,$‘p |sort -n -k4 |tail -1 |awk ‘{print $NF}‘`if [ $f_size -lt $disk_size ];thenread -p "你确定要删除文件 $1 吗?y|n:" ccase $c iny|Y)    ;;n|N)    exit 0    ;;*)echo "请输入‘y‘或者‘n‘.";;esacelseecho "空间不足,无法备份文件$1"read -p "你确定要删除文件 $1 吗?y|n:" ccase $c iny|Y)echo "将会在5秒中之后进行无备份删除文件"for i in `seq 1 5`;do echo -ne ".";sleep 1;doneecho/bin/rm -rf $1;;n|N)echo “删除取消”exit 0;;*)echo "请输入‘y‘或者‘n‘.";;esacfi

Linux Learning Summary (67) RM command Limit script

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.