User Tools

Site Tools


haas:spring2013:unix:labs:lab1


Corning Community College


UNIX/Linux Fundamentals



Lab 0x1: Basic Utilities and their Manual Pages

~~TOC~~

Objective

To be able to effectively use a UNIX system, one must become familiar with the basic tools, as well as be able to know how to use the online manual.

This lab provides an introduction to many fundamental concepts.

Reading

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

  • Chapter 3 (pages 20-36).

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

  • Chapter 9 (“Documentation: The UNIX Manual and Info”, pages 189-221).
  • Chapter 10 (“Command Syntax”, pages 223-237).
  • 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:

  • Selections from Chapter 3 (“Using Your UNIX Account”):
    • “The UNIX Filesystem”, pages 42-54.
    • “Looking Inside Files with less”, pages 54-55.
    • “Completing File and Directory Names”, page 62.
  • Selections from Chapter 4 (“File Management”):
    • “File and Directory Names”, pages 66-67.
    • “File and Directory Wildcards”, pages 67-69.
    • “Managing Your Files”, pages 74-80.

Background

It has been said that the UNIX environment is terse and unfriendly. Terse at times it may be, but once the commands are understood and appreciated, the environment becomes a strong ally.

Only the original creators of UNIX could ever really explain their reasoning for naming certain commands the way they did. ls for list, cp for copy, and of course rm for remove are among the many such examples.

If you look at something from a space-efficient perspective, perhaps this naming scheme starts to make more sense.

Can you make out the meaning of a word if you remove all the vowels? Perhaps. How about a pattern of letters?

Let's take a list of basic commands and apply a simple rule:

Keep the first and third letter of the word the command represents, and drop the rest.

Ok, let's try a few commands:

English Word UNIX Command
list ls
copy cp
move mv
remove rm
link ln

While the rules don't work for very many additional commands besides these basic utilities, perhaps it'll help you remember these basic few.

Listing Files

1. Do the following:
a.Type ls and hit enter.
b.What do you see?

Hopefully, you will see all the files in the base of your home directory. For most of you, they should also appear colored. And no, it's not just to make it look pretty.

For starters, see those (hopefully) medium blue files called bin, public_html, and src? Those are actually directories. The color scheming system will make all directories that color.

That is how the coloring scheme works- it color codes by file type.

But isn't a directory different from a file? That's one of the wonderful things about UNIX that we'll encounter later- Practically everything on the UNIX filesystem is a file. A directory, then, is a file that contains the locations of other files.

Let's play a little with ls. It will undoubtedly be a useful command.

2. Do the following:
a.Type ls -l (dash “ell”) and hit enter.
b.What appears to have happened?

What we've just done is invoke ls with an argument, namely the -l. Traditionally, UNIX accepts arguments with the dash followed by a letter, in the form of a short argument. Long arguments also exist, and are commonly invoked by two dashes followed by a longer argument name. Remember, tradition is important in UNIX. You'd do well to take note of these things, as you never know where you'll encounter them later.

So, back to what you see on the screen. Let's take a close look at the line for your src directory:

example 0: long listing of files

lab46:~$ ls -l
drwxr-x--- 2 js001234 users 4096 Sep 24 16:58 src

While most of this information will be covered later, it will be important to understand generally what is seen here. In brief, the “drwxr-x—” on the left represent the permissions for that particular file. Looking at other files will yield different permissions.

Next, skip over to the 3rd column… instead of the “js001234” listed here, your own username should be present. This column represents the user on the system that owns that particular file, in this case- you.

Following that is the group owner of the file. This may be set to “users” or “unix”, depending on what other courses you are enrolled in. More on groups later.

Then you will come to the numeric value. This represents the size of the file in bytes. So the 4096 here would mean the file is 4kB in size.

The timestamp of the file follows, which basically is the time of modification/creation.

Lastly, is the name of the file itself.

3. Do the following:
a.Change your current directory to the /bin directory.
b.Do a long file listing (ls -l).
c.How large is the grep utility?
d.What is the timestamp on the cat utility?

Copying Files

To copy a file or files, the cp command is used. For people familiar with DOS, you will notice it works in the same fashion. The basic format of the command is: cp source destination

4. Do the following:
a.Change your current working directory to your home directory. How did you do this?
b.Copy the /etc/motd file into your current directory (see example 1 below)
c.Do an ls, do you see any changes? What?
d.Using the cp utility, make a copy of this file and name it lab1.text (see example 2 below)
e.Do a long file listing. Compare the sizes of motd and lab1.text. What do you observe?

