DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
About files and directories

Managing directories

Together with files, directories constitute the UNIX filesystem, which has a characteristic inverted tree structure beginning at the root directory. As with files (see ``Managing files''), you can create, rename, copy, erase, and compare directories. You can also set access permissions on directories to prevent or enable their use by other users. This is explained in ``Access control for files and directories''.

How directories are organized

Directories are organized as an inverted tree structure. Only one directory, at the top of the tree, is not contained in any other directory. This is called the root directory, and its name is represented by a slash (/) character. In this picture, directories are depicted by ovals, and ordinary files by rectangles. Notice that most of the directories have lines coming out of them, indicating that they lead to files or other directories.

When you work within a UNIX filesystem, you always have a current working directory. All filenames and commands that you type at the prompt are evaluated with respect to this position. When you log in, your current working directory is set to the directory created for your user account. This location is known as your home directory.

Any file or directory on the system can be specified uniquely by its pathname. Pathnames are instructions for finding a file or directory; they list, in order, each directory you must pass through to get to the file or directory in question. When the pathname is written down, the directories are separated by slashes (/). For example, the full pathname of the file called review in the diagram above is /usr/susanna/review. The first slash character denotes the root directory: all the others are separators.

A pathname that begins at the root directory is called an absolute or full pathname. There are also relative pathnames, which give directions to a file relative to your current working directory. Two dots in a row (..) represent the parent of the current directory.

For example, if you were working in the directory called /usr/sarah and you needed to specify the file in /usr/susanna called mbox, you could use the relative pathname ../susanna/mbox, or the absolute pathname /usr/susanna/mbox. (Remember, ``..'' refers to the parent directory of the current working directory; so in this case it refers to /usr, which is the parent of both susanna and sarah.)

An example: what the system contains

When an UnixWare system is installed, many directories are created automatically.


/
The root directory is the root of the filesystem tree. Every directory is a subdirectory of root.

/bin and /usr/bin
These directories contain most of the UNIX system commands. Generally, standard UNIX commands and applications are held in /bin, whereas group-specific commands and applications, that is, those used by a particular group of users, are held in /usr/bin.

/dev
This directory contains all the special device files. Special device files are access points to all the peripherals connected to the system.

/etc
This directory contains many of the system configuration files and system administration commands.

/unix
This file contains the UNIX kernel program. This program is loaded into memory when the operating system starts up. It is the heart of the UnixWare system.

/usr/lib
This directory contains many application library files.

/usr/spool
This directory is used by many commands for storing temporary files or files in a queue.

/var/opt/
This directory contains storage sections. See ``Creating a link to a directory'' for information about symbolic links and storage sections.

Creating a directory

To create a new directory, use the mkdir(1) (make directory) command, as follows:

mkdir directory

The directory argument can be either a simple name, in which case the new directory is created within the current working directory, or a pathname. For example, if you want to create a new subdirectory called projects in the directory called /u/workfiles, you can do the following:

   $ mkdir /u/workfiles/projects
You get the same result by entering the following:
   $ cd /u/workfiles
   $ mkdir projects
The cd command stands for ``change directory''. See ``Navigating the filesystem'' for more details.

For details of naming conventions for directories, see ``Filenaming conventions''.

You can create a directory within any directory where you have write permissions. See ``Access control for files and directories'' for details of how to manage access to files and directories.

You can create several directories at the same level. For example, to create directories called directory1 through directory3, use the following command:

   $ mkdir directory1 directory2 directory3
If you need to create an entire directory path at once, use the mkdir -p (path) option. The following command creates a directory called user_guide, and any of the other directories in the specified path that do not already exist:
   $ mkdir -p projects/myprojects/user_guide
Bear in mind that the efficiency of the filesystem has some impact on overall system performance. For example, you should not let your directories grow larger than necessary. Ideally, a directory should contain no more than 640 files (providing that the number of characters in each filename is 14 or less), otherwise it may take the system longer to search the directory whenever you access a file stored in it. If you use longer filenames, the limit may be lower. File size is also significant: large files impose overheads on access. See ``Looking at the contents of a file'' for more information.

Listing the contents of a directory

The names and other information about the files and subdirectories contained within a directory can be displayed using the ls(1) family of commands. In its simplest form, ls gives a list of the filenames found in the current working directory, as follows:

   $ ls
   cs-save         intro.err
   gav_make        nohup.out
   glossary.s      procs.txt
   graphics
For a full listing, giving file size, permissions, owner and other items of information, use the ls -l option, as follows:
   $ ls -l
   drwx------   2 chris   techpubs      64 Jul 07 17:19 tools
   drwxr-xr-x   2 chris   techpubs      80 Jul 06 16:51 trash
   -rw-r--r--   1 chris   techpubs    6204 Sep 23 09:34 travel
For a complete breakdown of this information, see ``File and directory attributes''.

