fredrik.eriksson

Coffee and a keyboard

Unix directories

The directory system supports a multilevel hierarchy. Files and directories have access protection. Files and directories are accessed through pathnames. Files support multiple name links. Removable filesystems are also supported.

All data in UNIX is organized into files. All files are organized into directories. These directories are organized into a tree-like structure called the filesystem. The following diagram describes the top level organization of the UNIX filesystem.

In this example, dev, etc, usr, and lib are directories. Directories contain other files or directories. Plain files contain text or binary data and contain no information about other files or directories. Users can make use of this same structure to organize their files.

Like children of one parent, no two files in the same directory can have the same name. Files in different directories, like children of different parents, can have the same name.

The filename you choose should mean something. Too often, a directory is filled with important files with names like foobar, wombat, and junk. A meaningless name won’t help you recall the contents of a file. Use filenames that are descriptive of the content.

Here is a partial list of directories and what files they contain:

/bin
This is where the executable files arelocated. They are available to all users.
/dev
These are device drivers.
/etc
Supervisor directory commands,configuration files, disk configurationfiles, reboot files, valid user lists,group, ethernet, hosts, where to sendcritical messages.
/lib
compiler libraries
/tmp
scratch processes, editors, compilers, anddatabases
/mnt
empty, used for disks
/lost+found
orphans go here (look here after systemcrash)

A directory is a file whose sole job is to store file names and related information. All files, whether ordinary, special, or directory, are contained in directories.

The directory in which you find yourself when you first login is called your home directory. You will be doing much of your work in your home directory and subdirectories that you’ll be creating to organize your files.

As we saw earlier, directories are arranged in hierarchy with root (/) at top. The position of any file within the hierarchy is descibed by its pathname. Elements of a pathname are separated by a /. A pathname is absolute if it is described in relation to root, so absolute pathnames always begin with a /. These are some example of absolute filenames.

/etc/passwd/home/cyberops/dev/tty0

A pathname can also be relative to your current working directory. Relative pathnames never begin with /. Relative to user cyberops home directory, some pathnames might look like this:

programming/srcpersonal/

To determine where you are within the filesystem hierarchy at any time, enter the command to print the current working directory.

$ pwd/home/cyberops$

Notice that this is an absolute pathname. This is the pathname of the current working directory.

Directories are created by the following command:

mkdir [options]

If the option to change permission mode is not given, the directory will have default permissions set to read, write, and execute for the user and read and execute for group and others. The files . and .. are created automatically. In order to create a sub-directory, you must have write permission on the parent directory. The owner id and the group id are set to the real users id and group id, respectively.

Directories can be deleted using the rmdir command

rmdir [options]

Normally, directories are deleted using the rmdir command. Before the directory can be removed, it must be empty; that is, it must not contain any files.

To move around in the filesystem, use the cd (change directory) command.

cd [dirname]

The mv (move) command can also be used to rename a directory.

mv

Example:

$ mv programming oldprogramming$

This will have the effect of changing the name of the directory programming into oldprogramming. The permissions on the directory will remain the same.