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

📄 ch23_20.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 23] 23.20 Deleting Stale Files </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:18Z"><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_19.htm"TITLE="23.19 Deleting (BSD) Manual Pages that Aren't Read "><LINKREL="next"HREF="ch23_21.htm"TITLE="23.21 Removing Every File but One "></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_19.htm"TITLE="23.19 Deleting (BSD) Manual Pages that Aren't Read "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 23.19 Deleting (BSD) Manual Pages that Aren't Read "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_21.htm"TITLE="23.21 Removing Every File but One "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 23.21 Removing Every File but One "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-1770">23.20 Deleting Stale Files </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-25988"></A><ACLASS="indexterm"NAME="AUTOID-25992"></A>Sooner or later, a lot of junk collects in your directories: filesthat you don't really care about and never use. It's possible towrite <SPANCLASS="link"><EMCLASS="emphasis">find</EM> (<ACLASS="linkend"HREF="ch17_01.htm#UPT-ART-7410"TITLE="The find Command Is Great; The Problem Is Finding How to Use It">17.1</A>)</SPAN>commands that will automatically clean these up. Ifyou want to clean up regularly, you can add some <EMCLASS="emphasis">find</EM> commandsto your <SPANCLASS="link"><EMCLASS="emphasis">crontab</EM> file (<ACLASS="linkend"HREF="ch40_12.htm"TITLE="Periodic Program Execution: The cron Facility ">40.12</A>)</SPAN>.</P><PCLASS="para">Basically, all you need to do is write a <EMCLASS="emphasis">find</EM> command thatlocates files based on their last access time(<SPANCLASS="link"><EMCLASS="emphasis">-atime</EM> (<ACLASS="linkend"HREF="ch17_05.htm"TITLE="Searching for Old Files ">17.5</A>)</SPAN>),and use<SPANCLASS="link"><EMCLASS="emphasis">-ok</EM> or <EMCLASS="emphasis">-exec</EM> (<ACLASS="linkend"HREF="ch17_10.htm"TITLE="Running Commands on What You Find ">17.10</A>)</SPAN>to delete them. Such a command might looklike this:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>find . -atime +60 -ok rm -f {} \;</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">This locates files that haven't been accessed in the last 60 days,asks if you want to delete the file, and then deletes the file.(If you run it from <EMCLASS="emphasis">cron</EM>, make sure you use <EMCLASS="emphasis">-exec</EM> insteadof <EMCLASS="emphasis">-ok</EM>; and make <EMCLASS="emphasis">absolutely sure</EM> that the <EMCLASS="emphasis">find</EM> won'tdelete files that you think are important.)</P><PCLASS="para">Of course, you can modify this <EMCLASS="emphasis">find</EM> command to exclude (orselect) files with particular names; for example, the command belowdeletes old core dumps and GNU Emacs backup files (whose names end in<CODECLASS="literal">~</CODE>), but leaves all others alone:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>find . \( -name core -o -name &quot;*~&quot; \) -atime +60 -ok rm -f {} \;</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">If you take an automated approach to deleting stale files, here aresome things to watch out for:</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para">There are plenty of files (for example, UNIX utilities and log files)that should <EMCLASS="emphasis">never</EM> be removed. Never run any &quot;automaticdeletion&quot; script on <EMCLASS="emphasis">/usr</EM> or <CODECLASS="literal">/</CODE> or any other &quot;system&quot;directory.&#13;</P></LI><LICLASS="listitem"><PCLASS="para">On some systems, executing a binary executable doesn't update thelast access time. Since there's no reason to read these files, you can expect them to get pretty stale, even if they're used often.You don't want to delete them. If you cook up a complicated enough<EMCLASS="emphasis">find</EM> command, you should be able to handle this automatically.Something like this should (at least partially) do the trick:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"><ACLASS="co"HREF="ch17_06.htm"TITLE="17.6 Be an Expert on find Search Operators ">!</A> <ACLASS="co"HREF="ch17_15.htm"TITLE="17.15 Searching for Files by Permission ">-perm</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">% <CODECLASS="userinput"><B>find . -atime +30 ! -perm -111 ... -exec rm {} \;</B></CODE></PRE></TD></TR></TABLE></P><PCLASS="para"></P></LI><LICLASS="listitem"><PCLASS="para">Along the same lines, you'd probably never want to delete C sourcecode, so you might modify your <EMCLASS="emphasis">find</EM> command to look like this:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>find . -atime +30 ! -perm -111 ! -name &quot;*.c&quot; ... -exec rm {} \;</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para"></P></LI><LICLASS="listitem"><PCLASS="para">I personally find that automatically deleting files is an extremeand bizarre solution. I can't imagine deleting files without knowingexactly what I've deleted or without (somehow) saving the &quot;trash&quot;somewhere just in case I accidentally removed something important.You can use the <EMCLASS="emphasis">find</EM><EMCLASS="emphasis">-cpio</EM>operator (if your system has it) to<SPANCLASS="link">archive (<ACLASS="linkend"HREF="ch19_09.htm"TITLE="A System V Tape Archiver: cpio ">19.9</A>)</SPAN>the deleted files on tape.</P></LI></UL><PCLASS="para">OK, I've said that I don't really think that automated deletion scriptsare a good idea. What's my solution, then?</P><PCLASS="para">I don't have a good comprehensive solution. I spend a reasonableamount of time (maybe an hour a month) going through directories anddeleting stale files by hand. I also have a <EMCLASS="emphasis">clean</EM> alias that Itype whenever I think about it. It looks like this:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">alias clean &quot;rm *~ junk *.BAK core #*&quot;</PRE></BLOCKQUOTE></P><PCLASS="para">&#13;That is, this alias deletes all of my<SPANCLASS="link">Emacs (<ACLASS="linkend"HREF="ch32_01.htm#UPT-ART-5540"TITLE="Emacs: The Other Editor ">32.1</A>)</SPAN>backup files, Emacsautosave files (I admit, that's risky), files named <EMCLASS="emphasis">junk</EM>, someother backup files, and<SPANCLASS="link">core dumps (<ACLASS="linkend"HREF="glossary.htm#UPT-ART-1010"TITLE="Glossary">52.9</A>)</SPAN>.I'll admit that since I <EMCLASS="emphasis">never</EM>want to save these files, I could probably live with something like:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>find ~ \( -name &quot;*~&quot; -o -name core \) -atime +1 -exec rm {} \;</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">But still: automated deletion commands make me really nervous, andI'd prefer to live without them.</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_19.htm"TITLE="23.19 Deleting (BSD) Manual Pages that Aren't Read "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 23.19 Deleting (BSD) Manual Pages that Aren't Read "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_21.htm"TITLE="23.21 Removing Every File but One "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 23.21 Removing Every File but One "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">23.19 Deleting (BSD) Manual Pages that Aren't Read </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.21 Removing Every File but One </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 + -