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,