Working with directories.

Initially, all users are assigned a default home directory which is created by the administrator. However, a user will eventually need to organize files into categories for easier control and access.

Unix provides a variety of commands for creating, moving, setting access control to, and deleting directory listings.


Creating a directory

To create one or more directories, use the command :

mkdir dir1 dir2 dir3

The paths specified above are relative and the mkdir command will create the new directory[s] file in the current directory unless a path is specified.

mkdir $HOME/330/assn02

creates the assn02 directory in the 330 directory. However, the 330 directory must already exist.

To create both the 330 directory and assn02 subdirectory, use the -p (path) option:

mkdir -p $HOME/330/assn02

Although the command is simple, there are several conditions required for creating a directory.

The user must have write and execute permissions for directory that the new directory listing is to be created in.

The name chosen must not be already exist in that directory.

In general, the name chosen should consist of only :

  • Upper case letters (A-Z)
  • Lower case letters (a-z)
  • Numbers (0-9)
  • Underscore (_)
  • Period/dot (.)
  • Hyphen (-) Stay away from all other punctuation and spaces. Although it is possible to use these, you will find that manipulating such a named file can be difficult.

    A tip on dealing with spaces. If you transfer files from a Windows system, you may find you have created filenames with spaces in them. To work with such a filename use quotes :

    mv "my spacey directory" my_directory


    Changing the current working directory.

    cd target_directory

    Because the current working directory is an environmental feature of the command interpreter shell, cd is an internal command of the command interpreter currently running. Be aware that different command interpreters may support options that modify the behavior of cd. See the man page for the command interpreter of interest for specifics.

    cd takes a single argument, the target directory.

    The user may specify the target directory either with a path relative to the current working directory or with an absolute path.

    bash's version of cd supports 2 options :

    We will review this after discussing links in detail.


    Removing a directory

    To remove a directory, use :

    rmdir requires that the file be a directory, that it is an empty directory, and that the user have permissions to modify the parent directory of the targeted directory.

    rm -r (remove recursively) can be used to remove a directory containing files and subdirectories. The user must permission to read, write, and execute the directory being removed and any subdirectories listed.

    Be very careful when using the -r option. On Unix type systems, once a file is removed, it is not possible to recover it unless a backup system exists. And on our system it doesn't. :( .


    Copying a directory

    Because a directory is a list of filenames and their position in the filesystem and because any of the filenames listed may also be directories, the copy command must be run with the recursive option.

    cp -r original_directory new_directory

    When copying directories, you may find that linked filenames may or may not fare well. The reasons for this will become obvious when we cover links.


    Moving a directory

    Moving or renaming a directory is much simpler than copying one. In most cases, mv (move) modifies the directory a file is currently listed in and the directory it will be listed in rather than actually moving the file itself. Renaming is simply a move within the same directory listing.

    mv original_directory_name new_directory_name

    mv original_directory_name new_location

    Manipulating files other than directories.
    Overview