Uploaded by DFrmBricks

UNIX Directory Navigation Project

advertisement
CT-152
Project #1
Directory Navigation
General
A directory is like a “folder” (in Windows). The general directory structure on all UNIX systems is the same.
Within this directory structure are directories for users. Within these directories, the users can create further
directories. Users can also navigate to their own directories or to other users’ directories.
1. General Directory Structure
When you log-in to a UNIX system, any file that you create is in your “home” directory. To find-out what this
home directory is called, execute the command
$ pwd
(Do not type the ‘$’—it is a “prompt.”) The response should be something like the following
/home/smith
(The name “smith” should be replaced with your log-in username.)
The way to visualize this directory structure is as follows:
/
/home
/home/smith
/home/jones
The symbol “/“ corresponds to the top-level directory (of the entire computer or operating system). The
symbol “/home“ corresponds to a sub-directory within the top-level directory. The symbol “/home/smith“
corresponds to a sub-directory within “/home“ the directory.
You should be able to look at your “sibling” directories under “/home“ by executing the following commands:
$ cd ..
$ pwd
go-up one directory level
$ ls
list the contents of the directory
You should see a whole bunch of names (with numbers in some cases). Get a screenshot of this output and
paste it into your project report.
To go back to your original (“home”) directory execute the command
$ cd $HOME
or just
$ cd
or
$ cd smith
(where “smith” is replaced with your log-in username). Within your user directory, you may create files and
sub-directories.
2. Creating Simple Files
The quickest way to create a small, text file is to use the commands “cat” or “echo.”
$ cat << xxx >> xyz
Now is the time for all good men to come to the aid of their party.
xxx
A new file should exist called xyz that contains the text “Now is the time for all good men to
come to the aid of their party.” To see if this file exists merely enter
$ ls
and you should see the response
xyz
To see the (text) contents of the file enter
$ cat xyz
You should see the response
Now is the time for all good men to come to the aid of their party.
Another way of creating a simple file (in one line) is to do the following:
$ echo “Now is the time for all good men to come to the aid of their party.” >> xyz
Create a few files “abc” and “def” and fill them with the text ‘abctext’ and ‘deftext’. Upon entering
$ ls
you should see the response
xyz
abc
def
Upon entering
$ cat abc
you should see the response
abctext
3. Creating Subdirectories
Suppose you wished to create a directory called “temp” (within your current directory). To do so, enter
$ mkdir temp
You may then go to this directory
$ cd temp
and create files there.
Create the following file and directory hierarchy within your newly-created “temp” directory:
smith
temp
temp1
abc1
def1
temp2
ghi1
abc2
temp3
def2
abc3
def3
ghi3
For each file, write-out the full directory “path,” e.g., /home/smith/temp/temp1/abc1.
You may copy an entire “directory branch” using the following command:
$ cp –r temp3 temp4
The “-r” option stands for “recursive”: it copies all sub-directories within the directory.
What happens when you execute this command twice? (What is the directory tree under “temp4”?)
You may “save” the entire directory tree into a single file called temp.tar by executing the following
command (from the $HOME directory):
$ tar cf temp.tar temp
The command “tar” stands for “tape archive” (when archives were on magnetic tape). (Archives are like
“.zip” files.) If you should “lose” your directory tree, you may restore it from the archive (temp.tar) using
the following command
$ tar xf temp.tar temp
If you wish to just “look inside” the archive execute the command
$ tar tf temp.tar temp
(The options “c,” “x” and “t” stand for “create,” “extract” and “list.”) (We will cover more about this
“archiving” in the next project.)
4. More File Information
Go to your “home” directory (cd $HOME) and execute the following commands:
$ ls
you should see the responses
temp
Now enter
$ ls -lart
you should see a response that looks something like this:
drwxr-xr-x
10
smith smith 4096
2011-08-23 10:00
temp
This response is a “more complete listing” of the contents of your directory. The most interesting part of this
listing are the letters “drwxr-xr-x.” These letters stand for “directory,” “read,” “write” and “execute.”
These letters represent “permissions,” i.e., can you read this file, can you write this file and can you execute
this file. You will notice that there are three “reads” and three “writes” (and, possibly, three “exccutes”). The
first group of three corresponds to you, the creator or “user” of the file. The second group of three
corresponds to a “group” of users (of which you are a member). One computer or operating system can
have many “groups.” In this example, everyone in your group can “read” your file, but not “write” to your file.
The last group of three letters is “others,” i.e., everyone on the system (in any “group”). (The system
administrator can do “anything” with your files.)
If you wish to change the read, write or execute “permissions,” use the command chmod. Suppose you
wished to protect your file (or directory) from your (accidentally) overwriting this file. You would execute the
command
$ chmod –w temp
If you wish your “group” to not read from the file, execute the command
$ chmod g-r temp
If you wish your “group” to read from the file and everyone else (“others”) to not read from the file, enter
$ chmod g+r,o-r temp
Important note: for someone to “read” your directory and its sub-directories, i.e., get istiings, the directory
must have “read” and “execute” privileges.
Tricky stuff: If you look at the letters in the “more complete listing” (e.g., drwxr-xr-x) each letter in
rwxrwxrwx can be thought of as a binary digit. For example rwxr-xr-x can be thought of as 111101101.
If we represent this binary number as a three octal-digit number we would have 755. You could quickly
grant everyone read and execute privileges, grant all group members read and execute privileges and grant
yourself full privileges by using the command
$ chmod 755 temp
Modify your “temp” directory tree so that everyone in your “group” can read your temp1, but no one
(besides yourself) can read your temp2 sub-directory. Test-it out by having another member of your
“group,” i.e., another member of your class, access your directory.
Related documents
Download