📄 ch23_08.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 23] 23.8 Safe Delete: Pros and Cons </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly & Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:41:04Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-260-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch23_01.htm"TITLE="23. Removing Files"><LINKREL="prev"HREF="ch23_07.htm"TITLE="23.7 Safer File Deletion in Some Directories "><LINKREL="next"HREF="ch23_09.htm"TITLE="23.9 delete: Protecting Files from Accidental Deletion "></HEAD><BODYBGCOLOR="#FFFFFF"TEXT="#000000"><DIVCLASS="htmlnav"><H1><IMGSRC="gifs/smbanner.gif"ALT="UNIX Power Tools"USEMAP="#srchmap"BORDER="0"></H1><MAPNAME="srchmap"><AREASHAPE="RECT"COORDS="0,0,466,58"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="467,0,514,18"HREF="jobjects/fsearch.htm"ALT="Search this book"></MAP><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch23_07.htm"TITLE="23.7 Safer File Deletion in Some Directories "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 23.7 Safer File Deletion in Some Directories "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 23<BR>Removing Files</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch23_09.htm"TITLE="23.9 delete: Protecting Files from Accidental Deletion "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 23.9 delete: Protecting Files from Accidental Deletion "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-0391">23.8 Safe Delete: Pros and Cons </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-25426"></A>To protect themselves from accidentally deleting files, some userscreate a "trash" directory somewhere, and then write a "safe delete"program that, instead of <EMCLASS="emphasis">rm</EM>ing a file, moves it into the<EMCLASS="emphasis">trash</EM> directory. The implementation can be quite complex,but a simple alias will do most of what you want:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">alias del "mv \!* ~/trash"</PRE></BLOCKQUOTE></P><PCLASS="para">Of course, now your deleted files collect in your <EMCLASS="emphasis">trash</EM>directory, so you have to clean that out from time to time. You cando this either by hand or automatically, via a<SPANCLASS="link"><EMCLASS="emphasis">cron</EM> (<ACLASS="linkend"HREF="ch40_12.htm"TITLE="Periodic Program Execution: The cron Facility ">40.12</A>)</SPAN>entry like:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"><ACLASS="co"HREF="ch44_09.htm"TITLE="44.9 Testing Your Success ">&&</A> <ACLASS="co"HREF="ch23_17.htm"TITLE="23.17 Problems Deleting Directories ">-r</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">23 2 * * * cd $HOME/trash && rm -rf *</PRE></TD></TR></TABLE></P><PCLASS="para">This deletes everything in the trash directory at 2:23 a.m. daily.To restore a file that you deleted, you have to look through yourtrash directory by hand and put the file back in the right place.That may not be much more pleasant than poking through your garbage tofind the tax return you threw out by mistake, but (hopefully) youdon't make lots of mistakes.</P><PCLASS="para">There are plenty of problems with this approach. Obviously, if youdelete two files with the same name in the same day, you're going tolose one of them. A shell script could (presumably) handle thisproblem, though you'd have to generate a new name for the deletedfile. There are also lots of nasty side effects and "gotchas,"particularly if you want an <EMCLASS="emphasis">rm -r</EM> equivalent, ifyou want this approach to work on a network of workstations, or if youuse it to delete files that are shared by a team of users.</P><PCLASS="para">Unfortunately, this is precisely the problem. A "safe delete" thatisn't really safe may not be worth the effort. A safety net withholes in it is only good if you can guarantee in advance that youwon't land in one of the holes. You can patch some of the holes byreplacing this simple alias with a shell script. But you can't fixall of them. For a real solution, see Jonathan Kamens'article on<SPANCLASS="link"><EMCLASS="emphasis">delete</EM> (<ACLASS="linkend"HREF="ch23_09.htm"TITLE="delete: Protecting Files from Accidental Deletion ">23.9</A>)</SPAN>.</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">ML</SPAN></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch23_07.htm"TITLE="23.7 Safer File Deletion in Some Directories "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 23.7 Safer File Deletion in Some Directories "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="book"HREF="index.htm"TITLE="UNIX Power Tools"><IMGSRC="gifs/txthome.gif"SRC="gifs/txthome.gif"ALT="UNIX Power Tools"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch23_09.htm"TITLE="23.9 delete: Protecting Files from Accidental Deletion "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 23.9 delete: Protecting Files from Accidental Deletion "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">23.7 Safer File Deletion in Some Directories </TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="index"HREF="index/idx_0.htm"TITLE="Book Index"><IMGSRC="gifs/index.gif"SRC="gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172">23.9 delete: Protecting Files from Accidental Deletion </TD></TR></TABLE><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><IMGSRC="gifs/smnavbar.gif"SRC="gifs/smnavbar.gif"USEMAP="#map"BORDER="0"ALT="The UNIX CD Bookshelf Navigation"><MAPNAME="map"><AREASHAPE="RECT"COORDS="0,0,73,21"HREF="../index.htm"ALT="The UNIX CD Bookshelf"><AREASHAPE="RECT"COORDS="74,0,163,21"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="164,0,257,21"HREF="../unixnut/index.htm"ALT="UNIX in a Nutshell"><AREASHAPE="RECT"COORDS="258,0,321,21"HREF="../vi/index.htm"ALT="Learning the vi Editor"><AREASHAPE="RECT"COORDS="322,0,378,21"HREF="../sedawk/index.htm"ALT="sed & awk"><AREASHAPE="RECT"COORDS="379,0,438,21"HREF="../ksh/index.htm"ALT="Learning the Korn Shell"><AREASHAPE="RECT"COORDS="439,0,514,21"HREF="../lrnunix/index.htm"ALT="Learning the UNIX Operating System"></MAP></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -