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

📄 ch16_21.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 16] 16.21 Finding All Directories with the Same Name </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:37: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="ch16_01.htm"TITLE="16. Where Did I Put That?"><LINKREL="prev"HREF="ch16_20.htm"TITLE="16.20 The vtree Visual Directory Tree Programs "><LINKREL="next"HREF="ch16_22.htm"TITLE="16.22 Comparing Two Directory Trees with dircmp "></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="ch16_20.htm"TITLE="16.20 The vtree Visual Directory Tree Programs "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 16.20 The vtree Visual Directory Tree Programs "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 16<BR>Where Did I Put That?</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch16_22.htm"TITLE="16.22 Comparing Two Directory Trees with dircmp "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 16.22 Comparing Two Directory Trees with dircmp "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-0413">16.21 Finding All Directories with the Same Name </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-17622"></A><ACLASS="indexterm"NAME="AUTOID-17625"></A><ACLASS="indexterm"NAME="AUTOID-17628"></A>Time for a confession. I collect a lot of software. I have one disk filled withpublic-domain software. Some directories are &quot;collections&quot;like the Sun User Group tapes.It is likely that I might have the same program in twodifferent directories. To prevent this waste of space, I create anindex of directories and the path needed to reach them.If I have two directories with the same name, I would like to knowabout it. I might be able to delete one of the directories.A simple way to search for redundant directories is with the following command:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">find . -type d -print | \awk -F/ '{printf(&quot;%s\t%s\n&quot;,$NF,$0);}' | \sort</PRE></BLOCKQUOTE></P><PCLASS="para">[You might want to make this into an<SPANCLASS="link"> alias or function . (<ACLASS="linkend"HREF="ch10_01.htm#UPT-ART-4930"TITLE="Creating Custom Commands ">10.1</A>)</SPAN><EMCLASS="emphasis">--JP</EM> ] The<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>command prints out all directories.The<SPANCLASS="link"><EMCLASS="emphasis">awk</EM> (<ACLASS="linkend"HREF="ch33_11.htm"TITLE="Quick Reference: awk ">33.11</A>)</SPAN>command uses theslash (<CODECLASS="literal">/</CODE>)as the field separator.<CODECLASS="literal">NF</CODE>is the number of fields, and <CODECLASS="literal">$NF</CODE> the last field.<CODECLASS="literal">$0</CODE>is the <EMCLASS="emphasis">awk</EM>variable for the entire line.The output would tell you where all of the directories named<EMCLASS="emphasis">misc</EM>are located:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">misc    ./X11/lib/X11/fonts/miscmisc    ./miscmisc    ./src/XView2/contrib/examples/miscmisc    ./src/XView2/fonts/bdf/miscmisc    ./src/XView2/lib/libxvin/miscmisc    ./src/XView2/lib/libxvol/miscmisc    ./src/XView2/misc</PRE></BLOCKQUOTE></P><PCLASS="para">&#13;This could be converted into a shell script that takes arguments.If no arguments are specified, I want it to default to theargument <CODECLASS="literal">.</CODE> (dot):<ACLASS="indexterm"NAME="AUTOID-17650"></A></P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist">&#13;<ACLASS="co"HREF="ch45_12.htm"TITLE="45.12 Parameter Substitution ">${*-.}</A> &#13;</PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">&#13;#!/bin/sh# usage: dir_path [directory ...]# list directory and path to directoryfind ${*-.} -type d -print | awk -F/  '{       printf (&quot;%s\t%s\n&quot;,$NF,$0);}' | sort</PRE></TD></TR></TABLE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-17657"></A>[You could also use this great idea for finding duplicate files.Change the <CODECLASS="literal">-type</CODE> <CODECLASS="literal">d</CODE> to <CODECLASS="literal">-type</CODE> <CODECLASS="literal">f</CODE>.If you (or all the users on your system) want to use this a lot,run <EMCLASS="emphasis">dir_path</EM> nightly with<SPANCLASS="link"><EMCLASS="emphasis">cron</EM> (<ACLASS="linkend"HREF="ch40_12.htm"TITLE="Periodic Program Execution: The cron Facility ">40.12</A>)</SPAN>or<SPANCLASS="link"><EMCLASS="emphasis">at</EM> (<ACLASS="linkend"HREF="ch40_03.htm"TITLE="The at Command ">40.3</A>)</SPAN>.Save the output to a &quot;database&quot; file.Use the speedy<SPANCLASS="link"><EMCLASS="emphasis">look</EM> command (<ACLASS="linkend"HREF="ch27_18.htm"TITLE='Fast Searches and Spelling Checks with  "look&quot;'>27.18</A>)</SPAN>to search the database file.Article<ACLASS="xref"HREF="ch17_19.htm"TITLE="Finding Files (Much) Faster with a find Database ">17.19</A>shows another <EMCLASS="emphasis">find</EM> database. <EMCLASS="emphasis">-JIK, JP</EM> ]</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="ch16_20.htm"TITLE="16.20 The vtree Visual Directory Tree Programs "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 16.20 The vtree Visual Directory Tree Programs "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="ch16_22.htm"TITLE="16.22 Comparing Two Directory Trees with dircmp "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 16.22 Comparing Two Directory Trees with dircmp "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">16.20 The vtree Visual Directory Tree Programs </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">16.22 Comparing Two Directory Trees with dircmp </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 + -