More About Listing Files


Many options can be used with the ls command. They are used either to obtain additional information about files or to control the format used to display this information. This section introduces the most important options that were not covered earlier. You can find a description of all options to the ls command and what they do by consulting the man page for ls.

Listing Hidden Files

Files with names beginning with a dot (.) are hidden in the sense that they are not normally displayed when you list the files in a directory These are typically configuration files that are used regularly by the system, but you will only rarely read or edit them. Suppose you see something like this when you list the files in your home directory:

 $ ls Email notes Work

This example shows that your home directory contains files named Email, Work, and notes. But there may also be hidden files that do not show up in this listing. Examples of common hidden files are your .profile, which sets up your work environment, and the .mailrc file, which is used by the mailx electronic mail command. To avoid clutter, ls does not list hidden files unless you explicitly ask to see them.

To see all files in this directory, use ls -a:

 $ ls -a . .. .mailrc .profile Email notes Work

The example shows two hidden files. In addition, it shows the current directory and its parent directory as . (dot) and .. (dot-dot), respectively.

Controlling the Way Is Displays Filenames

By default, in many flavors of UNIX, ls displays files in multiple columns, sorted down the columns, as shown here:

 $ ls 1st            b         folders   misc           proposals 8.16letter     BOOKS     letters   Names          temp abc            drafts    memos     newletter      x

You can use the -x option to have names of files displayed horizontally, in as many lines as necessary For example,

 $ ls -x 1st        8.16letter    abc        b        BOOKS     drafts folders    letters       memos      misc     Names     newletter proposals  temp          x

You also can use the -1 (one) option to have files displayed one line per row (as the old version of ls did), in alphabetical order:

 $ ls -l 1st 8.16letter abc b BOOKS drafts folders letters memos misc Names newletter proposals temp

Showing Nonprinting Characters

Occasionally you will create a filename that contains nonprinting characters. This is usually an accident, and when it occurs it can be hard to find or operate on such a file.

Suppose you mean to create a file named Budget but accidentally type CTRL-B rather than SHIFT-B. When you try to run a command to read or edit Budget, you will get an error message, because no file of that name exists. If you use ls to check, you will see a file with the apparent name of udget, since the CTRL-B is not a printing character. If a filename contains only nonprinting characters, you won’t even see it in the normal ls listing. You can force ls to show nonprinting characters with the -b option. This replaces a nonprinting character with its octal code, as shown in this example:

 $ ls udget    Expenses $ ls -b \002udget     Expenses

An alternative is the -q option, which prints a question mark in place of a nonprinting character:

 $ ls -q ?udget Expenses

Sorting Listings

Several options enable you to control the order in which ls sorts its output. Two of these options are particularly useful.

You can have ls sort according to when each file was created or last modified with the -t (time) option. With this option, the most recently changed files are listed first. This form of listing makes it easy to find a file you worked on recently

To reverse the order of a sort, use the -r (reverse) option. By itself, ls -r lists files in reverse alphabetical order. Combined with the -t option, it lists oldest files first and newest ones last.

Combining Options to Is

You can use more than one option to the ls command simultaneously For example, the following shows the result of using the ls command with the options -F and -a on a home directory:

 $ ls -aF ./    ../   .mailrc*   .profile*     Letters/     memos    notes@

You can combine any number of options. In the following example, three options are given to the ls command: -a to get the names of all files, -t to list files in temporal order (the most recently modified file first), and -F to mark the type of file.

 $ ls -Fat ./      memos@      Letters/        notes     .profile*      .mailrc*       ../

The Long Form of Is

The ls command and the options discussed so far provide limited information about files. For instance, with these options, you cannot determine the size of files or when they were last modified. To get more information about files, use the -l (long format) option of ls.

Here is an example of what the long format of ls might look like:

 $ ls -1 total 8 drwxr-xr-x   3   jmf   group1    4096   Nov 29 02:34   Letters Irwxr-xr-x   1   jmf   group1      13   Apr  1 21:17   memos -> Letters/memos -rwxr-xr-x   2   jmf   group1     682   Feb  2 08:08   notes

The first line (“total 8”) in the output gives the amount of disk space used in blocks. (A block is a unit of disk storage. On Linux systems, a block contains 1,024 bytes; on Solaris, a block is 512 bytes. The command df can be used to determine the block size-see Chapter 13 for details.) The rest of the lines in the listing show information about each of the files in the directory.

Each of these lines contains seven fields. The name of the file is in the seventh field, at the far right. (In the listing for memos, you can see that there is an arrow with another filename after it. The file memos is a symbolic link to that file, Letters/memos.) To the left of the filename, in the sixth field, is the date when the file was created or last modified. To the left of that, in the fifth field, is its size in bytes.

The third and fourth fields from the left show the owner of the file (in this case, the files are owned by the user jmf), and the group the file belongs to (group1). The concepts of file ownership and groups are discussed later in this chapter.

The second field from the left contains the link count. For a file, the link count is the number of linked copies of that file. For example, the “2” in the link count for notes shows that there is a linked copy of it somewhere. For a directory, the link count is the number of directories under it plus two (one for the directory itself, and one for its parent). So the directory Letters must have one subdirectory, since it has a link count of three.

The first character in each line tells you what kind of file this is.

-

Ordinary file

c

Special character file

d

Directory

l

Symbolic link

b

Special block file

P

Named pipe special file

This directory contains one ordinary file, one directory, and one symbolic link. (Notice that even though notes is a hard linked file, it does not have an l next to it, because it is not a symbolic link like memos.) Special character files and block files are covered as part of the discussion of system administration in Chapter 14.

The rest of the first field, that is, the next nine characters (in these examples, rwxr-xr-x), contains information about the file’s permissions. Permissions determine who can work with a file or directory and how it can be used. Permissions are an important and somewhat complicated part of the UNIX file system that will be covered next.




UNIX. The Complete Reference
UNIX: The Complete Reference, Second Edition (Complete Reference Series)
ISBN: 0072263369
EAN: 2147483647
Year: 2006
Pages: 316

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net