User Tools

Site Tools


haas:spring2011:unix:labs:lab2


Corning Community College


UNIX/Linux Fundamentals



Lab 0x2: Files and Directories

~~TOC~~

Objective

To become familiar with the UNIX directory structure as well as file permissions.

Reading

In “UNIX for the Beginning Mage”, please read:

  • Chapter 4 (pages 37-44).
  • Chapter 5 (pages 45-58).

In “Harley Hahn's Guide to UNIX and Linux”, please read:

  • Chapter 23 (“The UNIX Filesystem”, pages 627-658).
  • Chapter 24 (“Working with Directories”, pages 659-714).
  • Chapter 25 (“Working with Files”, pages 715-766).

In “Learning the UNIX Operating System, 5th Edition”, please read:

  • Chapter 3 (“Using Your UNIX Account”, pages 42-65).

Background

Directories

The UNIX filesystem is built on a hierarchical structure. The base, or root, directory is the starting point of the filesystem. It is identified by the /, which is also used as the directory separator. / is the lowest point on the filesystem- you cannot go beyond it.

From the base of the filesystem are the traditional subdirectories that contain the UNIX system as we know it. While there can be any number of directories, the traditional ones found include: /bin, /etc, /home, /lib, /mnt, /root, /sbin, /tmp, /usr, and /var.

