Unit 4: Unix Directory Structure and Navigation
Unix Directory
Unix organizes files according to a directory structure. The following shows an example.
This structure is also known as a directory tree.
There are two important directories that you need to know.
Root Directory
The root directory is the directory at the top of the directory tree1. It is simply referred to as /
, without any name. Under the root directory are many other systems directory, which a casual user does not normally need to (and have no permission to) modify.
Home Directory
Each user has his/her own home directory.
The above figure shows where the home directory of the user ooiwt
resides in the directory tree. The user ooiwt
may create files or directories in his/her home directory, but not elsewhere unless permission is given.
The home directory is referred to with the symbol ~
in bash
. Sometimes we add the username behind ~
to indicate the home directory of the other user. E.g., ~bob
means the home directory of a user named bob
.
Current Working Directory
A user can navigate around the directory tree. The current working directory is the directory that the user is currently in. In contrast to the root and home directory, which are fixed2, the current working directory changes as the user moves around. Knowing the current working directory is important since this is the default location in the directory tree a command executes. As such, many systems by default display the current working directory as part of the bash
command prompt.
The current working directory is referred to with the symbol .
in bash
.
Parent Directory
The parent directory is directory one layer up from the current directory.
The parent directory is referred to with the symbol ..
in bash
.
To summarize, here are the short form representations:
Symbol | Meaning |
---|---|
/ |
the root directory |
~ |
the home directory |
. |
the current working directory |
.. |
the parent directory |
Specifying a Path
To specify a directory or a file in the Unix directory tree, we can use either the absolute path or the relative path.
Absolute path
The absolute path is constructed as follows, starting from the root of the directory structure, find a path (a sequence of directories) to the location that you want to specify, then concatenate the names of the directories together, separated by the forward-slash /
. This is a similar notation used for Web site URLs so you should already be familiar with it. For instance, the path
/home/o/ooiwt
is the absolute path of the directory named ooiwt
in the figure above.
An absolute path is independent of the current working directory and always start with /
or ~
Relative path
The relative path is dependent on the current working directory. To refer to another location, start from the current directory, and find a path (a sequence of directories) to the location that you refer to. When we go up a tree, we use ..
to represent the directory.
For example, referring to the figure above, if we are in the directory /home/b
, and we wish to refer to /home/o/ooiwt
, we can use the relative path ../o/ooiwt
. If we wish to refer to /home/b/bob
, we can use the relative path bob
.
A relative path never starts with /
.
Directory-related Commands
Now, let's take a look at some basic commands available in bash
that
deals with navigation and directories.
pwd
: Print Current Working directory
pwd
shows you which directory you are currently in.
Type pwd
into the command prompt, and it will print the absolute path to your current working directory. For instance,
Suppose you are in /home/o/ooiwt
, entering
pwd
/home/o/ooiwt
ls
: LiSt content of a directory
The ls
list the content in the current working directory.
Rule of Silence
Unix follows the economical rule of silence: programs should not print unnecessary output, to allow other programs and users to easily parse the output from one program. So, if ls
has nothing to list, it will list nothing (as opposed to, say, printing "This is an empty directory.")
mkdir
: MaKe a subDIRectory
The mkdir
command creates a subdirectory with the given name in the current directory.
In the example below, we assume that we start with an empty directory.
$ ls
$ mkdir workshop
$ ls
workshop
$ ls -F
workshop/
Here, we create a directory called workshop
. Now, when we ls
, you can see the directory listed.
You may also use ls -F
for more information (-F
is one of the many options/flags available for the ls
command. To see a complete list of the options, refer to the man pages, i.e., man ls
.)
The slash /
beside the filename tells you that the file is a directory. A normal file does not have a slash beside its name when "ls -F" is used.
You may also use the ls -l
command (hyphen el, not hyphen one) to display almost all the file information, include the size of the file and the date of modification.
Use Up Arrow for Command History
bash
maintains a history of your previously executed commands, and you may use the Ctrl+P (previous) and Ctrl+N (next) to
go through it. Press the Ctrl+P until you find a previously executed command. You may then press Enter to execute it or edit the command before executing it. This is handy when you need to repeatedly execute a long bash
command.
cd
: Change Directory
To navigate in the directory tree, changing the current working directory from one to another, we use the cd
command.
$ pwd
/home/o/ooiwt
$ cd workshop
$ pwd
/home/o/ooiwt/workshop
Suppose our starting working directory is /home/o/ooiwt
, after we cd
into workshop
, the current working directory becomes /home/o/ooiwt/workshop
. Note that cd
can take in either an absolute path or a relative path. The example above takes in a relative path as the argument.
As mentioned in the Introduction to Shell unit, it is common to include the current working directory into the shell's prompt. So, you may see your command prompt updated to include the new working directory.
Entering cd
alone (without argument) brings you back to your home directory.
rmdir
: ReMove a subDIRectory
rmdir
removes a subDIRectory in the current directory -- note that a directory must be empty before it can be removed.
The command
$ rmdir workshop