Navigating the directory tree and listing files



Last revision August 3, 2004

Table of Contents:
  1. The Unix file system
  2. The directory tree
  3. File ownership and permissions
  4. Files as units
  5. Examining file contents
  6. Other commands
  7. Standard Files and Data Pipes

One of the attributes of any process, including your login shell, is that it has a working directory. That is, files referenced by commands and programs are assumed to belong in that directory unless you explicitly indicate another directory. Here are descriptions of basic commands that you use to show, set, and list your working directory.

pwd

Tells you the name of your current working directory. There are no arguments to this command. Use anytime to verify a directory change or just see where you are.

cd

Used to change your working directory. Give it a single argument which is the name of the new directory that you want to set as your working directory.

Can give a relative pathname, which means to go further down the directory tree from current position. In this case, do not start relative pathname with a slash; rather, start it with the name of the next sub-directory in line.

Or can give an absolute pathname. Start this with a slash, and then specify all the sub-directories in the path beginning from the root directory.

If you do not give any pathname argument at all, cd just returns you to your own home directory.

Relative and absolute pathnames are concepts used by all commands that refer to files. Use ../ followed by a sub-directory name in order to move over to another sub-directory at the same level (remember that .. is a reference to the parent directory of the current directory).

ls

Used to display a list of the files within a directory.

If no options are given, ls lists the files in the current working directory. If one or more directory names are given as filename arguments, ls will list the contents of those directories.

If specific filenames (either relative to the current directory or with an absolute pathname) are given as arguments, ls will list them if they exist - one way to see if a file exists, or to get more information about it.

When invoked without any options, ls just shows the names of files, usually in several columns to conserve space on the screen.

The most useful option is -l (letter "ell", not numeral "one"). This gives the long listing, which has complete information about the file. Note that the exact format of the ls -l command output may differ slightly from one system to another. Like most Unix programs, the output from the ls command is terse and contains no explanatory headers. You need to reference the on-line manual or notes like these to explain the meanings of the various columns of output.

For example, in the gp111ins home directory on pangea, the command

pangea> ls -l symlink.awk

yields the output:

-rw-r--r-- 1 gp111ins class 115 Oct 29 1987 symlink.awk

The output of the ls -l command is interpreted as:

    1. First group of 10 characters gives type of file and permissions.
      • Type is first letter: - means a plain file, d means a directory, l (letter "ell", not numeral "one") is a symbolic link. There are a few other special system types.
      • Permissions control who can read, execute, modify or delete the file.
    2. Next is an integer indicating how many links (names) this file has. Most plain files have only one link. Directories have at least two links: their name as stored in the parent directory, and the symbolic name . (dot) that they store meaning myself. Directories have additional links if they contain subdirectories, as each subdirectory has a link back to the parent stored under the symbolic name .. (dot dot).
    3. The third and fourth items are the user account that owns the file and the group to which the file belongs (for example, user gp111ins and group class).
    4. Next is an integer giving the number of bytes (characters) in the file.
    5. The date of last modification is given. If it is more than about 6 months old, you get the month, day, and year; younger and you get the month, day, and time of day (hour:minute).
    6. Finally, you see the name of the file. If the file is a symbolic link, it also shows the name of the real file to which it points.

Other options to ls provide different types of information.

-a This option will show those files that begin with a dot character; these are normally invisible. This also gives information about the directory file itself (.) and its parent directory (..).
 
-R This option does a recursive list. It first lists the files in the specified (or current) directory; then if any of those files are themselves sub-directories, it lists their contents; and so forth down through all sub-directories in the tree. This one can generate a lot of output, so use it carefully.
 
-F This option is useful to give a quick visual indication of what kinds of files you have, without making a long listing like the -l option. It does this by putting a / character after the name of any file that is a sub-directory, and a * character after the name of any file that is an executable program. There are also some other less common trailing characters; read the manual.

Suggestion: Capitalize directory names to help them stand out. Uppercase letters sort before lowercase letters in Unix, so capitalized names will be listed by ls before names that are all lowercase.

You can combine several options, either separately listed, like -a -l, or with concatenated letters, like -al.

Other potentially useful options are those that sort the output list before displaying it. Output is normally sorted alphabetically by filename. Usually, you would use these in conjunction with the -l option so the relevant sort field is displayed.

-t This option says to sort by file age, with youngest displayed first. By default, age means the time when the contents (data) of the file were last modified (or created).
 
-tc These options together sort by youngest first, where youngest is now defined as the time when the inode (administrative data, such as permissions, ownership, etc.), rather than the file contents (data), was last modified.
 
-tu These options together sort by youngest first, where youngest is now defined as time of last access (last time either read or written).
 
-r This option reverses the order of sorting - either reverse alphabetical, or oldest first, depending upon which other sort options also used.

file

Makes a guess to determine the type of contents of a file. Its argument is a list of files or directories, either relative or absolute pathnames. It then looks at the first few bytes of each file and tries to guess whether the file is a directory, an executable command, a C program, a Fortran program, numeric data, English text, etc.

<--Previous Overview Next-->

Comments or Questions?