Directory viewing commands

You took a quick look around your home directory in earlier module. Now it is time to take a more detailed look. And ls is still the command to use.

ls has several options that allow you to view additional information about your files. Among the more commonly used are :


All: The -a option you have already seen. It displays all files in the target directory.


Long : The -l option display a variety of information in a table format. The first line is a total count of files being displayed. Then a line of information will be displayed for each file found.

This line breaks down into the following information.

The 1st column shows filetype and access permission for various types of users.

The 1st character of this column is the file type and will indicate one of the following : - (regular file), d (directory), s (symbolic), c (character special), b (block special), p (named pipe). Some Unix systems may have additional file types with the appropriate symbolic character.

The next 9 characters indicate access settings for the file.

The Unix system allows each account owner to create files and own the files created. It also requires a file to be associated with a known group.

When a user's account is created, the user is assigned an unique user id and a group association.

Every user belongs to a default group. Users can belong to any number of additional groups. Files created by a user can be associated to any group recognized by the system but to only one. Normally, this will be default group of the user as identified from the user's entry in the password/account system, but at the user's or system administrator's discretion, a different group association can be implemented.

The group association allows the user (owner) to control specific access to file by that group. And any member of the associated group to have custom access permissions.

However, only one user can own a file and a user does not belong to all groups, so the Unix system provides for another class of file access on the system. This class is referred to as "other". An individual who is not the owner of the file of interest and doesn't not belong to the group associated with the file is an "other".

The 9 access status characters of the 1st column are broken into three groups of three characters indicating access permissions for the owner (user id), the group association (group id), or other (everyone else).

Each user type group is three characters long and controls whether a file can be read (r), written (w), or executed (x). If a particular permission is off, a (-) will appear in the appropriate location.

For example, ls -l fit displays the following :

-rwxr-x--x    1  gilberg  staff       12  May 17 08:45   fit

The 1st character "-" is in the filetype column and indicates that this is a regular file. Then next three characters "rwx" indicate that the owner of the file "gilberg" has permission to read the file, write or modify it, and execute it. The next three characters "r-x" indicate that anyone in the group "staff" may read (or copy) the file and execute it but NOT make changes to this file. Finally, the last 3 characters in the 1st column, "--x", indicates that anyone else on the system may execute it but not read or change it.

Curiously, if a file's "group" permission is more restrictive than "other"s permissions and a user other that the owner belongs to the group, the more restrictive permissions will apply to that user.

The next column in the "ls -l" display is the link count. The link count represents the number of filenames assigned to this particular file. When and only when link count becomes zero will the Unix os considers the file deleted and make the storage space available to other users and files. We will discuss linking in detail in a later module.

The next two columns indicate the owner's login name and the associated group name respectively. If you use the -n option rather than -l, you will see the numeric values, the uid and gid, rather than the user and group names.

Column 5 indicates the size of the file in bytes. The next column grouping indicates the date and time the file was last modified. And the last column indicates the file's assigned name for this entry in this directory listing.


Directory : If ls is specified with a directory filename, it will display the contents of the directory. Sometimes, you need to see information about the directory itself. The -d will cause ls to be applied directly to the directory itself.

ls -d


Inode : Unix uses a data structure called an inode to track and control each file created on a Unix system. Each of these inodes is assigned a unique index value called an inode number. The -i option will cause ls to display a file's inode number. We will look at inode structures in detail at a later time.

ls -i


Quiet or Binary : On occasion, a file name will be created with an unprintable character in it. ls may display what looks like a normal file name, yet any attempt to access the file fails. To check for unprintable characters, use either -q option or -b. -q will generate a question mark for any characters which cannot be normally displayed. -b will display the octal value for that character. Once a user knows where these unprintable characters are, careful use of file name wildcards will allow access to the file.

ls -q
ls -b


One : This option will cause output to be displayed in a single column. This is useful when writing shell script that process lists of files.

ls -1


Reverse : Display in reverse sorted order. Other options can influence which field is the key value to sort on. By default, it will be the filenames.

ls -r


File access time : When used with -l or -n, -u will display the time the file was last accessed rather than the time it was last modified.

ls -lu


File status change time (inode modification time) : When used with -l or -n, -c will display the time the file's status changed. We will look at this in more detail when we examine how the filesystem works.

ls -lc


Sort by timestamp : Rather than sorting on the name, -t requests the files be listed by file modification times. The -c and -u can be used to pick alternative time values.

ls -t

The man page lists several other options. Feel free to play with them and see what gets listed. Keep in mind that some of these options have evolved or been created as part of the GNU utilities and may not be available on all Unix systems.

An exploration of file types
Overview