Use the deleted document script

Source: Internet
Author: User
Since a deleted file directory exists in the deleted file script, and this directory is a hidden directory under the user's home directory, A program that allows users to retrieve the scripts for these deleted files is very useful. In any case, it is difficult to indicate all possible situations... info & nbs used the deleted document script since there is a directory of the deleted files, and this directory is a hidden directory under the user's home directory, A program that allows users to retrieve the scripts for these deleted files is very useful. In any case, it is difficult to indicate all possible situations, because it involves three situations: no matching, only one matching, and multiple matching. In multiple matching cases, for example, if you want to pick out the latest file and restore it? Or specify how many cases are matched successfully and then exit? Or is the data displayed in different situations available to users? Next, let's see what we can do... 001 #! /Bin/sh002 003 # unrm. sh -- search for the specified file or directory 004 in the deleted document # if there are multiple matches, a list of results sorted by timestamp is displayed. 005 # then, ask the user to specify which 006 mydir = "$ HOME /. deleted-files "008 realrm ="/bin/rm "009 move ="/bin/mv "010 011 dest = $ (pwd) 012 013 if [! -D $ mydir]; then014 echo "'basename $ 0': No deleted files directory: nothing to unrm "> & 2015 exit 1016 fi017 018 cd $ mydir019 020 if [$ #-eq 0]; then021 echo" Contents of your deleted files archive (sorted by data ): "-F in 022 # ls adds an indicator to the listed items. for example, if the file is not suffixed, the directory is added with a slash /, in the executable file ** 023 # ls,-C displays the 024 ls-FC by column | sed-e's/\ ([[: digit:] [[: digit:] \. \) \ {5 \} // g' \ 025-e's/^ // # This line is replaced with a space 026 exit 0027 fi02 8 029 # otherwise, we must use a user-specified mode. 030 # Let's see if this mode matches multiple files or directories in the document 031 032 matches = "$ (ls *" $1 "2>/dev/null | wc- l) "033 034 if [$ matches-eq 0]; then035 echo" No match for \ "$1 \" in the deleted file archive. "> & 2036 exit 1037 fi038 039 if [$ matches-gt 1]; then040 echo" More than one file or directory match in the archive: "041 index = 1042 #-t in ls is displayed according to the last modification time 043 #-d in ls is only the name of the Directory, instead of displaying the contents in the directory 044 for name in $ (ls-td * "$1") 045 do046 Datetime = "$ (echo $ name | cut-c1-14 | \ 047 awk-F. '{print $5 usd/" $4 "at" $3 ":" $2 ":" $1 }') "# use awk to format and output 048 if [-d $ name]; then049 size =" $ (ls $ name | wc-l | sed's/[^ [: digit:] // g') "# calculate the number of files in the $ name Directory 050 echo" $ index) $1 (contents =$ {size} itmes, deleted = $ datetime) "051 else052 size =" $(ls-sdk1 $ name | awk '{print $1}') "# Calculate the file size 053 echo" $ index) $1 (size = $ {size} Kb, deleted = $ datetime) "05 4 fi055 index =$ ($ index + 1 )) 056 done057 echo "" 058 echo-n "Which version of $1 do you want to restore ('0' to quit )? [1]: "059 read desired060 if [$ {desired: = 1}-ge $ index]; then061 echo" $ (basename $0): Restore canceled by user: index value too big. "> & 2062 exit 1063 fi064 065 if [$ desired-lt 1]; then066 echo" $ (basename $0): canceled by user. "> & 2067 exit 1068 fi069 070 restore =" $(ls-td1 * "$1" | sed-n "$ {desired} p ") "071 072 if [-e" $ dest/$1 "]; then073 echo" \ "$1 \" already exists in this directory. ca Nnot overwrite. "> & 2074 exit 1075 fi076 077 echo-n" Restoring file \ "$1 \"... "078 $ move" $ restore "" $ dest/$1 "079 echo" done. "080 081 echo-n" Delete the additional copies of this file? [Y]: "082 read answer083 084 if [$ {answer: = y} =" y "]; then085 $ realrm-rf * "$1" 086 echo "deleted" 087 else088 echo "additional copies retained. "089 fi090 else091 if [-e" $ dest/$1 "]; then092 echo" \ "$1 \" already exists in this directory. cannot Overwrite. "> & 2093 exit 1094 fi095 096 restore =" $ (ls-d * "$1") "097 098 echo-n" Restoring file \ "$1 \"... & quot; 099 $ move & quot; $ restore & quot; $ dest/$1 & quot; 100 echo & quot; done. "1 01 fi102 103 how the exit 0 script works: the first large block of code, if [$ #-eq 0] condition block, will be executed according to this: if no parameter is specified, delete all content in the document. But there is something hidden here. We cannot display real file names, because we don't want users to see timestamps. These timestamps are used internally to protect file names from conflict. To display the file name in a more elegant way, the sed expression deletes the first five fields. If a parameter is specified, it is the file name or directory to be restored. The next step is to find out how many matching names are given. The following statement completes this function: 1 matches = "$ (ls *" $1 "2>/dev/null | wc-l) "The ls parameters contain uncommon quotation marks. they are used to ensure that the pattern matches the embedded blank file name, while the wildcard '*' is appropriately extended by shell. 2>/dev/null saves the error information generated in the command and will not be displayed to the user. The vast majority of discarded information may be No such file or directory, which is generally caused by the failure to find the specified file name. If there are multiple matches for a given file or directory name, the most complex script part is the if [$ matches-gt 1] block. This block shows all the results. Use the-t option in the ls command of the for loop to display the document in the order from new to old. Then the awk statement separates the timestamp in the file name. The-k option included in the following ls is used to calculate the size of a file using kilobytes (kb) as the unit, rather than the usual bytes. 1 size = "$ (ls-sdk1 $ name | awk '{print $1}')" The script displays the number of files in each matching directory, instead of explicitly displaying the size of matching File items. In fact, it is easy to calculate the number of items in a directory. run the wc command: 1 size = "$ (ls $ name | wc-l | sed's/[^ [: digit:] // g') "Once a user gives a possible matching file or directory, the corresponding extension will be defined by the following statement: 1 restore = "$ (ls-td1 *" $1 "| sed-n" $ {desired} p ")" This sentence contains an alternative usage of sed. Use the-n option, followed by a number followed by the print command p ($ {desired}). This method can quickly extract rows of a given row number from the input stream. Run the script: There are two methods to call the script. First, there is no parameter. it will display all the files and directories in the deleted document. Second, there is a required file or directory name as the parameter. the script will either restore the file or directory (if there is only one match), or display all the candidate lists that can be recovered, in this way, you can select one. Running result: If no parameters are displayed, the script displays all the deleted documents: 1unrm. sh2Contents of your deleted files archive (sorted by data): 3.a.txt. a. o *. adir/has a file name as the parameter: 1unrm. sh a.txt 2 More than one file or directory match in the archive: 3 1) a.txt (size = 4Kb, deleted = 11/18 at 17:15:45) 4 2) a.txt (size = 4Kb, deleted = 11/18 at 17:15:10) 5 6 Which version of a.txt do you want to restore ('0' to quit )? [1] 27 Restoring file "a.txt"... done.8 Delete the additional copies of this file? [Y] y9 deleted analysis script: If you execute this script, there is a potential danger to be aware. If there is no control or restriction, there will be no limit to the number of files or directories in the document to be deleted. To avoid this, you can add a cronjob to reduce the number of documents. The document retained within 14 days should be reasonable at that time.
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.