## 8.7. Some GNU ls Features

A lot of the GNU utilities came from Unix utilities -- but with extra features. The GNU ls command is no exception: as its info page (Section 2.9) says, "Because ls is such a fundamental program, it has accumulated many options over the years." Amen. Let's look at three of the options that aren't covered by other articles on ls.

An Emacs editor backup file (Section 19.4) has a name ending in `~` (tilde). If you use Emacs a lot, these files can really clutter your directories. The ls -B option ignores Emacs backup files:

>     $ ls
>     bar.c  bar.c~  baz.c  baz.c~  foo.c  foo.c~
>     $ ls -B
>     bar.c  baz.c  foo.c

The option -I (uppercase letter I) takes -B one step further: you can give a wildcard expression (shell wildcard pattern, not grep-like expressions) for entries not to list. (Remember that -- because you want to pass the wildcard pattern to ls, and not let the shell expand it first -- you need to quote (Section 27.12) the pattern.) For instance, to skip all filenames ending in .a and .o, use the wildcard pattern `*.[ao]`, like this:

>     $ ls
>     bar.a  bar.c  bar.o  baz.a  baz.c  baz.o  foo.a  foo.c  foo.o
>     $ ls -I "*.[ao]"
>     bar.c  baz.c  foo.c

The "minimalist" side of me might argue that both -B and -I are feeping creatures because you can get basically the same effect by combining plain old ls with one of the "not this file" shell wildcard operators. This next option is in the same category. Instead of using -S to sort the files by size, you could pipe the output of plain ls -l to sort -n (Section 22.5) and sort on the size field, then strip off the information you didn't want and . . . ahem. (Grumble, grumble.) Okay, -S really is pretty useful. `;-)` I use it a lot when I'm cleaning out directories and want to find the most effective files to remove:

>     $ ls -lS
>     total 1724
>     -rw-rw-r--    1 jerry    ora   395927 Sep  9 06:21 SunTran_map.pdf
>     -rw-------    1 jerry    ora   389120 Oct 31 09:55 core
>     -rw-r--r--    1 jerry    ora   178844 May  8 16:36 how
>     -rw-------    1 jerry    ora    77122 Oct 29 08:46 dead.letter
>        ...

-- JP

Copyright © 2003 O'Reilly & Associates. All rights reserved.