start page | rating of books | rating of authors | reviews | copyrights

UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 16.1 Everything but the find Command Chapter 16
Where Did I Put That?
Next: 16.3 Reordering ls Listings
 

16.2 Finding Oldest or Newest Files with ls -t and ls -u

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 16.5.)

In this example, I'll show you my bin (4.2) directory full of shell scripts and other programs-I want to see which programs I don't use very often. You can use the same technique for directories with text or other files.

The ls command has options to change the way it orders files. By default, ls lists files alphabetically - that probably won't help you find old files, but it's a good place to start this explanation. For finding old files, use the -t option. This sorts files by their modification time, or the last time the file was changed. The newest files are listed first. Here's what happens:

jerry@ora ~/.bin 
60 % ls -t
weather       unshar        scandrafts    rn2mh         recomp
crontab       zloop         tofrom        rmmer         mhprofile
rhyes         showpr        incc          mhadd         append
rhno          rfl           drmm          fixsubj       README
pickthis      maillog       reheader      distprompter  rtfm
cgrep         c-w           zrefile       xmhprint      saveart
dirtop        cw            zscan         replf         echoerr
which         cx            zfolders      fols
tcx           showmult      alifile       incs

I just added a shell script named weather yesterday; you can see it as the first file in the first column. I also made a change to my script named crontab last week; it's shown next. The oldest program in here is echoerr; it's listed last. [1]

[1] On some systems, ls -t will list the files in one column, with the newest file first. Although that's usually a pain, I actually find that more convenient when I'm interested in the most recent files. If your system does that and you don't like the single-column display, you can use ls -Ct. On other systems, if a single column display would be handy, use ls -1t; the "1" option means "one column." Throughout this article, we'll assume you're using a multi-column display.

ls -t is also great for file time comparisons in a script (2.15, 16.27). [Personally, I find ls -t most useful when I've forgotten whether or not I've edited a file recently. If I've changed a file, it will be at or near the top of the ls -t listing. For example, I might ask, "Have I made the changes to that letter I was going 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. -ML ]

The -u option shows the files' last-access time instead of the last-modification time. The -u option doesn't do anything with plain ls-you have to use it with another option like -t or -l. The next listing shows that I've recently used the rtfm and rmmer files. I haven't read README in a long time, though - oops:

jerry@ora ~/.bin
62 % ls -tu
rtfm          cx            drmm          saveart       fixsubj
rmmer         c-w           zscan         scandrafts    echoerr
rfl           cw            zrefile       rhno          dirtop
mhprofile     distprompter  xmhprint      rhyes         cgrep
showmult      recomp        zloop         replf         append
tcx           crontab       zfolders      reheader      alifile
tofrom        mhadd         which         incs          README
rn2mh         pickthis      unshar        maillog
weather       incc          showpr        fols

(Some UNIXes don't update the last-access time of executable files (21.5) when you run them. Shell scripts are always read, so their last-access times will always be updated.)

The -c option shows when the file's inode information (1.22, 21.6) was last changed. The inode time tells when the file was created, when you used chmod to change the permissions, and so on. That doesn't help you find "stale" files:

jerry@ora ~/.bin 
64 % ls -tc
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

If you're wondering just how long ago a file was modified (or accessed), add the -l option for a long listing. As before, adding -u shows the last-access time; -c shows inode change time. If I look at the access times of a few specific files, I find that I haven't read README since 1989...

jerry@ora ~/.bin 
65 % ls -ltu README alifile maillog
-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

- JP


Previous: 16.1 Everything but the find Command UNIX Power ToolsNext: 16.3 Reordering ls Listings
16.1 Everything but the find Command Book Index16.3 Reordering ls Listings

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System