The ls Command

This section introduces you to the ls command, which enables you to examine the file system and see what files are kept where.

Task 4.1: All About the ls Command

From the examples in the preceding hour, you've already figured out that the command used to list files and directories in UNIX is the ls command.


All operating systems have a similar command, a way to see what's in the current location. In DOS, for example, you're no doubt familiar with the DIR command. DOS also has command flags, which are denoted by a leading slash before the specific option. For example, DIR /W produces a directory listing in wide-display format. The DIR command has quite a few other options and capabilities.

Listing the files in a directory is a pretty simple task, so why all the different options? You've already seen some examples, including ls -a, which lists hidden dot files. The answer is that there are many different ways to look at files and directories, as you will learn.

  1. The best way to learn what ls can do is to go ahead and use it. Turn to your computer, log in to your account, and try each command as it's explained.

  2. The most basic use of ls is to list files. The command ls lists all the files and directories in the present working directory (recall that you can check what directory you're in with the pwd command at any time).

    % ls
    Archives       Mail           RUMORS.18Sept  mailing.lists
    InfoWorld      News           bin            newels
    LISTS          OWL            iecc.list      src
    

    Notice that the files are sorted alphabetically from top to bottom, left to right. This is the default, known as column-first order because it sorts downward, then across. You should also note how things are sorted in UNIX: The system differentiates between uppercase and lowercase letters, unlike DOS. (The Macintosh remembers whether you use uppercase or lowercase letters for naming files, but it can't distinguish between them internally. Try it. Name one file TEST and another file test the next time you're using a Macintosh.)

Some of the UNIX versions available for the PC—notably SCO and INTERACTIVE UNIX—have an ls that behaves slightly differently and may list all files in a single column rather than in multiple columns. If your PC does this, you can use the -C flag to ls to force multiple columns.


It's important that you always remember to type UNIX commands in lowercase letters, unless you know that the particular command is actually uppercase; remember that UNIX treats Archives and archives as different filenames. Also, avoid entering your account name in uppercase when you log in. UNIX has some old compatibility features that make using the system much more difficult if you use an all-uppercase login. If you ever accidentally log in with all uppercase, log out and try again in lowercase.


Task 4.2: Having ls Tell You More

Without options, the ls command offers relatively little information. Questions you might still have about your directory include these: How big are the files? Which are files, and which are directories? How old are they? What hidden files do you have?


  1. Start by entering ls -s to indicate file sizes:

    % ls -s
    total 403
      1 Archives     1 Mail      5 RUMORS.18Sept  280 mailing.lists
      1 InfoWorld    1 News      1 bin              2 newels
    108 LISTS        1 OWL       4 iecc.list        1 src
    
  2. To ascertain the size of each file or directory listed, you can use the -s flag with ls. The size indicated is the number of kilobytes, rounded upward, for each file. The first line of the listing also indicates the total amount of disk space used, in kilobytes, for the contents of this directory. The summary number does not, however, include the contents of any subdirectories, so it's deceptively small.

A kilobyte is 1,024 bytes of information, a byte being a single character. The preceding paragraph, for example, contains slightly more than 400 characters. UNIX works in units of a block of information, which, depending on which version of UNIX you're using, is either 1 kilobyte or 512 bytes. Most UNIX systems now work with a 1 kilobyte block. When you use the -s flag, you're being shown how many of these blocks compose each file.


  1. Here is a further definition of what occurs when you use the -s flag: ls -s indicates the number of blocks each file or directory occupies. You then can use simple calculations to convert blocks into bytes. For example, the ls command indicates that the LISTS file in my home directory occupies 108 blocks. A quick calculation of block size × number of blocks reveals that the maximum file size of LISTS is 110,592 bytes.

    You can always estimate size by multiplying the number of blocks by 1,000. Be aware, however, that in large files, the difference between 1,000 and 1,024 is significant enough to introduce an error into your calculation. As an example, bigfile is more than 3 megabytes in size (a megabyte is 1,024 kilobytes, which is 1,024 bytes, so a megabyte is 1,024×1,024, or 1,048,576 bytes):

    % ls -s bigfile
    3648 bigfile
    
  2. The file actually occupies 3,727,360 bytes. If I estimated its size by multiplying the number of blocks (3,648 as seen from the preceding command) by 1,000 (which equals 3,648,000 bytes), I'd have underestimated its size by 79,360 bytes. (Remember, blocks × 1,000 is simply an easy estimate!)

The preceding example reveals something else about the ls command. You can specify individual files or directories you're interested in viewing and avoid having to see all files and directories in your current location.


Depending on what shell you're using, and what version of UNIX you have, you might be able to press the Tab key while entering a filename and have it automatically completed. Try it; if you have this shortcut, it's great!


  1. You can specify as many files or directories as you like, and separate them by spaces:

    % ls -s LISTS iecc.list newels
     108 LISTS         4 iecc.list     2 newels
    

    In the preceding hour, you learned that UNIX identifies each file that begins with a dot (.) as a hidden file. Your home directory is probably littered with dot files, which retain preferences, status information, and other data. To list these hidden files, use the -a flag to ls:

    % ls -a
    .             gopherrc   .oldnewsrc     .sig         RUMORS.18Sept
    ..           .history    .plan          Archives     bin
    .Agenda      .info       .pnewsexpert   InfoWorld    iecc.list
    .aconfigrc   .letter     .report        LISTS        mailing.lists
    .article     .login      .rm-timestamp  Mail         newels
    .cshrc       .mailrc     .rnlast        News         src
    .elm         .newsrc     .rnsoft        OWL
    

    You can see that this directory contains more dot files than regular files and directories. That's not uncommon in a UNIX home directory. However, it's rare to find any dot files other than the standard dot and dot-dot directories (those are in every directory in the entire file system) in directories other than your home directory. (These dot files are often created by applications you use, and they should not be edited.)

  2. You used another flag to the ls command—the -F flag—in the preceding hour. Do you remember what it does?

    % ls -F
    Archives/      Mail/          RUMORS.18Sept  mailing.lists
    InfoWorld@     News/          bin/           newels
    LISTS          OWL/           iecc.list      src/
    

    Adding the -F flag to ls appends suffixes to certain filenames so that you can ascertain more easily what types of files they are. Three different suffixes can be added, as shown in Table 4.1.

Table 4.1. Filename Suffixes Appended by ls -F
Suffix Example Meaning
/ Mail/ Mail is a directory.
* prog* prog is an executable program.
@ bin@ bin is a symbolic link to another file or directory.

  1. If you're familiar with the Macintosh and have used System 8 or a more recent version of the operating system, you may recall the new feature that enables the user to create and use an alias. An alias is a file that does not contain information, but acts, instead, as a pointer to the actual information files. Aliases can exist either for specific files or for folders. Windows folk know this as a “shortcut” file.

    UNIX has offered a similar feature forever, which in UNIX jargon is called a symbolic link. A symbolic link, such as bin in Table 4.1, contains the name of another file or directory rather than any contents of its own. If you could peek inside, it might look like bin = @/usr/bin. Every time someone tries to look at bin, the system shows the contents of /usr/bin instead.

    You'll learn more about symbolic links and how they help you organize your files in Hour 6, “Creating, Moving, Renaming, and Deleting Files and Directories.” For now, just remember that if you see an @ after a filename, it's a link to another spot in the file system.

  2. A useful flag for ls (one that might not be available in your version of UNIX) is the -m flag. This flag outputs the files as a comma-separated list. If there are many files, -m can be a quick and easy way to see what's available:

    % ls -m
    Archives, InfoWorld, LISTS, Mail, News, OWL, RUMORS.18Sept,
    bin, iecc.list, mailing.lists, newels, src
    

Sometime you might want to list each of your files on a separate line, perhaps for a printout you want to annotate. You've seen that the -C flag forces recalcitrant versions of ls to output in multiple columns. Unfortunately, the opposite behavior isn't obtained by use of a lowercase c. (UNIX should be so consistent!) Instead, use the -1 flag to indicate that you want one column of output. Try it.


Task 4.3: Combining Flags

The different flags you've learned so far are summarized in Table 4.2.


Table 4.2. Some Useful Flags to ls
Flag Meaning
-a List all files, including any dot files.
-F Indicate file types; / = directory, * = executable.
-m Show files as a comma-separated list.
-s Show size of files, in blocks (typically, 1 block = 1,024 bytes).
-C Force multiple-column output on listings.
-1 Force single-column output on listings.

What if you want a list, generated with the -F conventions, that simultaneously shows you all files and indicates their types?

  1. Combining flags in UNIX is easy. All you have to do is run them together in a sequence of characters, and prefix the whole thing with a dash:

    % ls -aF
    ./             .gopherrc      .oldnewsrc     .sig
    ../            .history*      .plan          Archives/
    .Agenda        .info          .pnewsexpert   InfoWorld/
    .aconfigrc     .letter        .report        LISTS
    .article       .login         .rm-timestamp  Mail/
    .cshrc         .mailrc        .rnlast        News/
    .elm/          .newsrc        .rnsoft        OWL/
    
  2. Sometimes it's more convenient to keep all the flags separate. This is fine, as long as each flag is prefixed by its own dash:

    % ls -s -F
    total 403
       1 Archives/      1 Mail/          5 RUMORS.18Sept   280 mailing.lists
       1 InfoWorld/     1 News/          1 bin/              2 newels
     108 LISTS          1 OWL/           4 iecc.list         1 src/
    
  3. Try some of these combinations on your own computer. Also try to list a flag more than once (for example, ls -sss -s), or list flags in different orders.

Very few UNIX commands care about the order in which flags are listed. Because it's the presence or absence of a flag that's important, listing a flag more than once doesn't make any difference.


Task 4.4: Listing Other Directories Without Changing Location

Every time I try to do any research in the library, I find myself spending hours and hours there, but it seems to me that I do less research than I think I should. That's because most of my time is for the tasks between the specifics of my research: finding the location of the next book, and finding the book itself.


If ls constrained you to listing only the directory you were in, it would hobble you in a similar way. Using only ls would slow you down dramatically and force you to use cd to move around each time.

Instead, just as you can specify certain files by using ls, you can specify certain directories you're interested in viewing.

  1. Try this yourself. List /usr on your system:

    % ls -F /usr
    5bin/         diag/         lddrv/        share/        ucbinclude@
    5include/     dict/         lib/          source/       ucblib@
    5lib/         etc/          local/        spool@        xpg2bin/
    acc/          export/       lost+found/   src@          xpg2include/
    acctlog*      games/        man@          stand@        xpg2lib/
    adm@          hack/         mdec@         sys@
    bin/          hosts/        old/          system/
    boot@         include/      pub@          tmp@
    demo/         kvm/          sccs/         ucb/
    

    You probably have different files and directories listed in your own /usr directory. Remember, @ files are symbolic links in the listing, too.

  2. You can also specify more than one directory:

    % ls /usr/local /home/taylor
    /home/taylor:
    Global.Software   Mail/             Src/             history.usenet.Z
    Interactive.Unix  News/             bin/
    /usr/local/:
    T/            emacs/        ftp/          lists/        motd~
    admin/        emacs-18.59/  gnubin/       lost+found/   netcom/
    bin/          etc/          include/      man/          policy/
    cat/          faq/          info/         menu/         src/
    doc/          forms/        lib/          motd          tmp/
    

    In this example, the ls command also sorted the directories before listing them. I specified that I wanted to see /usr/local and then /home/taylor, but it presented the directories in opposite order.

I've never been able to figure out how ls sorts directories when you ask for more than one to be listed—it's not an alphabetical listing. Consider it a mystery. Remember that if you must have the output in a specific order, you can use the ls command twice in a row.


  1. Here's where the dot-dot shorthand for the parent directory can come in handy. Try it yourself:

    % ls -m ..
    armstrong, bruce, cedric, christine, david, green,
    guest, higgins, james, kane, laura, mac, mark,
    patrickb, rank, shalini, shane, taylor, vicki
    

    If you were down one branch of the file system and wanted to look at some files down another branch, you could easily find yourself using the command ls ../Indiana/Personnel or ls -s ../../source.

  2. There's a problem here, however. You've seen that you can specify filenames to look at those files, and directory names to look at the contents of those directories, but what if you're interested in the directory itself, not in its contents? I might want to list just two directories—not the contents, just the directory names themselves, as shown here:

    % ls -F
    Archives/      Mail/           RUMORS.18Sept  mailing.lists
    InfoWorld/     News/           bin/           newlists
    LISTS          OWL/            iecc.list      src/
    % ls -s LISTS Mail newlists
     108 LISTS         2 newlists
    Mail:
    total 705
     8 cennamo    27 ean_houts     4 kcs     21 mark    7 sartin
    28 dan_sommer  2 gordon_haight 34 lehman  5 raf     3 shelf
    14 decc       48 harrism       64 mac     7 rock   20 steve
     3 druby      14 james         92 mailbox 5 rustle 18 tai
    
  3. The problem is that ls doesn't know that you want to look at Mail unless you tell it not to look inside the directories specified. The command flag needed is -d, which forces ls to list directories rather than their contents. The same ls command, but with the -d flag, has dramatically different output:

    % ls -ds LISTS Mail newlists
     108 LISTS        1 Mail/        2 newlists
    

    Try some of these flags on your own system, and watch how they work together.

To list a file or directory, you can specify it to ls. Directories, however, reveal their contents, unless you also include the -d flag.


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

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