Although many variations can occur, here is an example diagram of the filesystem structure on Lab46 (and with the exception of our /home directory tree, most everything else will be fairly standard across default Linux installs.

Illustration of UNIX filesystem directory tree

Each directory will typically hold certain types of files, as seen below:

Directory Description
/bin Essential basic tools for normal system usage
/etc Configuration files
/home Location of the system's user directories
/lib, /lib64, /lib32 Contains important system libraries
/mnt Common place to mount additional filesystems
/root The superuser's home directory
/sbin Essential system administration utilities
/tmp Temporary directory
/usr Additional (secondary) system functionality & userspace tools
/var Misc. items (mail files & databases)
1. Using the cd command, do the following:
a.Switch to the root (/) directory (aka base of the filesystem). How did you do it?
b.Get a directory listing. Do you see the above-mentioned directories?
c.Which ones have you dealt with in previous assignments?
d.Try changing into these subdirectories and listing files. Are there any directories you are not allowed in? Which ones?

Home Directory

Your home directory is the starting point of your login session. It contains customized login files as well as any personal files you have created while on the system. In Lab #0 you used the pwd utility to display the actual location of your home directory.

2. Do the following:
a.Change to the root (/) of the filesystem.
b.Going step-by-step, show me how you would get back to your home directory. (ie do not use any directory separators (/), or any shortcuts (~ or just hitting cd)).
c.Show how you accomplished this.

NOTE: Do not just issue a cd and hit enter. I want you to trace your steps from the root of the filesystem to your home directory.

Working Directory

Your prompt reflects the current working directory. This is the directory you are currently located in on the filesystem. The pwd utility will display the full path of your working directory.

3. Do the following:
a.Change to the /tmp directory and run the pwd utility.
b.What is your current working directory?

Pathnames

Your book talks about two types of pathnames: absolute and relative.

An absolute pathname is a specific reference to a location on the filesystem. When you issue the pwd command, it returns the absolute pathname of your current location. When an absolute pathname is given, the final destination is immediately known.

For example: If you type “cd /usr/bin” at your prompt, it will take you to the /usr/bin directory, regardless of where you currently are. This is because you are giving a precise destination.

A relative pathname, on the other hand, refers to something reachable from your present location. A relative pathname's final destination isn't as defined. It only makes sense from the particular starting location.

For example: If you are in your home directory and type “cd src” at your prompt, you will be taken into your src/ subdirectory. If you change to the root and again try to “cd src” the system will give you an error because there is no subdirectory in /.

4. For the following table, determine if the pathname is absolute or relative:
a.src
b.../../../../../usr/etc/../man/man2
c./var/public/unix
d./usr/sbin

The . and .. files

If you give ls the -a option while in any directory, you will notice that any given directory will have the . and .. directories. These files serve a special meaning for directory & pathname navigation.

The . directory always refers to your current directory.

The .. directory, on the other hand, refers to the previous (or parent) directory to your current directory. In Lab #0 you went to the .. directory while in your home directory to end up at /home.

Both . and .. can be used as relative pathnames, as they direct you somewhere from your current position.

5. Record your current working directory and do the following:
a.Change to the “.” directory. Check your present working directory.
b.Did it change? Explain.
c.Change to the root (/) directory. Now change to the .. directory.
d.Has your directory changed? Explain.

Files

Everything you encounter on the UNIX filesystem is a file. There are three types of files in UNIX: regular files, directories, and special files.

regular (or ordinary) files are the text and executable files we've been playing with.

A directory is merely a file that points to other files.

Special files consist of devices, network pipes, and sockets.

In Learning the UNIX Operating System, 5th Edition on page 51, the “Type” column in figure 3-5 gives a clear indication from ls regarding what type of file is being listed. The - and d are the most common. Others seen include l for symbolic link, b for block device, c for character device, p for a pipe, and s for socket.

6. Using the -l (dash ell) option to ls, look at files within the following directories and determine if they are predominantly regular, directory, or special files.
a./var/log
b./dev
c./
d./etc/init.d

Permissions

Going back to page 51, there is reference to a file's access modes. Page 57 also begins a section on File Access Permissions.

File Permissions allow for customized access to the files on a UNIX system, according to three levels of authority- user, group, and other. There are three modes of access for each category- read, write, and execute/search.

Observe the following table:

Type Description
user (u) the user that owns the file
group (g) the group that owns the file
other (o) everyone else on the system that is not included in the top 2 categories. Commonly referred to as “world”

Terminology: You execute files and search directories. Both are represented as the x bit.

Looking at the figure 3-5 in the book (or any output of ls -l), the first column of output consists of the file's type and access modes. There are nine places available for the access modes, which corresponds perfectly to the scheme described above. The first 3 places describe the file owner's permissions, the next three describe the file group's permissions, and the remaining three places describe what permissions anyone else on the system has.

7. Change to the /usr/bin directory and take a look at the permissions on the vim utility.
a.What user owns this file?
b.What can the owner do with the file?
c.What can the group do with the file?
d.What can anyone else on the system do with the file?

Octal and Symbolic Permissions

File permissions and attributes are dually hierarchial in UNIX. Not only do you have access control with regards to file ownership, there is also access control over file attributes (indicating what actions can be performed on the files).

It is with a combination of these two hierarchies that we get the UNIX file access structure.

Two methods exist for manipulating file access permissions, and both will be described here. The traditional method has involved the use of octal numbers, which as we will see below, nicely works to support the data we are representing.

To appeal to a more “user-friendly” environment, symbolic permissions are also present, which allow us to use specially designated symbols to manipulate the permissions.

Observe the following table:

Octal Symbolic Description
read 4 r view / read the file
write 2 w save / create / modify / delete the file
execute / search 1 x run / parse through contents of a file

NOTE: As explored above, a “directory” in UNIX is considered a type of file.

Be sure you read over the section on “File Attributes” in your text (pages 41-46).

Read access is represented by a 4. Write access is a 2, and Execute access is a 1. To obtain the octal value, you add the necessary access bits together.

For example- if the owner of a text file can read and write it, they have both read (4) + write (2) = read/write (6) access to that file. If both the group and the world can only read (4) that same file, it is said that the file has permissions of 644.

This information is displayed visually for you when using the -l option to the ls utility. A diagram of such appears as follows:

user group world
- r w x r w x r w x

Of course, under each field of “user”, “group”, or “world”, you can have your desired settings of “read”, “write”, and “execute”. Attributes left unset will be represented symbolically as a dash (-), and numerically as 0.

8. View the permissions of the following files and determine their octal permissions:
a./var/log/daemon.log
b./etc/resolv.conf
c./usr/bin/split
d.Your home directory

Symbolic permissions in some respects are not as flexible as the octal permissions, but allow for easy changes of individual modes. Your textbooks explain and give examples of how to use the symbolic notation.

9. Using symbolic notation:
a.How would you assign write permission to the world?
b.How would you remove the execute bit from the owning group?

The chmod(1) utility is used to change file permissions. It accepts both symbolic and octal permissions. Please be sure to reference its manual page for additional information.

The concepts presented here will be important in your usage of the system throughout the semester. Make sure you understand the material presented here, and to ask questions if something does not make sense.

Exercise

10. Using the mkdir(1) utility, create a directory named lab2/ in your home directory. Then, using chmod(1), set the directory with octal permissions to the following criteria:
a.give the directory full read/write/search for the owning user
b.no permissions for group
c.and search-only permission for other
d.What was the exact incantation used? (combine parts a-c into one command-line)

As we discovered earlier in the lab, the UNIX filesystem is a hierarchical structure, starting at /, the root, and propagating from that point on.

Your home directory exists somewhere within this hierarchy, and so do the files within your home directory.

11. Do the following:
a.Draw an ASCII tree showing me your home directory within the UNIX directory structure.
b.In addition to your home directory, show me 3 additional files and directories within it.
c.Place your lovely drawing in this week's journal entry. Make sure it looks okay.

One thing you may have noticed is that at several different levels of the UNIX filesystem there are directories with identical names, such as ~/bin/, /bin/, /usr/bin/, and /usr/local/bin/.

It is important to make this distinction between the various similarly-named directories located on the system as they reside in a path. Same name but different entities play an important role in many areas.

12. Explore the following:
a.Investigate the various “bin” directories on the system.
b.Prove that they are all independent entities (give a couple examples of proof).

Conclusions

All questions in this assignment require an action or response. Please organize your responses into an easily readable format and submit the final results to your instructor.

Your assignment is expected to be performed and submitted in a clear and organized fashion- messy or unorganized assignments may have points deducted. Be sure to adhere to the submission policy.

The successful results of the following actions will be considered for evaluation:

  • your responses to questions submitted at the following form:

<html><center><a href=“http://lab46.corning-cc.edu/haas/content/unix/submit.php?lab2”>http://lab46.corning-cc.edu/haas/content/unix/submit.php?lab2</a></center></html>

  • the response from the form (received via e-mail) saved as lab2.txt to your ~/src/unix/ directory
  • addition/commit of ~/src/unix/lab2.txt into your repository (CS 0x0 sets you up to do this).

As always, the class mailing list and class IRC channel are available for assistance, but not answers.

haas/spring2011/unix/labs/lab2.txt · Last modified: 2011/02/12 01:20 by 127.0.0.1