Web | This Site

Unix for Beginners
Unix Shell Programming
Advanced Unix
Unix Useful References
Unix Useful Resources
Selected Reading
© 2011 TutorialsPoint.COM

| Home |  | References |  | Discussion Forums |  | About TP

# Unix - Directory Management

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.

UNIX uses a hierarchical structure for organizing files and directories. This structure is often referred to as a directory tree . The tree has a single root node, the slash character ( /), and all other directories are contained below it.

# Home Directory:

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.

You can go in your home directory anytime using the following command:

    [amrood]$cd ~
    [amrood]$

Here ~ indicates home directory. If you want to go in any other user's home directory then use the following command:

    [amrood]$cd ~username
    [amrood]$

To go in your last directory you can use following command:

    [amrood]$cd -
    [amrood]$

# Absolute/Relative Pathnames:

Directories are arranged in a hierarchy with root (/) at the top. The position of any file within the hierarchy is described 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
    /users/sjones/chem/notes
    /dev/rdsk/Os3

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

    chem/notes
    personal/res

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

    [amrood]$pwd
    /user0/home/amrood

    [amrood]$

# Listing Directories:

To list the files in a directory you can use the following syntax:

    [amrood]$ls dirname

Following is the example to list all the files contained in /usr/local directory:

    [amrood]$ls /usr/local

    X11       bin          gimp       jikes       sbin
    ace       doc          include    lib         share
    atalk     etc          info       man         ami

# Creating Directories:

Directories are created by the following command:

    [amrood]$mkdir dirname

Here, directory is the absolute or relative pathname of the directory you want to create. For example, the command:

    [amrood]$mkdir mydir
    [amrood]$

Creates the directory mydir in the current directory. Here is another example:

    [amrood]$mkdir /tmp/test-dir
    [amrood]$

This command creates the directory test-dir in the /tmp directory. The mkdir command produces no output if it successfully creates the requested directory.

If you give more than one directory on the command line, mkdir creates each of the directories. For example:

    [amrood]$mkdir docs pub
    [amrood]$

Creates the directories docs and pub under the current directory.

# Creating Parent Directories:

Sometimes when you want to create a directory, its parent directory or directories might not exist. In this case, mkdir issues an error message as follows:

    [amrood]$mkdir /tmp/amrood/test
    mkdir: Failed to make directory "/tmp/amrood/test";
    No such file or directory
    [amrood]$

In such cases, you can specify the -p option to the mkdir command. It creates all the necessary directories for you. For example:

    [amrood]$mkdir -p /tmp/amrood/test
    [amrood]$

Above command creates all the required parent directories.

# Removing Directories:

Directories can be deleted using the rmdir command as follows:

    [amrood]$rmdir dirname
    [amrood]$

Note: To remove a directory make sure it is empty which means there should not be any file or sub-directory inside this directory.

You can create multiple directories at a time as follows:

    [amrood]$rmdir dirname1 dirname2 dirname3
    [amrood]$

Above command removes the directories dirname1, dirname2, and dirname2 if they are empty. The rmdir command produces no output if it is successful.

# Changing Directories:

You can use the cd command to do more than change to a home directory: You can use it to change to any directory by specifying a valid absolute or relative path. The syntax is as follows:

    [amrood]$cd dirname
    [amrood]$

Here, dirname is the name of the directory that you want to change to. For example, the command:

    [amrood]$cd /usr/local/bin
    [amrood]$

Changes to the directory /usr/local/bin. From this directory you can cd to the directory /usr/home/amrood using the following relative path:

    [amrood]$cd ../../home/amrood
    [amrood]$

# Renaming Directories:

The mv (move) command can also be used to rename a directory. The syntax is as follows:

    [amrood]$mv olddir newdir
    [amrood]$

You can rename a directory mydir to yourdir as follows:

    [amrood]$mv mydir yourdir
    [amrood]$

# The directories . (dot) and .. (dot dot)

The filename . (dot) represents the current working directory; and the filename .. (dot dot) represent the directory one level above the current working directory, often referred to as the parent directory.

If we enter the command to show a listing of the current working directories files and use the -a option to list all the files and the -l option provides the long listing, this is the result.

    [amrood]$ls -la
    drwxrwxr-x    4    teacher   class   2048  Jul 16 17.56 .
    drwxr-xr-x    60   root              1536  Jul 13 14:18 ..
    ----------    1    teacher   class   4210  May 1 08:27 .profile
    -rwxr-xr-x    1    teacher   class   1948  May 12 13:42 memo
    [amrood]$

Advertisement

Online Image Processing

Indian Baby Names