📄 ch16_29.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 16] 16.29 sls: Super ls with Format You Can Choose </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly & Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:38:08Z"><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_28.htm"TITLE="16.28 oldlinks: Find Unconnected Symbolic Links "><LINKREL="next"HREF="ch17_01.htm"TITLE="17. Finding Files with find"></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_28.htm"TITLE="16.28 oldlinks: Find Unconnected Symbolic Links "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 16.28 oldlinks: Find Unconnected Symbolic Links "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="CHAPTER"HREF="ch17_01.htm"TITLE="17. Finding Files with find"><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 17. Finding Files with find"BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-7560">16.29 sls: Super ls with Format You Can Choose </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-18024"></A><ACLASS="indexterm"NAME="AUTOID-18026"></A>The <EMCLASS="emphasis">ls -l</EM> command, and related commands like<SPANCLASS="link"><EMCLASS="emphasis">stat</EM> (<ACLASS="linkend"HREF="ch21_13.htm"TITLE="Read an Inode with stat ">21.13</A>)</SPAN>,give lots of information about a file (more exactly, about a file's<SPANCLASS="link">inode (<ACLASS="linkend"HREF="ch01_22.htm"TITLE="How UNIX Keeps Track of Files: Inodes ">1.22</A>)</SPAN>).The information is printed in a way that's (sort of) nice to look at.But the format might not be exactly what you want.That format can be tough for shell programmers to use:parsing the output with <EMCLASS="emphasis">sed</EM>, <EMCLASS="emphasis">awk</EM>, and others is trickyand a pain (article<ACLASS="xref"HREF="ch16_25.htm"TITLE="Listing Files by Age and Size ">16.25</A>has an example).Finally, the <EMCLASS="emphasis">ls -l</EM> output is different on BSD and System V systems.</P><TABLECLASS="para.programreference"BORDER="1"><TR><THVALIGN="TOP"><ACLASS="programreference"HREF="examples/index.htm"TITLE="sls">sls</A><BR></TH><TDVALIGN="TOP"> The <EMCLASS="emphasis">sls</EM> command solves those problems and more.It lets you:</TD></TR></TABLE><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para">Make your own output format: pick the information you want to seeand the order it's shown.</P></LI><LICLASS="listitem"><PCLASS="para">Sort the output on one or more fields.</P></LI><LICLASS="listitem"><PCLASS="para">Make a consistent date format: numeric or in words, include the secondsif you want to, and more.Best of all, the date format won't change for files more than six months old(unless you use the <EMCLASS="emphasis">-u</EM> option).</P></LI></UL><PCLASS="para">And there's much more.</P><PCLASS="para">The manual page on the disc explains <EMCLASS="emphasis">sls</EM> formatting in detail.Here are a few examples.Let's start with the style of <EMCLASS="emphasis">ls -l</EM> output that hasfixed-width columns and doesn't show group ownership.(The default <EMCLASS="emphasis">sls -l</EM> is similar, but itsdate format doesn't change after six months and it doesn't have the<EMCLASS="emphasis">total</EM> line.)</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>ls -l</B></CODE>total 3-rw-r----- 1 jerry 1641 Feb 29 1992 afilelrwxrwxrwx 1 jerry 8 Nov 18 00:38 bfile -> ../bfile</PRE></BLOCKQUOTE></P><PCLASS="para">Here's a more user-friendly format for people who aren't UNIX hackers(it might be best to put this into an<SPANCLASS="link">alias or shell function (<ACLASS="linkend"HREF="ch10_01.htm#UPT-ART-4930"TITLE="Creating Custom Commands ">10.1</A>)</SPAN>).The date and time are shown, followed by the owner's name, the size inkbytes, and the filename without the symbolic link information like<CODECLASS="literal">-> ../bfile</CODE>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>sls -p '%m"%F %d, 19%y %r" %u %4skK %n'</B></CODE>February 29, 1992 03:43:00 PM jerry 2K afileNovember 18, 1992 00:38:22 AM jerry 1K bfile</PRE></BLOCKQUOTE></P><PCLASS="para">How about a simple <EMCLASS="emphasis">ls</EM> output that shows all three<SPANCLASS="link">file dates (<ACLASS="linkend"HREF="ch16_05.htm"TITLE="The Three UNIX File Times ">16.5</A>)</SPAN>:modification, access, and inode change?We'll use<SPANCLASS="link"><EMCLASS="emphasis">echo</EM> (<ACLASS="linkend"HREF="ch08_06.htm"TITLE="Output Command-Line Arguments ">8.6</A>)</SPAN>to print a title first:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>echo 'modify access inode'; \sls -p '%m"%D" %a"%D" %c"%D" %n'</B></CODE>modify access inode02/29/92 09/17/92 11/18/92 afile11/18/92 11/18/92 11/18/92 bfile</PRE></BLOCKQUOTE></P><PCLASS="para">Finally, let's ask <EMCLASS="emphasis">sls</EM> to make a set of UNIX commands that could beused at the end of a<SPANCLASS="link">shell archive (<ACLASS="linkend"HREF="ch19_02.htm"TITLE="Introduction to Shell Archives ">19.2</A>)</SPAN>file.These commands would recreate the modes, date and owner (with a numeric UID)as the files are extracted from the archive:</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> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">% <CODECLASS="userinput"><B>sls -p 'chmod %P %n; chown %U %n; touch %m"%m%d%H%M%y" %n'</B></CODE>chmod 640 afile; chown 225 afile; touch 0229154392 afilechmod 777 bfile; chown 225 bfile; touch 1118003892 bfile</PRE></TD></TR></TABLE></P><PCLASS="para">I didn't show the sorting options or many of the other output formatcharacters.But I hope I've given you an idea (or ten).<ACLASS="indexterm"NAME="AUTOID-18079"></A></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_28.htm"TITLE="16.28 oldlinks: Find Unconnected Symbolic Links "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 16.28 oldlinks: Find Unconnected Symbolic Links "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="CHAPTER"HREF="ch17_01.htm"TITLE="17. Finding Files with find"><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 17. Finding Files with find"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">16.28 oldlinks: Find Unconnected Symbolic Links </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">17. Finding Files with find</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 & 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 + -