Section 3.14. Listing the Contents of a Directory: ls


[Page 55 (continued)]

3.14. Listing the Contents of a Directory: ls

Once the "heart" file was created, I wanted to confirm its existence in my home directory and see how many bytes of storage it used. To do this, I used the ls utility, which lists information about a file or a directory. Figure 3-14 describes how ls works.

Figure 3-14. Description of the ls command.

Utility: ls -adglsFGR { fileName }* { directoryName }*

With no arguments at all, ls lists all of the files in the current working directory in alphabetical order, excluding files whose name starts with a period. The -a option causes such files to be included in the listing. Files that begin with a period are sometimes known as "hidden" files. To obtain a listing of directories other than the current directory, place their names after the options. To obtain listings of specific files, place their names after the options. The -d option causes the details of the directories themselves to be listed, rather than their contents. The -g option lists a file's group. The -l option generates a long listing, including permission flags, the file's owner, and the last modification time. The -s option causes the number of disk blocks that the file occupies to be included in the listing (a block is typically between 512 and 4K bytes). The -F option causes a character to be placed after the file's name to indicate the type of the file: * means an executable file, / means a directory file, @ means a symbolic link, and = means a socket. The -G option causes group information to be omitted from the listing. The -R option recursively lists the contents of a directory and its subdirectories.


Some of the ls options described in Figure 3-14 won't mean a lot right now, but will become increasingly relevant as this book progresses.

Here's an example of ls:

$ ls              ...list all files in current directory. heart $ ls -lG heart     ...long listing of 'heart'. -rw-r--r--  1     glass     106   Jan 30 19:46    heart $ _ 



[Page 56]

I'll describe the exact meaning of each field in the long directory listing later in this chapter, but for now I'll give you a brief overview (Figure 3-15).

Figure 3-15. Description of output from the ls command.

Field #

Field value

Meaning

1

-rw-r--r--

The type and permission mode of the file, which indicates who can read, write, and execute the file.

2

1

The hard link count (discussed much later in this book).

3

glass

The username of the owner of the file.

4

106

The size of the file (in bytes).

5

Jan 30 19:46

The time that the file was last modified.

6

heart

The name of the file.


You may obtain even more information by using additional options:

$ ls -alFs         ...extra-long listing of current dir. total 3             ...total number of blocks of storage. 1 drwxr-xr-x  3   glass    cs      512  Jan 30  22:52 ./ 1 drwxr-xr-x 12   root     cs     1024  Jan 30  19:45 ../ 1 -rw-r--r--  1   glass    cs      106  Jan 30  19:46 heart $ _ 


The -s option generates an extra first field, which tells you how many disk blocks the file occupies. On my Linux system, each disk block is 1024 bytes long, which implies that my 106-byte file actually takes up 1024 bytes of physical storage. This is a result of the physical implementation of the file system, which is described in Chapter 13, "Linux Internals." The -a option causes ls to include a listing of all hidden files, which are files whose name begins with a period. "." and ".." are hidden files that correspond to the current directory and its parent directory, respectively. The -F option appends a / to all files that are directories.




Linux for Programmers and Users
Linux for Programmers and Users
ISBN: 0131857487
EAN: 2147483647
Year: 2007
Pages: 339

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