📄 ch16_02.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 16] 16.2 Finding Oldest or Newest Files with ls -t and ls -u </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:37:35Z"><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_01.htm"TITLE="16.1 Everything but the find Command "><LINKREL="next"HREF="ch16_03.htm"TITLE="16.3 Reordering ls Listings "></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_01.htm"TITLE="16.1 Everything but the find Command "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 16.1 Everything but the find Command "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_03.htm"TITLE="16.3 Reordering ls Listings "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 16.3 Reordering ls Listings "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-1570">16.2 Finding Oldest or Newest Files with ls -t and ls -u </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-16682"></A><ACLASS="indexterm"NAME="AUTOID-16685"></A><ACLASS="indexterm"NAME="AUTOID-16688"></A>Your directory might have 50, 100, or more files.Which files haven't been used for a while?You might be able to save space by removing them.You read or edited a file yesterday but you can't remember its name?These commands will help you find it.(If you want a quick review of UNIX file times, see article<ACLASS="xref"HREF="ch16_05.htm"TITLE="The Three UNIX File Times ">16.5</A>.)</P><PCLASS="para">In this example, I'll show you my<SPANCLASS="link"><EMCLASS="emphasis">bin</EM> (<ACLASS="linkend"HREF="ch04_02.htm"TITLE="A bin Directory for Your Programs and Scripts ">4.2</A>)</SPAN>directory full of shell scripts and other programs-I want to see whichprograms I don't use very often.You can use the same technique for directories with text or other files.</P><PCLASS="para">The <EMCLASS="emphasis">ls</EM> command has options to change the way it orders files.By default, <EMCLASS="emphasis">ls</EM> lists files alphabetically - that probably won'thelp you find old files, but it's a good place to start this explanation.<ACLASS="indexterm"NAME="AUTOID-16697"></A>For finding old files, use the <EMCLASS="emphasis">-t</EM> option. This sorts files bytheir <EMCLASS="emphasis">modification time</EM>, orthe last time the file was changed. Thenewest files are listed first. Here's what happens:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">jerry@ora ~/.bin 60 % <CODECLASS="userinput"><B>ls -t</B></CODE>weather unshar scandrafts rn2mh recompcrontab zloop tofrom rmmer mhprofilerhyes showpr incc mhadd appendrhno rfl drmm fixsubj READMEpickthis maillog reheader distprompter rtfmcgrep c-w zrefile xmhprint saveartdirtop cw zscan replf echoerrwhich cx zfolders folstcx showmult alifile incs</PRE></BLOCKQUOTE></P><PCLASS="para">I just added a shell script named <EMCLASS="emphasis">weather</EM> yesterday; you can seeit as the first file in the first column.I also made a change to my script named <EMCLASS="emphasis">crontab</EM> last week;it's shown next.The oldest program in here is <EMCLASS="emphasis">echoerr</EM>; it's listed last.[1]</P><BLOCKQUOTECLASS="footnote"><PCLASS="para">[1] On some systems, <EMCLASS="emphasis">ls -t</EM> will list the files inone column, with the newest file first. Although that's usually apain, I actually find that more convenient when I'm interested in themost recent files. If your system does that and you don't like thesingle-column display, you can use <EMCLASS="emphasis">ls -Ct</EM>.On other systems, if a single column display would be handy, use <EMCLASS="emphasis">ls -1t</EM>;the "1" option means "one column."Throughout this article, we'll assume you're using a multi-column display.</P></BLOCKQUOTE><PCLASS="para"><EMCLASS="emphasis">ls -t</EM> is also great for file time comparisons<SPANCLASS="link">in a script (<ACLASS="linkend"HREF="ch02_15.htm"TITLE="Unclutter Logins: Show Login Messages Just Once ">2.15</A>, <ACLASS="linkend"HREF="ch16_27.htm"TITLE="newer: Print the Name of the Newest File ">16.27</A>)</SPAN>.[Personally, I find <EMCLASS="emphasis">ls -t</EM> most useful when I've forgottenwhether or not I've edited a file recently. If I've changed a file,it will be at or near the top of the <EMCLASS="emphasis">ls -t</EM> listing. Forexample, I might ask, "Have I made the changes to that letter I wasgoing to send?" If I haven't made the changes (but only think I have),my letter will most likely appear somewhere in the middle of the listing. <EMCLASS="emphasis">-ML</EM> ]</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-16721"></A>The <EMCLASS="emphasis">-u</EM> option shows the files' last-access time instead of thelast-modification time.The <EMCLASS="emphasis">-u</EM> option doesn't do anything with plain <EMCLASS="emphasis">ls</EM>-you have to useit with another option like <EMCLASS="emphasis">-t</EM> or <EMCLASS="emphasis">-l</EM>.The next listing shows that I've recently used the <EMCLASS="emphasis">rtfm</EM> and<EMCLASS="emphasis">rmmer</EM> files.I haven't read <EMCLASS="emphasis">README</EM> in a long time, though - oops:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">jerry@ora ~/.bin62 % <CODECLASS="userinput"><B>ls -tu</B></CODE>rtfm cx drmm saveart fixsubjrmmer c-w zscan scandrafts echoerrrfl cw zrefile rhno dirtopmhprofile distprompter xmhprint rhyes cgrepshowmult recomp zloop replf appendtcx crontab zfolders reheader alifiletofrom mhadd which incs READMErn2mh pickthis unshar maillogweather incc showpr fols</PRE></BLOCKQUOTE></P><PCLASS="para">(Some UNIXes<SPANCLASS="link">don't update the last-access time of executable files (<ACLASS="linkend"HREF="ch21_05.htm"TITLE="What Good Is a File's Last Access Time? ">21.5</A>)</SPAN>when you run them.Shell scripts are always read, so their last-access times will always beupdated.)</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-16738"></A><ACLASS="indexterm"NAME="AUTOID-16741"></A><ACLASS="indexterm"NAME="AUTOID-16744"></A>The <EMCLASS="emphasis">-c</EM> option shows when the file's<SPANCLASS="link">inode information (<ACLASS="linkend"HREF="ch01_22.htm"TITLE="How UNIX Keeps Track of Files: Inodes ">1.22</A>, <ACLASS="linkend"HREF="ch21_06.htm"TITLE="A File's Inode Change (not "Creation"!) Time">21.6</A>)</SPAN>was last changed.The inode time tells when the file was created, when you used <EMCLASS="emphasis">chmod</EM>to change thepermissions, and so on.That doesn't help you find "stale" files:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">jerry@ora ~/.bin 64 % <CODECLASS="userinput"><B>ls -tc</B></CODE>weather maillog reheader recomp incs crontab tcx rn2mh fols cx cgrep zscan tofrom rmmer cw zloop zrefile mhadd fixsubj c-w dirtop rfl drmm mhprofile echoerr pickthis showmult alifile append which rhno rtfm showpr saveart README unshar incc scandrafts distprompter rhyes zfolders xmhprint replf</PRE></BLOCKQUOTE></P><PCLASS="para">If you're wondering just how long ago a file was modified (or accessed),add the <EMCLASS="emphasis">-l</EM> option for a long listing.As before, adding <EMCLASS="emphasis">-u</EM> shows the last-access time; <EMCLASS="emphasis">-c</EM> showsinode change time.If I look at the access times of a few specific files, I find that Ihaven't read <EMCLASS="emphasis">README</EM> since 1989...</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">jerry@ora ~/.bin 65 % <CODECLASS="userinput"><B>ls -ltu README alifile maillog</B></CODE>-rwxr-xr-x 1 jerry ora 59 Feb 2 1991 maillog-rwxrwxr-x 1 jerry ora 213 Nov 29 1989 alifile-rw-r--r-- 1 jerry ora 3654 Nov 27 1989 README</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_01.htm"TITLE="16.1 Everything but the find Command "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 16.1 Everything but the find Command "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_03.htm"TITLE="16.3 Reordering ls Listings "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 16.3 Reordering ls Listings "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">16.1 Everything but the find Command </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.3 Reordering ls Listings </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 + -