As we saw in ``Filenaming conventions'', filenames may begin with a dot, in which case, the files are hidden from normal directory listings. The ls -a (all) option displays hidden files as well as normal files, as follows:

   $ ls -a
   .           ..          .history     .kshrc
   .mailbox    .profile    cs-save      gav_make
   glossary.s  graphics    intro.err    nohup.out
   procs.txt
To list the contents of another directory, without first moving to that directory, use the ls command, specifying the directory to look at as an argument, as follows:
   $ ls /u/workfiles/projects
This command line lists the contents of a directory called /u/workfiles/projects.

You need permission to read a directory before you can view its contents. See ``Access control for files and directories'' for an explanation of permissions.

The tilde-plus sequence (~+) is expanded by the shell to point to the current working directory (actually, the value of the PWD environment variable). A more useful variant of this notation is the tilde-minus notation (~-), which expands to the value of OLDPWD, that is, the previous working directory. This allows you to refer back to your earlier work without having to type in the relevant pathname, as follows:

   $ ls -l
   drwx------   2 chris   techpubs      64 Jul 07 17:19 tools
   drwxr-xr-x   2 chris   techpubs      80 Jul 06 16:51 trash
   -rw-r--r--   1 chris   techpubs    6204 Sep 23 09:34 travel
   $ cd ../project2
   $ ls -l
   -rw-r--r--   1 chris   techpubs    3137 Oct 24 17:49 agenda
   drwxr-xr-x   2 chris   techpubs      96 Aug 31 13:08 bin
   $ ls -l ~-
   drwx------   2 chris   techpubs      64 Jul 07 17:19 tools
   drwxr-xr-x   2 chris   techpubs      80 Jul 06 16:51 trash
   -rw-r--r--   1 chris   techpubs    6204 Sep 23 09:34 travel
If you list the contents of a directory that contains more files and subdirectories than can be displayed on one screen, the list scrolls continually until all the files have been displayed. This makes it very difficult to read them. To view the list one screen at a time, type the following:
   $ ls | more
The output from ls is piped to the more(1) command which then displays it; more prints its input one screen at a time. (See ``Running commands in a pipeline'' for more information about pipes.) Press <Enter> to scroll down by one line, or the <Space> bar to scroll down by one screen. Otherwise, you can pipe the output from ls into the pg(1) command, which performs a similar operation. The main difference between the two is that pg allows you to step backward through a file (by pressing the minus key (-)), as well as forward (by pressing <Enter> or the plus key (+)).

Another way to pause the scrolling is to use the <Ctrl>S and <Ctrl>Q keystrokes. Press <Ctrl>S to temporarily stop the scrolling, and <Ctrl>Q to continue. If you want to stop the listing completely, press <Del>. These keystrokes depend on your terminal setup; if they do not seem to work, ask your system administrator to help you.

Renaming a directory

To rename or move a directory, use the mv(1) (move) command, as follows:

mv oldname newname

oldname is the directory's current name, and newname is the new name you want to assign it. As with the mkdir command, arguments to mv may be simple names or pathnames, as follows:

   $ mv users_guide uguide
   $ mv appendix ../docset2/admin_guide/app
The first of these commands renames a subdirectory: the second renames a directory from appendix to app, at the same time moving it to another location.

Removing a directory

To remove an empty directory, use the rmdir(1) command, as follows:

rmdir directory

This will fail if there are any files or subdirectories in the directory. If this is so, you must either delete the files it contains, or move them to other directories. The only exceptions are the dot and dot-dot directories, which you cannot delete, and which are dealt with by the UNIX system itself.

You can remove a directory and any files it contains by using the rm -r option, as follows:

rm -r directory

Be very careful when doing this because the -r option tells rm to recursively enter any subdirectories and remove their contents. You may remove more than you expect. It is often safer to use the rm -i option. See ``Removing a file'' for information on interactive deletion. You must have write permission on a directory before you can remove it.

Comparing directories

Comparing directories is useful when two people are working on the same set of files, for example, the chapters of a book. By comparing the directories you can quickly identify which files are different.

To compare two directories, use the dircmp(1) (directory compare) command, as follows:

dircmp directory1 directory2

The output is a list of the differences between the directory listings for directory1 and directory2, for example:

   $ dircmp ./dir1 ./dir2
   Jan 12 10:54 1995  dir1 only and dir2 only Page 1
   

./t.appx.s ./t.prog.s ./t.scosh.s ./t.shell.s . . . Jan 12 10:54 1995 Comparison of dir1 dir2 Page 1

directory . same ./0.ct.s same ./00.partno.s same ./00.title.s . . .

The top of the listing consists of those files which are unique to one or other of the directories; in this example, dir2 contains four files which are not present in dir1. The listing then contains a detailed comparison of every file which is named in both directories.

© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 22 April 2004