NC State Computing
Center

Directory Structures

Let's shift our focus to take a look at the directories and subdirectories where we store and manage our files. Directory names can be up to 256 characters long and can accept numbers and some common special characters, except blanks. (See the rules for filenames for more information.)

Creating Directories

To organize your data into directories, you simply create new ones. The directory you are creating will be one level below your working directory. This is important to keep in mind, since if you were in a directory called "classes" and you created a subdirectory, it would have a path of "~/classes/subdirectory" (remember, the ~ signifies your home directory). To make sure that you are in your home directory, type cd at your unity prompt. The "cd" command always returns you to your home directory.

Use the mkdir directory_name command (make directory) to create a new directory. We'll make a directory called "temp":

	unity% mkdir temp[return]
	unity%
It appears that nothing happened and you are still in your home directory, but if you do an ls, you'll see that the "temp" directory shows up in your listing.

	unity% ls [return]
	file1 file2_test temp
	unity%

Changing Directories

To change into another directory, use the cd command ("change directory") followed by the directory name to change into.To change to the temp directory:

	unity%  cd temp[return]
	unity%
        unity% pwd [return] /afs/unity/users/n/noell/temp
If you do an ls -l, you will see that there are no files in this directory, though the directory exists. Note that your prompt did not change to reflect your new directory location. To learn how to change your prompt so that it would show something like this, take the Computing Services' course Advanced Unity Commands, or refer to the Center's tutorials on Unity and "How to customize your prompt" available via the Web: http://www2.ncsu.edu/ncsu/cc/pub/tutorials/unity_intro/42_customizing.html.

When you are changing to a directory or subdirectory that is off of your home directory, you simply enter the directory name. To change to a directory that is not in your home directory, you need to enter the complete path of the directory you want, using the format cd /dirname (e.g., cd /afs/unity/project/www/ncsu/) instead of just dirname. The "/"indicates the root directory and takes you up to the root directory and then begins looking down the directory "tree" for the requested directory.

If we do a pwd (print working directory), we can see exactly where we are:

 
	unity% pwd [return]
	/afs/unity/users/u/unix#/temp
	unity%
To go back one level, enter cd ..; the ".." tells Unity to take us up one level. In this particular example, it returns us to our home directory; however, if we had been down two directory levels, it would have brought us up one level.

	unity% cd.. [return]
	unity%
At any time, regardless of where you are located, you can return to your home directory by entering "cd" and return (without the "..").
	unity% cd[return]
	unity%
Create one more directory (make sure that you are in your home directory):

	unity% cd [return]
	unity% mkdir data [return]

Removing Directories

To remove or delete a directory that you no longer need, use the rmdir command, where rmdir stands for remove directory. You cannot remove a directory until you have deleted all files within that directory.

Delete the "temp" directory. Make sure that you are in your home directory:

 
	unity%cd [return]
	unity% pwd [return]
	/afs/unity/users/u/unix#
	unity%rmdir temp  [return]
	rm: remove temp? y [return]
	unity%
Once this command is issued and you answered in the affirmative that you want to delete the directory, the directory "temp" no longer exists. If there had been files in the "temp" directory, it would have asked for confirmation before deleting them. When you are prompted to delete files and subdirectories, if you answer "n" to deleting a file or directory, unix will not delete the directory or file. Directories must be empty before they can be deleted.


Go on to next section, Files in unix

Return to Table of Contents