Special ls Command Flags

It should be becoming clear to you that UNIX is the ultimate toolbox. Even some of the simplest commands have dozens of different options. On one system I use, ls has more than 20 different flags.

Task 4.5: Changing the Sort Order in ls

What if you wanted to look at files, but wanted them to show up in a directory sorting order different from the default (that is, column-first order)? How could you change the sort order in ls?


  1. The -x flag sorts across, listing the output in columns, or first-row order (entries are sorted across, then down):

    % ls -a
    .               .elm              .plan             Global.Software
    ..              .forward          .pnewsexpert      Interactive.Unix
    .Pnews.header   .ircmotd          .rnlast           Mail
    .accinfo        .login            .rnlock           News
    .article        .logout           .rnsoft           Src
    .cshrc          .newsrc           .sig              bin
    .delgroups      .oldnewsrc        .tin              history.usenet.Z
    % ls -x -a
    .               ..                .Pnews.header     .accinfo
    .article        .cshrc            .delgroups        .elm
    .forward        .ircmotd          .login            .logout
    .newsrc         .oldnewsrc        .plan             .pnewsexpert
    .rnlast         .rnlock           .rnsoft           .sig
    .tin            Global.Software   Interactive.Unix  Mail
    News            Src               bin               history.usenet.Z
    
  2. There are even more ways to sort files in ls. If you want to sort by most recently accessed to least recently accessed, you use the -t flag:

    % ls -a -t
    ./                 ../             .rnlock            .cshrc
    .newsrc            News/           .rnlast            .sig
    .oldnewsrc         .tin/           .rnsoft            .plan
    .article           .ircmotd        Interactive.Unix    Mail/
    .elm/              .delgroups      .accinfo*          .Pnews.header*
    .forward           .login          Src/               .pnewsexpert
    history.usenet.Z   bin/            Global.Software    .logout
    

    From this output, you can see that the most recently accessed files are .newsrc and .oldnewsrc, and that it's been quite a while since .logout was touched. Try using the -t flag on your system to see which files you've been accessing and which you haven't.

  3. So far, you know three different approaches to sorting files within the ls command: column-first order, row-first order, and most recently accessed–first order. But there are more options in ls than just these three; the -r flag reverses any sorting order.

    % ls
    Global.Software    Mail/        Src/              history.usenet.Z
    Interactive.Unix   News/        bin/
    % ls -r
    history.usenet.Z   Src/         Mail/             Global.Software
    bin/               News/        Interactive.Unix
    
  4. Things may become confusing when you combine some of these flags. Try to list the contents of the directory that is one level above the current directory, sorted so the most recently accessed file is last in the list. At the same time, indicate which items are directories and the size of each file.

    % ls -r -t -F -s ..
    total 150
       2 bruce/    2 rank/       2 kane/       14 higgins/
       2 laura/    2 christine/  2 shane/      6 mac/
       2 cedric    2 peggy/      4 patrickb/   10 mark/
       2 james@    4 taylor/     4 green/      6 armstrong/
       2 vicki/    2 guest/      6 shalini/    4 david/
    

A better and easier way to type the preceding command would be to bundle flags into the single argument ls -rtFs .., which would work just as well, and you'd look like an expert!


Task 4.6: Listing Directory Trees Recursively in ls

In case things aren't yet complicated enough with ls, two more important valuable flags are available. One is the -R flag, which causes ls to list recursively directories below the current or specified directory. (If you are familiar with DOS, you can think of using the -R flag as being the same as the tree command in DOS.) If you think of listing files as a numbered set of steps, recursion is simply adding a step—the rule is if this file is a directory, list it too—to the list.


  1. When I use the -R flag, here's what I see:

    % ls -R
    Global.Software    Mail/      Src/     history.usenet.Z
    Interactive.Unix   News/      bin/
    Mail:
    Folders/  Netnews/
    Mail/Folders:
    mail.sent  mailbox    steinman   tucker
    Mail/Netnews:
    postings
    News:
    uptodate  volts
    Src:
    sum-up.c
    bin:
    Pnews*   punt*    submit*
    

    Try it yourself.

    Notice that ls lists the current directory and then alphabetically lists the contents of all subdirectories. Notice also that the Mail directory has two directories within it and that those are also listed here.

Viewing all files and directories below a certain point in the file system can be a valuable way to look for files (although you'll soon learn better tools for finding files). If you aren't careful, though, you may get hundreds or thousands of lines of information streaming across your screen. Do not enter a command such as ls -R / unless you have time to sit and watch information fly past.


If you try to list the contents of a directory when you don't have permission to access the information, ls warns you with an error message:

						% ls ../marv
../marv unreadable

Now ask for a recursive listing, with indications of file type and size, of the directory /etc, and see what's there. The listing will include many files and subdirectories, but they should be easy to wade through due to all the notations ls uses to indicate files and directories.

Task 4.7: Long Listing Format in ls

You've seen how to estimate the size of a file by using the -s flag to find the number of blocks it occupies. To find the exact size of a file in bytes, you need to use the -l flag. (Use a lowercase letter L. The numeral 1 produces single-column output, as you've already learned).


  1. The first long listing shows information for the LISTS file.

    % ls -l LISTS
    -rw-------  1 taylor     106020 Oct  8 15:17 LISTS
    

    The output is explained in Figure 4.1.

    Figure 4.1. The meaning of the -l output for a file.

For each file and directory in the UNIX file system, the owner, size, name, number of other files pointing to it (links), and access permissions are recorded. The creation, modification, and access times and dates are also recorded for each file. The modification time is the default time used for the -t sorting option and listed by the ls long format.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset