Unix Fundamentals: Directories
You might ask: how do I create and manage folders? UNIX, like other operating systems has folders; we call them directories. Directories in UNIX work just like folders on any graphical operating system. You have been in a directory all along without knowing it. You are in your home directory. We will see how to create directories, store files in them and manage our directories.
The mkdir
command makes a new directory. It takes one
argument, the name of the directory you intend to create. Let's make
a directory.
mkdir Projects
makes a directory called Projects; this directory is now empty. We can always get rid of an empty directory by typing
rmdir <garbageDirectory>
and that directory is removed. The rmdir
command will not
work if the directory is nonempty. There is a way to snip off
directories with their contents, but I will avoid it for now because
it is very dangerous. For now, you can delete the contents of a
directory, then remove the directory.
To get into our new directory Projects, enter this command:
cd Projects
and type ls
. You will see no files. This is because Projects
is empty. Open files named moo and baa with vi and place a little
text in them. Then enter ls -al
at the command line. You
will see just the files you created. The command ls
displays
only files in the directory you are currently occupying.
Sometimes you forget where you are. If you type cd
any time
you are in UNIX, you will automatically go back to your home
directory; you will feel like Dorothy going back to Kansas. You can
also type
pwd
This is "print working directory" and this will show your current location. If I type this right now, it says I am in
/home/faculty/morrison/public_html/programming/unixBasics
Let's see what is going on here. I know my home directory: it is
/home/faculty/morrison
This means I am inside a directory called unixBasics, which lives inside of programming, which lives inside of public_html, which lives in my home directory. Each slash represents a layer in the directory structure. Time for a field trip!
To get to our first destination, type cd /
. The directory /
is the "root" directory. If you think of the directory structure as an
upside-down tree (root at top), the directory / is at the top. Type
pwd
and see where you are. Type
ls
; you should see the directory home in there. Now type
cd home
; if you type ls
you will see the directory
faculty. See if you can follow this all the way down to my unixBasics
directory. From this, you see that you can step down through the
directory structure. How do you step up?
Try typing cd ..
; the special symbol ..
represents the directory above where we are. Now you can climb up and
down the directory structure!
Practice this; go back to your home directory. Make a new directory
called mudpies. Put files in it. Make new directories in mudpies,
got down inside these and make more directories and files. Practice
using cd
to navigate the tree you create. When you are done,
get rid of the whole mess; remember you have to go to the bottom,
empty out the files using rm
and then use rmdir
to
get rid of the empty directories.
If you type ls
in a directory, notice how any directories
inside it are in blue type and how files are in white type. You can
use the -F
option in ls
to print directory names
with a slash (/) after them. Try this. If you use the -l
option in ls
, you will see that in the permissions column,
the colum begins with a d.
-rw-rw-r-- 1 morrison morrison 0 Jun 9 14:54 bar -rw-rw-r-- 1 morrison morrison 0 Jun 9 14:54 foo drwxrwxr-x 2 morrison morrison 4096 Jun 9 14:54 junk
You can see there that bar
and foo
are empty files.
Notice the d at the beginning of the line in junk
; this tells
you junk
is a directory.
Finally, I shall do a brief discussion of permissions. You are the owner of your home directory and all directories and files it contains. This is your "subtree" of the system's directories belonging to you. There are three layers of permission: you, your group, and others. You is letter u, your group is letter g and others is letter o. There are three types of permission for each of these: read, write and execute. Read means that level can read the file, write means that level can execute the file, ant execute means that level can execute the file. Example:
-rw-rw-r--
on my file bar
means the following
- I can read or write to the file. I cannot execute.
- My group can read or write but not execute.
- The world can read this file but neither write nor execute.
If I want to execute this file myself I can use the chmod
command as follows
chmod u +x bar
I changed the u(ser's, that's me) permission to allow the user (me) to execute the file.
If I do not want the world to see this file I could enter
chmod o -x bar
and revoke permission for the world to see my file. Since I am the owner of the file, I have this right. In general you can do
chmod (u or g or o) (+ or -) (r or w or x) fileName
to manage permissions. There are other ways to do this; you can look these up.
While we are on a computer we might just want to program. What a thought!