The RM command on a Linux system is too dangerous to accidentally delete system files. Write a shell script to replace the system's RM command,

Source: Internet
Author: User
Tags rsync

The RM command on a Linux system is too dangerous to accidentally delete system files. Write a shell script to replace the system's RM command, requiring a backup to be made when deleting a file or directory, and then deleting it.

Suppose you have a large partition/data/, each time you delete a file or directory, you need to create a hidden directory under/data/, named after the date/time, such as/data/.201802021054, and then sync all the deleted files to the directory, using Rsync- R synchronize the file paths together


Answer:

#!/bin/bashd= ' Date +%y%m%d%h%m ' read-p ' is you sure delete the file or directory $? Y|n: "CIF [$c = =" Y "] | | [$c = = "Y"]then mkdir/data/. $d rsync-ar $1//data/. $d/bin/rm-rf $1elif [$c = = "N"] | | [$c = = "N"]then exit 0else echo "Please input Y or n" fi

Parsing: First declare a variable $d, the execution result of the date command is assigned to the variable d, where the use of anti-quotation marks (""), the function is to return the result of the command in the anti-quotation marks. Use Read-p to assign the input result to the variable $c.


Use if to judge, here first explain the role of a few commands, [] inside the parentheses is the condition of the judgment, both sides have a space, the condition of the "= =" on both sides also have a space, | | I mean, yes or No. So, the whole paragraph means: If $c is all equal to the string y or Y, execute mkdir Create folder, execute rsync synchronization command, and finally execute the delete command. If, $c is all equal to the string n or N, then exit, otherwise (both cases are not included, output echo "Please input Y or n").


Separate description Rsync-ar $1//data/. $d. In fact, the complete writing should be Rsync-ar $1//data/. $d/$1, but if the value of $ is an absolute path, such as:/tmp/1.txt, then the surrogate finds

rsync-ar/tmp/1.txt/data/.201802021122//tmp/1.txt. One more / will error, so omit the /$1, avoid this situation.


-------Exercise Source Amin Linux--------

The above is for personal understanding, please point out, thank you.

The RM command on a Linux system is too dangerous to accidentally delete system files. Write a shell script to replace the system's RM command,

Related Article

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.