example 1: copying a file into your current working directory

lab46:~$ cp /etc/motd .
lab46:~$ 

example 2: making an extra copy of a file

lab46:~$ cp motd lab1.text
lab46:~$ 

Moving Files

In UNIX, there is no rename command, however the same functionality can be accomplished by moving a file to a new name.

The mv command has the same format as the cp command just discussed. Let's use it to rename the motd file:

example 3: renaming a file

lab46:~$ mv motd lab1a.txt
lab46:~$ 

Get a file listing, you should notice the change.

Now to actually move a file into another directory:

example 4: moving a file

lab46:~$ mv lab1.text src
lab46:~$ 

A file listing will show that lab1.text is no longer present in the base of your home directory. If you look in your src/ subdirectory, you should find the file.

Removing Files

The rm command allows you to remove files.

Let's remove that file in your src/ directory:

example 5: removing a file

lab46:~/src$ rm lab1.text
rm: remove regular file `lab1.text'? y
lab46:~$ 

You will probably be prompted to ask if you are SURE you want to delete the file. This is to provide a layer of protection between what you say and what the system does. Answer y to go ahead and delete it.

NOTE: Once a file is deleted, it is gone for good. There is no undelete! (or un-rm!) So be careful when removing files.

Be sure to change back to the base of your home directory.

5. Now to move around and delete some files (be sure to show how you did it):
a.Move the lab1a.text file created in the above examples into your src/ directory, and name it lab1.file
b.If you did the above with two mv commands, how would you do it with one?
c.Remove lab1.file from your src/ directory.
d.DOS has a rename and a move utility, do you think UNIX also needs a rename utility? Explain.

There exists a mechanism in UNIX to create a link to a file. This is accomplished via the ln command. The common invocation will be of the form: ln [-s] realfile linkfile

NOTE: When discussing command syntax, items enclosed in the square brackets, [ and ], specify that the argument is an optional one. When using the optional argument, the brackets are omitted.

The -s argument to ln tells it to make a soft link. This is usually preferable over the hard link, which actually creates a file in the resulting directory of the same size as the linked file.

Create a symbolic link to your src/ directory:

lab46:~$ ln -s src lab1
lab46:~$ 

Do an ls -l, and take notice to the lines pertaining to src and lab1. lab1 points to src, so operations that can be done on lab1 will also effect src.

6. With the lab1 → src symbolic link in place, do the following:
a.Make a copy of /etc/motd and place it in your src/ directory. How did you do this?
b.Look in both src/ and lab1/, is the file present?
c.Remove your copied file from the lab1/ linked directory.
d.Is the file removed from lab1/ and src/ ? Do you have a better understanding of symbolic links?
lab46:~$ rm lab1
rm: remove symbolic link `lab1'? y
lab46:~$ 

You will notice that lab1 is now gone, but the original src/ remains.

Manual Pages

Last, but not least, are the manual pages. These are the online documentation of all the various commands and library functions on a system.

To view a command's manual page, simply type man followed by the command. This will bring up the standard section of the man page for that command, if it exists.

NOTE: There are several sections available in the online manual pages. Some commands can have several different manual pages, and will often by referenced by their section.

For example:

  • cp(1) would mean to reference the section 1 manual page for cp.
  • printf(3) would reference the section 3 manual page for printf.

To view a man page in a particular section, you would do the following: man [section] command

example 8: specifying manual section

lab46:~$ man 3 printf

In the man pages, the particular command is defined, its options given, as well as any other information the developer/packager wanted to include.

To navigate, use the up & down cursor keys to scroll one line at a time. The space bar skips ahead an entire screen.

To exit out of a manual page, hit q

Becoming familiar with and using the manual pages will be important throughout the entire course.

7. Time to consult the manual pages:
a.Look up the manual page for the du(1) utility. How did you do this?
b.What argument can be given to provide file sizes in “human readable” format?
c.Try it out on the command-line(both with and without the argument). Did it work?
d.What does your command-line look like?
8. Using your book or the manual pages, find the following:
a.What argument to cp can be given to make its operations verbose?
b.What argument to mv can be given to cause it to backup files it may be altering?

NOTE: There may be more than one correct argument to accomplish a given task.

Conclusions

This assignment has activities which you should tend to. While there is no formal submission required, it would be prudent to document some of the knowledge and experience gained on your Opus.

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

haas/spring2013/unix/labs/lab1.txt · Last modified: 2013/01/30 04:31 by 127.0.0.1