⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch23_07.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 23] 23.7 Safer File Deletion in Some Directories </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly &amp; Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:41:02Z"><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_06.htm"TITLE="23.6 A Faster Way to Remove Files Interactively "><LINKREL="next"HREF="ch23_08.htm"TITLE="23.8 Safe Delete: Pros and Cons "></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_06.htm"TITLE="23.6 A Faster Way to Remove Files Interactively "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 23.6 A Faster Way to Remove Files Interactively "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_08.htm"TITLE="23.8 Safe Delete: Pros and Cons "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 23.8 Safe Delete: Pros and Cons "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-0408">23.7 Safer File Deletion in Some Directories </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-25356"></A><ACLASS="indexterm"NAME="AUTOID-25358"></A><ACLASS="indexterm"NAME="UPT-ART-408-IX-DELETING-FILES-SAFEGUARDS-AGAINST"></A><ACLASS="indexterm"NAME="AUTOID-25365"></A>Using<SPANCLASS="link"><EMCLASS="emphasis">noclobber</EM> (<ACLASS="linkend"HREF="ch13_06.htm"TITLE="Safe I/O Redirection with noclobber ">13.6</A>)</SPAN>and read-only files only protects you from a few occasional mistakes.A potentially catastrophic error is typing:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>rm * .o</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">instead of:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>rm *.o</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">&#13;In the blink of an eye, all of your files would be gone.A simple, yet effective, preventive measure is to create a file called<EMCLASS="emphasis">-i</EM>in the particular directory in which you want extra protection:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"><ACLASS="co"HREF="ch21_07.htm"TITLE="21.7 Setting File Modification Time with touch ">touch</A> <ACLASS="co"HREF="ch23_14.htm"TITLE="23.14 Handling a Filename Starting with a Dash (-) ">./-</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">% <CODECLASS="userinput"><B>touch ./-i</B></CODE></PRE></TD></TR></TABLE></P><PCLASS="para">In the above case, the<CODECLASS="literal">*</CODE> is expanded to match all of the filenames in the directory.Because the file<EMCLASS="emphasis">-i</EM>is<SPANCLASS="link">alphabetically listed (<ACLASS="linkend"HREF="ch51_03.htm"TITLE="ASCII Characters: Listing and Getting Values ">51.3</A>)</SPAN>before any file except those that start withone of these characters:<CODECLASS="literal">!#$%&amp;`()*+,</CODE>,the<EMCLASS="emphasis">rm</EM>command sees the <EMCLASS="emphasis">-i</EM>file as a command-line argument.When <EMCLASS="emphasis">rm</EM>is executed with its<SPANCLASS="link"><EMCLASS="emphasis">-i</EM> option (<ACLASS="linkend"HREF="ch21_11.htm"TITLE="Safer Removing, Moving, and Copying ">21.11</A>)</SPAN>,files will not be deleted unless you verify the action.This still isn't perfect. If you have a file that starts with a comma(<CODECLASS="literal">,</CODE>)in the directory, it will come before the file starting with a dash,and <EMCLASS="emphasis">rm</EM>will not get the<EMCLASS="emphasis">-i</EM>argument first.</P><PCLASS="para">The<EMCLASS="emphasis">-i</EM>file also won't save you from errors like:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>rm [a-z]* .o</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">[Two comments about Bruce's classic and handy tip:first, if lots of users each make a <EMCLASS="emphasis">-i</EM> file in each of their zillionsof subdirectories, that could waste a lot of disk<SPANCLASS="link">inodes (<ACLASS="linkend"HREF="ch01_22.htm"TITLE="How UNIX Keeps Track of Files: Inodes ">1.22</A>)</SPAN>.It might be better to make one <EMCLASS="emphasis">-i</EM> file in your home directoryand<SPANCLASS="link">hard link (<ACLASS="linkend"HREF="ch24_04.htm"TITLE="Save Space with a Link ">24.4</A>)</SPAN>the rest to it, like:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist">&#13;<ACLASS="co"HREF="ch14_11.htm"TITLE="14.11 Finding (Anyone's) Home Directory, Quickly ">~</A> &#13;</PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">% <CODECLASS="userinput"><B>cd</B></CODE>% <CODECLASS="userinput"><B>touch ./-i</B></CODE>% <CODECLASS="userinput"><B>cd</B></CODE><CODECLASS="replaceable"><I> somedir</I></CODE>% <CODECLASS="userinput"><B>ln ~/-i .</B></CODE>   ...</PRE></TD></TR></TABLE></P><PCLASS="para">Second, to save disk blocks, make sure the <EMCLASS="emphasis">-i</EM> file is zero-length&nbsp;- use the <EMCLASS="emphasis">touch</EM> command, not <EMCLASS="emphasis">vi</EM> or some other command that putscharacters in the file. <EMCLASS="emphasis">-JP</EM>&nbsp;]<ACLASS="indexterm"NAME="AUTOID-25420"></A></P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">BB</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_06.htm"TITLE="23.6 A Faster Way to Remove Files Interactively "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 23.6 A Faster Way to Remove Files Interactively "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_08.htm"TITLE="23.8 Safe Delete: Pros and Cons "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 23.8 Safe Delete: Pros and Cons "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">23.6 A Faster Way to Remove Files Interactively </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.8 Safe Delete: Pros and Cons </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 &amp; 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 + -