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

📄 ch24_16.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 24] 24.16 Trimming a Huge Directory </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:45Z"><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="ch24_01.htm"TITLE="24. Other Ways to Get Disk Space"><LINKREL="prev"HREF="ch24_15.htm"TITLE="24.15 Trimming a Directory "><LINKREL="next"HREF="ch24_17.htm"TITLE="24.17 Disk Quotas "></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="ch24_15.htm"TITLE="24.15 Trimming a Directory "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 24.15 Trimming a Directory "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 24<BR>Other Ways to Get Disk Space</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch24_17.htm"TITLE="24.17 Disk Quotas "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 24.17 Disk Quotas "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-6020">24.16 Trimming a Huge Directory </A></H2><PCLASS="para">Some implementations of the BSD fast filesystem never truncatedirectories. That is, when you delete a file, the filesystem marks itsdirectory entry as &quot;invalid,&quot; but doesn't actually delete the entry.The old entry can be re-used when someone creates a new file, but will never go away. Therefore, the directories themselves can only get larger with time. Directoriesusually don't occupy a huge amount of space, but searching through alarge directory is noticeably slow. So you should avoid lettingdirectories get too large.</P><PCLASS="para">On many UNIX systems, the only way to &quot;shrink a directory&quot;is to move all of its files somewhere else and then remove it;for example:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist">&#13;<ACLASS="co"HREF="ch15_05.htm"TITLE='15.5 Matching All "Dot Files&quot; with Wildcards'>.[^A--/-^?]</A> &#13;</PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">ls -lgd old   <EMCLASS="emphasis">Get old owner, group, and mode</EM>mkdir new; chown <CODECLASS="replaceable"><I>user</I></CODE> new; chgrp <CODECLASS="replaceable"><I>group</I></CODE> new; chmod <CODECLASS="replaceable"><I>mode</I></CODE> newmv old/.??* old/.[^A--/-^?] old/* new   ^A <EMCLASS="emphasis">and</EM> <CODECLASS="literal">^?</CODE> <EMCLASS="emphasis">are CTRL-a and DEL</EM>rmdir oldmv new old</PRE></TD></TR></TABLE></P><PCLASS="para">This method also works on V7-ish filesystems.It cannot be appliedto the root of a filesystem.</P><PCLASS="para">Other implementations of the BSD fast filesystem do truncatedirectories. They do this after a complete scan of thedirectory has shown that some number of trailing fragments areempty. Complete scans are forced for any operation that placesa new name into the directory&nbsp;- such as <EMCLASS="emphasis">creat</EM>(2) or <EMCLASS="emphasis">link</EM>(2).In addition, new names are always placed in the earliest possiblefree slot. Hence, on these systems there is another way toshrink a directory.[How do you know if your BSD filesystem truncates directories?Try the pseudo-code below (but use actual commands), and see if it has an effect. -<EMCLASS="emphasis">ML</EM>&nbsp;]</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">while (the directory can be shrunk) {    mv (file in last slot) (some short name)    mv (the short name) (original name)}</PRE></BLOCKQUOTE></P><PCLASS="para">This works on the root of a filesystem as well as subdirectories.</P><PCLASS="para">Neither method should be used if some external agent (for example, adaemon) is busy lookingat the directory. The first method will also fail if theexternal agent is quiet but will resume <EMCLASS="emphasis">and</EM> hold the existingdirectory open (for example, a daemon program, like <EMCLASS="emphasis">sendmail</EM>, thatrescans the directory,but which is currently stopped or idle).The second method requires knowing a &quot;safe&quot; short name&nbsp;- i.e., a namethat doesn't duplicate any other name in the directory.</P><PCLASS="para">I have found the second method useful enough to write a shell scriptto do the job. I call the script <EMCLASS="emphasis">squoze</EM>:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist">&#13;<ACLASS="co"HREF="ch35_21.htm"TITLE="35.21 Using IFS to Split Strings ">IFS</A> <ACLASS="co"HREF="ch45_09.htm"TITLE='45.9 The Unappreciated Bourne Shell  ":&quot; Operator'>:</A> <ACLASS="co"HREF="ch25_15.htm"TITLE="25.15 Finer Control on tail ">-r</A> <ACLASS="co"HREF="ch21_11.htm"TITLE="21.11 Safer Removing, Moving, and Copying ">-i</A> <ACLASS="co"HREF="ch44_09.htm"TITLE="44.9 Testing Your Success ">&amp;&amp;</A> &#13;</PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">&#13;#! /bin/sh## squozelast=ls -ldgIFS=''while :do   set `ls -f | tail -10r`   for i do      case &quot;$i&quot; in &quot;$last&quot;|.|..) break 2;; esac      # _ (underscore) is the &quot;safe, short&quot; filename      /bin/mv -i &quot;$i&quot; _ &amp;&amp; /bin/mv _ &quot;$i&quot;   done   last=&quot;$i&quot;donels -ldg</PRE></TD></TR></TABLE></P><PCLASS="para">[The <EMCLASS="emphasis">ls -f</EM> option lists entries in the order they appear in thedirectory; it doesn't sort. <EMCLASS="emphasis">-JP</EM>&nbsp;]This script does not handle filenames with embedded newlines. It is,however, safe to apply to a <EMCLASS="emphasis">sendmail</EM> queue while <EMCLASS="emphasis">sendmail</EM> is stopped.<ACLASS="indexterm"NAME="AUTOID-27031"></A><ACLASS="indexterm"NAME="AUTOID-27032"></A><ACLASS="indexterm"NAME="AUTOID-27033"></A></P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">CT</SPAN> <SPANCLASS="bibliomisc">in <ACLASS="systemitem.newsgroup"HREF="news:comp.unix.admin">comp.unix.admin</A> on Usenet, 22 August 1991</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="ch24_15.htm"TITLE="24.15 Trimming a Directory "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 24.15 Trimming a Directory "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="ch24_17.htm"TITLE="24.17 Disk Quotas "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 24.17 Disk Quotas "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">24.15 Trimming a Directory </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">24.17 Disk Quotas </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 + -