Archive the deleted file

Source: Internet
Author: User
One of the most common problems for Unix users who Archive deleted files is that they cannot restore accidentally deleted files and directories. After you type rmxyz, once you press the RETURN key, everything is cloudification. One solution to this problem is the secret and automatic archiving of files and directories. Imagine one of the most common problems for Unix users who use archive scripts to archive deleted files is that they cannot restore accidentally deleted files and directories. After you type rm xyz, once you press the RETURN key, everything is cloudization. One solution to this problem is the secret and automatic archiving of files and directories. Imagine what the basic work in this script can do to make the user imperceptible. 01 #! /Bin/sh02 03 # newrm. sh, an alternative to the rm command. 04 # by creating and using a new directory under the user's home directory, a non-delete capability is provided. 05 # it can process directories, you can also process independent files. 06 # If you provide the-f option, the files will not be archived during deletion. 07 08 # Important warning: if you want to automatically delete junk directories, so do not use 09 # this script, because it means that you have not actually deleted anything, and your disk space will eventually fill up 10 11 mydir = "$ HOME /. deleted-files "12 realrm ="/bin/rm "13 copy ="/bin/cp-R "14 15 if [$ #-eq 0]; then # let 'RM' output usage error message 16 exec $ realrm # Current shell replaced by/bin/rm 17 fi18 19 # parse all options to find '-f' 20 21 flags = "" 22 23 w Hile getopts "dfiPRrvW" opt; do24 case $ opt in25 f) exec $ realrm "$ @"; # exec let's exit this script 26 *) flags = "$ flags-$ opt"; # Other options are for 'rm ', not our 27 esac28 done29 shift $ ($ OPTIND-1 )) 30 31 # Make sure $ mydir has 32 33 if [! -D $ mydir]; then34 if [! -W $ HOME]; then35 echo "$0 failed: can't create $ mydir in $ HOME "> & 236 exit 137 fi38 mkdir $ mydir39 chmod 700 $ mydir # Please provide privacy 40 fi41 42 for arg43 do44 newname =" $ mydir/$ (date "+ % S. % M. % H. % d. % m. "). $ (basename "$ arg") "45 if [-f" $ arg "]; then46 $ copy "$ arg" "$ newname" 47 elif [-d "$ arg"]; then48 $ copy "$ arg" "$ newname" 49 fi50 done51 52 exec $ realrm $ flags "$ @" # how the script works when our shell is replaced by realrm: this script is worth thinking about.. Note that most error messages are generated by calling realrm, whether it is an incorrect option, a file, or a directory name. Similarly, the exec command can replace the current process with a new specified process. Once exec calls realrm, it actually exits the script. we confirm that the return code obtained from the realrm (/bin/rm) process is returned to the called shell and is not discarded. Because this script creates a directory in the user's home directory in private, it needs to make sure that the file there cannot be accidentally readable to others because the umask value is not set. Therefore, this script uses the chmod command. Finally, a bit confusing file naming uses the basename command to remove all path information, and then adds a time and date stamp to name the deleted file. the specific format is second. minute.. days. month. file name 1 newname = "$ mydir/$ (date" + % S. % M. % H. % d. % m. "). $ (basename "$ arg") "pay attention to two $ () applications. This may be complicated but useful. Remember, everything between $ (and) is provided to a sub-shell, and the command result is replaced. Why use a troublesome timestamp? Because we delete a large number of files, it is inevitable that there are duplicate names. Run the script: to import this script, simply add an alias, so that you can replace it with the script when typing rm, instead of the original rm command. The alias command for Bash/Ksh is: 1 alias rm = yourpath/newrm. sh note: If this alias is canceled, unalias rm is used. Running result: directly run the command on the command line, and then go to the hidden directory under your home directory. deleted-files to view it.

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.