📄 ch23_02.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 23] 23.2 rm and Its Dangers </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:40:56Z"><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_01.htm"TITLE="23.1 The Cycle of Creation and Destruction "><LINKREL="next"HREF="ch23_03.htm"TITLE="23.3 Tricks for Making rm Safer "></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_01.htm"TITLE="23.1 The Cycle of Creation and Destruction "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 23.1 The Cycle of Creation and Destruction "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_03.htm"TITLE="23.3 Tricks for Making rm Safer "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 23.3 Tricks for Making rm Safer "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-1690">23.2 rm and Its Dangers </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="UPT-ART-1690-IX-RM-COMMAND"></A><ACLASS="indexterm"NAME="UPT-ART-1690-IX-DELETING-FILES"></A><ACLASS="indexterm"NAME="AUTOID-25171"></A><ACLASS="indexterm"NAME="AUTOID-25174"></A>Under UNIX, you use the <EMCLASS="emphasis">rm</EM> command to delete files. The commanditself is simple enough; you just type <CODECLASS="literal">rm</CODE> followed by a list offiles. If anything, <EMCLASS="emphasis">rm</EM> is too simple. It's easy to delete morethan you want, and once something is gone, it's permanently gone.There are a few hacks that make <EMCLASS="emphasis">rm</EM> somewhat safer, and we'll getto those momentarily. But first, a lecture on some of the dangers.</P><PCLASS="para">To understand why it's impossible to reclaim deleted files, you needto know a bit about how the UNIX filesystem works.The system contains a "free list," which is a list of disk blocksthat aren't used.When you delete a file, its directory entry (whichgives it its name) is removed.If there are no more<SPANCLASS="link">links (<ACLASS="linkend"HREF="ch18_03.htm"TITLE="Files with Two or More Names">18.3</A>)</SPAN>to the file(i.e., if the file only had one name), its<SPANCLASS="link">inode (<ACLASS="linkend"HREF="ch01_22.htm"TITLE="How UNIX Keeps Track of Files: Inodes ">1.22</A>)</SPAN>is added to the listof free inodes, and its datablocks are added to the free list.</P><PCLASS="para">Well, why can't you get the file back from the free list? After all,there are DOS utilities that can reclaim deleted files by doingsomething similar. Remember, though, UNIX is a multi-taskingoperating system. Even if you think your system is a single-usersystem, there are a lot of things going on "behind your back":daemons are writing to log files, handling network connections,processing electronic mail, and so on. You could theoreticallyreclaim a file if you could "freeze" the filesystem the instant yourfile was deleted - but that's not possible. With UNIX, everything isalways active. By the time you realize you made a mistake, yourfile's data blocks may well have been re-used for something else.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-25186"></A><ACLASS="indexterm"NAME="AUTOID-25189"></A>When you're deleting files, it's particularly important to usewildcards carefully. Simple typing errors can have disastrousconsequences. Let's say you want to delete all yourobject (<EMCLASS="emphasis">.o</EM>) files. But because of a nervous twitch, you add anextra space and type:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>rm * .o</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">It looks right, and you might not even notice the error. But beforeyou know it, all the files in the current directory will be gone.Irretrievably.</P><PCLASS="para">If you don't think this can happen to you, here's something thatactually did happen to me. At one point, when I was a relatively newUNIX user, I was working on mycompany's business plan. The executives thought that, to be "secure,"they'd set a business plan's permissions so you had to be<SPANCLASS="link"><EMCLASS="emphasis">root</EM> (<ACLASS="linkend"HREF="ch01_24.htm"TITLE="The Superuser (Root) ">1.24</A>)</SPAN>tomodify it. (A mistake in its own right, but that's another story.) Iwas using a terminal I wasn't familiar with, and accidentally managedto create a bunch of files with four control characters at thebeginning of their name. To get rid of these, I typed (as <EMCLASS="emphasis">root</EM>):</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"># <CODECLASS="userinput"><B>rm ????*</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">This command took a <EMCLASS="emphasis">long</EM> time to execute. When about two-thirdsof the directory was gone, I realized (with horror) what washappening: I was deleting all files with four or more characters inthe filename.</P><PCLASS="para">The story got worse. They hadn't made a backup in about fivemonths. (By the way, this article should give you plenty of reasonsfor making regular<SPANCLASS="link">backups (<ACLASS="linkend"HREF="ch20_02.htm"TITLE="Make Your Own Backups ">20.2</A>)</SPAN>.)By the time I had restored the files I had deleted (a several-hourprocess in itself; this was on an ancient version of UNIX with ahorrible <EMCLASS="emphasis">backup</EM> utility) and checked (by hand) all the filesagainst our printed copy of the business plan, I had resolved to be<EMCLASS="emphasis">very careful</EM> with my <EMCLASS="emphasis">rm</EM> commands.</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_01.htm"TITLE="23.1 The Cycle of Creation and Destruction "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 23.1 The Cycle of Creation and Destruction "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_03.htm"TITLE="23.3 Tricks for Making rm Safer "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 23.3 Tricks for Making rm Safer "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">23.1 The Cycle of Creation and Destruction </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.3 Tricks for Making rm Safer </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 + -