home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 16.2 Finding Oldest or Newest Files with ls -t and ls -u Chapter 16
Where Did I Put That?
Next: 16.4 List All Subdirectories with ls -R
 

16.3 Reordering ls Listings

In the previous tutorial ( 16.2 ) , I introduced you to several different ways of sorting ls ' output, based on file time. There was one constant: ls listings were always from first (most recent) to last, moving (first) down each column and then across the page.

That's often not the most convenient way to look at the world. For example, if you're going to look at a directory with many, many files, it might be easier to list the files across the screen first, and then down. This would be particularly nice if you're using a pager (like pg or more ( 25.3 ) ) to read the listing. Then the first screenful will show the files at the top of the list. Here's an example:

jerry@ora ~/.bin
59 % 

ls -x | pg


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

This listing is "alphabetic"-not sorted by time. So README is first in the list (uppercase comes below lowercase), and alifile is next to it. The -x flag makes the output multi-column. BSD doesn't have -x . To get the same sorting order under BSD, pipe ls output through the cols ( 35.16 ) script.

Both BSD and System V have the -C option; it sorts filenames down columns instead of across. In fact, -C is the default on BSD when you aren't redirecting the output of ls . If BSD ls detects that it's writing anywhere other than a terminal, it defaults to single-column output, rather than multi-column. Under BSD, you'll need to use -C (or another technique like the cols script or pr -number ( 35.17 ) ) to get output in columns when you pipe ls output.

The -r option lists the files in reverse order. I find this particularly useful when I'm looking at modification times. Because -t shows files by modification time, newest first - using -tr shows files by modification time, oldest first:

jerry@ora ~/.bin 
61 % 

ls -tr


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

Adding the -u option shows the least recently accessed files first:

jerry@ora ~/.bin
63 % 

ls -tur


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

- JP , ML


Previous: 16.2 Finding Oldest or Newest Files with ls -t and ls -u UNIX Power Tools Next: 16.4 List All Subdirectories with ls -R
16.2 Finding Oldest or Newest Files with ls -t and ls -u Book Index 16.4 List All Subdirectories with ls -R

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