Main Course Webpage

Course Slack Page

In-Class Lab 8: File Utilities

As you know, filenames are an illusion. Data on the computer is represented as numbers. This includes where data exists. What we might know as a file named resume in our home directory might be known to the system as data object 45663 on device #5. Our home directory is a data object that is simply interpreted as a table associating data object numbers with some text names: irrelevant as far as the system is concerned. (These data object numbers are called inode numbers, since they refer to the master table that keeps track of all data objects: i.e. the inode table. The term inode stands for information node.

A link is often referred to as another name for a file. A better definition would be an association between a name and a data object [number]. All filenames are links; in fact, links are essentially what are placed in directories. Most data objects that are regular files have a single link. Some regular files have multiple links. All directories have at least two links.

Unix has two types of links: hard links and symbolic (or soft) links. Tradtionally, use of the term link implied hard link, which is the origin of the term. Symbolic links, which were added to Unix later, however, have become so common and practical that we now use this more readily.

Begin by creating a links directory to work in

Hard Links

In this exercise you will practice creating and using hard links. Every association between a name and a data object is a hard link, so a hard link can refer to anything. A user, however, can only create a hard link to a regular file. Creating hard links to other types of objects is reserved to the operating system.

You should be in your links directory. Then, start your script session.

  1. Create a single file named resume by placing a string of text in it using cat or echo. Then, list the file with the options -li Notice the inode number. This is the number that is used internally in Unix to identify the data object. (If we were strict about our terminology here we would say that the name resume is a link to the data whose inode number is shown)

  2. Add a second link to the data using the ln command. Name the link resume_ln. List both names with the -li option. What do you notice? using the cat command, display the contents of resume_ln.

  3. Remove the name resume using rm. Then list the directory contents again using -li. Display the contents of the file resume_ln.

  4. Rename resume_ln to resume. If a friend walked up to your terminal now, would he know that steps 2 and 3 had occurred? In other words, could he tell that the original resume had been deleted?

  5. Again, create a second link to resume. Name it resume_ln again. Then, create a directory named subdir and move resume to it. Can you cat resume_ln now? How about subdir/resume?

  6. Try to create a hard link to subdir. Can you?

  7. Change the permissions of resume_ln to 777. Then list resume_ln and subdir/resume using -li. What happened to the permissions of subdir/resume? Can you explain this?

  8. Overwrite the file resume_ln with a different string of text. Then use cat to display both resume_ln and subdir/resume. Are you surprised?

  9. Using the mv command, move resume_ln to the directory /tmp (If you are sharing a linux machine with another student, this might be difficult. You have to rename resume_ln as you move it). Display the contents of your file on /tmp and the file subdir/resume.

  10. Overwite subdir/resume with a different string of text and redisplay both files. Can you explain this? List both using the -li option. What do you notice about the inode numbers?

  11. Last, try to create a hard link named link to the [non-existent] files foo. Can you?

Soft Links

You should be in your links directory. You will need a resume file in your links directory when you start this exercise. You could either retrieve the one from your subdir directory or create a new one.

  1. Set the permissions of resume to 644. Then make two symbolic links to resume. The first one should contain a relative path to resume (e.g. resume). This link should be named rel_symlink. The second should contain an absolute path to resume. It should be named abs_symlink. You can easily do this by using $PWD/resume (PWD is a variable that contains the path to the current directory). Using the cat command, ensure that the symbolic links work.

  2. List the symbolic links and the file they point to using the -l option. Note that the permissions, owner, group, size, and what appears in the name field of the symbolic links. List the symbolic links and resume again using the -lL options. What is different?

  3. Move the resume file to the parent directory. Try to cat the file using the symbolic links again. Do they work? What does ls -l show now? How about if you ad the -L option? Move the resume file back when you are finished.

  4. Now move the resume file back to the parent directory. Try using the symbolic links again. What happens? Move resume and the symbolic links back to the links directory and ensure that they work correctly.

  5. Change the permission of the relative symbolic link to 600. Then list it with -l What do you see? List it again after adding the -L option. What permissions changed?

  6. Create a symbolic link and put the string unix is weird in it. Try to cat the symlink. Then create a file named unix is weird, putting a string of text in it. Try to cat the symlink again. This shows that a symlink and the item it refers to are independent.

  7. Create a symbolic link to the current directory. What happens when you run ls on it?

Delete the links directory and its contents when youa re finished.

For answers and what to try out command line, refer to Greg Boyd's original assignment file.

Submitting Your Homework Assignment

Submit your answers to questions 2, 4-8, 10-11 in Part 1 and answers to questions 2-5, 7 as a normal text file in Part 2 on Canvas.