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

📄 ch16_06.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 16] 16.6 clf, cls: "Compressed&quot; ls Listings</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:39Z"><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_05.htm"TITLE="16.5 The Three UNIX File Times "><LINKREL="next"HREF="ch16_07.htm"TITLE="16.7 ls Shortcuts: ll, lf, lg, etc. "></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_05.htm"TITLE="16.5 The Three UNIX File Times "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 16.5 The Three UNIX File Times "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_07.htm"TITLE="16.7 ls Shortcuts: ll, lf, lg, etc. "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 16.7 ls Shortcuts: ll, lf, lg, etc. "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-4230">16.6 clf, cls: &quot;Compressed&quot; ls Listings</A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-16889"></A><ACLASS="indexterm"NAME="AUTOID-16892"></A>Most newer UNIX systems let you make filenames that are hundreds ofcharacters long. The bad thing about that is that when <EMCLASS="emphasis">ls</EM>lists the filenames in columns, it can't fit many columns across the screen.If your directory has a lot of files, the listing can scroll off thescreen.</P><PCLASS="para">I wrote a script that lists a directory in five columns.If a filename doesn't fit, the script truncates the name and printsa right angle bracket (<CODECLASS="literal">&gt;</CODE>) at the end of the name.Here's a demo.It starts with the standard <EMCLASS="emphasis">ls</EM> and its<ACLASS="indexterm"NAME="AUTOID-16898"></A><SPANCLASS="link"><EMCLASS="emphasis">-F</EM> option (<ACLASS="linkend"HREF="ch16_12.htm"TITLE="Useful ls Aliases ">16.12</A>)</SPAN>to mark directories with a trailing <CODECLASS="literal">/</CODE> and executable files witha <CODECLASS="literal">*</CODE>.<ACLASS="indexterm"NAME="AUTOID-16905"></A><ACLASS="indexterm"NAME="AUTOID-16907"></A><ACLASS="indexterm"NAME="AUTOID-16909"></A><ACLASS="indexterm"NAME="AUTOID-16911"></A>Next, <EMCLASS="emphasis">clf</EM> gives the same listing, compressed.Third, <EMCLASS="emphasis">cls</EM> gives a listing without the <CODECLASS="literal">/</CODE> and <CODECLASS="literal">*</CODE>.</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>ls -F</B></CODE>HOMEDIR_backup/                         more*adir/                                   projects.1995/afile                                   projects.1996/cfile                                   updatedb.clientdfile                                   zfilefile_with_a_very_long_name_what_a_mess* zoo.tar.Zjerryp_MH.tar.Z% <CODECLASS="userinput"><B>clf</B></CODE>HOMEDIR_back&gt;/ cfile          jerryp_MH.tar&gt; projects.1996/ zoo.tar.Zadir/          dfile          more*          updatedb.clie&gt;afile          file_with_a_&gt;* projects.1995/ zfile% <CODECLASS="userinput"><B>cls</B></CODE>HOMEDIR_backup cfile          jerryp_MH.tar&gt; projects.1996  zoo.tar.Zadir           dfile          more           updatedb.clie&gt;afile          file_with_a_v&gt; projects.1995  zfile</PRE></BLOCKQUOTE></P><TABLECLASS="para.programreference"BORDER="1"><TR><THVALIGN="TOP"><ACLASS="programreference"HREF="examples/index.htm"TITLE="cls">cls</A><BR></TH><TDVALIGN="TOP">The script has a total of four names (links).<EMCLASS="emphasis">cls</EM> lists in columns that are sorted top to bottom.<EMCLASS="emphasis">clf</EM> is like <EMCLASS="emphasis">cls</EM>, but marks directories and executable files.<EMCLASS="emphasis">cls2</EM> and <EMCLASS="emphasis">clf2</EM> are like <EMCLASS="emphasis">cls</EM> and <EMCLASS="emphasis">clf</EM>, but theysort filenames side to side instead; this is faster but may not beaseasy to read.The script tests its name and does the right commands in a<SPANCLASS="link"><EMCLASS="emphasis">case</EM> statement (<ACLASS="linkend"HREF="ch44_06.htm"TITLE="Pattern Matching in case Statements ">44.6</A>)</SPAN>that starts like this:</TD></TR></TABLE><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">case &quot;$0&quot; in*clf2)  $ls -F ${1+&quot;$@&quot;} | sed -e &quot;$sed&quot; | $pr -l1; exit ;;   ...</PRE></BLOCKQUOTE></P><PCLASS="para">The <CODECLASS="literal">${1+&quot;$@&quot;}</CODE> passes in quoted filenames from the command linewithout breaking them into pieces at the spaces.This is a workaround for<SPANCLASS="link"> differences in the way some old Bourne shellshandle an empty <CODECLASS="literal">&quot;$@&quot;</CODE> parameter . (<ACLASS="linkend"HREF="ch46_07.htm"TITLE="Quoting and Command-Line Parameters ">46.7</A>)</SPAN></P><PCLASS="para">The &quot;guts&quot; of the shell script is the two-line<SPANCLASS="link"><EMCLASS="emphasis">sed</EM> (<ACLASS="linkend"HREF="ch34_24.htm"TITLE="Quick Reference: sed ">34.24</A>)</SPAN>command below (the single quotes around the expression pass both lines intothe shell variable at once):</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">sed='/[/@*=]$/s/^\(............\)...*\([/@*=][/@*=]*\)$/\1&gt;\2/s/^\(.............\)...*/\1&gt;/'</PRE></BLOCKQUOTE></P><PCLASS="para">The <EMCLASS="emphasis">ls</EM> output is piped to <EMCLASS="emphasis">sed</EM>'s standard input:</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para">The first <EMCLASS="emphasis">sed</EM> script line matches lines that are more than 14characters long and end with one of the symbols<CODECLASS="literal">*</CODE>, <CODECLASS="literal">/</CODE>, <CODECLASS="literal">@</CODE>, or <CODECLASS="literal">=</CODE>.The&quot;escaped parenthesis&quot; operators<SPANCLASS="link"><CODECLASS="literal">/(.../)</CODE> (<ACLASS="linkend"HREF="ch34_10.htm"TITLE="Referencing Portions of a Search String ">34.10</A>)</SPAN>grab the first 12 characters into <CODECLASS="literal">\1</CODE> and the symbol into<CODECLASS="literal">\2</CODE>-then print them both together with a <CODECLASS="literal">&gt;</CODE> between.</P></LI><LICLASS="listitem"><PCLASS="para">The second <EMCLASS="emphasis">sed</EM> line matches other filenames over 14 charactersthat don't end with a symbol.(It won't match filenames that the first linematched because the first line shortened them.)The second line grabs the first 13 characters, prints them with a<CODECLASS="literal">&gt;</CODE> on the end.</P></LI></UL><PCLASS="para">If you figured that out yourself, tell your managerthat I think you deserve a promotion <CODECLASS="literal">:-)</CODE>.</P><PCLASS="para">The other tricky part is this line:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"><ACLASS="co"HREF="ch45_31.htm"TITLE="45.31 Nested Command Substitution ">`...`</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">$pr -l`expr \( `wc -l &lt; $temp` / 5 \) + 1` $temp</PRE></TD></TR></TABLE></P><PCLASS="para">&#13;It's used when you call the script <EMCLASS="emphasis">clf</EM> or <EMCLASS="emphasis">cls</EM> and thefilenames need to be printed down columns instead of across.The same kind of line is used in article<ACLASS="xref"HREF="ch35_16.htm"TITLE="Make Columns Automatically with cols ">35.16</A>-it's explained there.The time that line takes to run is why <EMCLASS="emphasis">clf</EM> and <EMCLASS="emphasis">cls</EM>are a little slower than <EMCLASS="emphasis">clf2</EM> and <EMCLASS="emphasis">cls2</EM>.</P><PCLASS="para">You can install this script from the CD-ROMor from the<SPANCLASS="link">online archive (<ACLASS="linkend"HREF="ch52_07.htm"TITLE="Other Ways to Get the Software ">52.7</A>)</SPAN>.If you get it from the archive,ask tar to install <EMCLASS="emphasis">cls</EM> and its three other links:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>tar xvf </B></CODE><CODECLASS="replaceable"><I>archive.tar</I></CODE><CODECLASS="userinput"><B> cls clf cls2 clf2</B></CODE>x cls, 1282 bytes, 3 tape blocksclf linked to colscls2 linked to colsclf2 linked to cols</PRE></BLOCKQUOTE></P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">JP</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_05.htm"TITLE="16.5 The Three UNIX File Times "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 16.5 The Three UNIX File Times "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_07.htm"TITLE="16.7 ls Shortcuts: ll, lf, lg, etc. "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 16.7 ls Shortcuts: ll, lf, lg, etc. "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">16.5 The Three UNIX File Times </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.7 ls Shortcuts: ll, lf, lg, etc. </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 + -