Intro to Using EECS Workstations

Version 2.0
1.3 How to Use Unix [Back to Homepage]


When you first glance at this tutorial, you may think, "Man, this thing is loooong!!" But don't get scared away. It's only this long because I make a conscious effort to explain everything step by step. See for yourself that it's pretty easy to follow along. I highly encourage you to browse through the tutorial, but if you're looking for a quick fix, you can check out the Summary below.

This tutorial first begins by describing what Unix is and how it works. If possible, I recommend that you log on to your account so that you can practice some commands as you read this tutorial (for information about how to log on to your account, see Section 1.1 and Section 1.2). After you get some background and hands-on experience in the walk through tutorial, this page then gives you a summary of the commands you learned. It closes with some advice on where to find more information and help.

Before we begin, let's talk about logging out. Usually, you log out when you're done with your work and you're leaving your computer. So, I would talk about this at the very end, except for the fact that you may choose to take a break from this tutorial and come back to it later. In that case, if you are working on a public computer, like those in the labs, be sure you logout. If you don't logout, other users will think that computer is still occupied. Or, if someone is feeling mean, that person may even mess with your files or settings. So, for the sake of others and yourself, you should logout when you leave the computer. To logout, simply enter the command "logout".

With that said, if you have to take a break from this tutorial, remember to come back later to finish it! It's got some good stuff. =) Okay, onto the good stuff...


Contents
These links all point to places on this page. Sections are numbered by topic, key terms are highlighted with a line -, and commands are highlighted with a double line =.

1.3 How to Use UNIX
    1.3.1.  Tutorial
            1.3.1.1 What is Unix?
                    - Operating System
                    - Shell, Command Prompt
                    - Graphical User Interface
            1.3.1.2 Path and Directories
                    - Directory
                    - Tree, Root Node, Children Nodes, Parent Nodes
                    = /      root directory
                    - path
                    = ~      home directory
                    - Absolute Path
                    - Relative Path
                    = pwd    print working directory
                    = cd     change directory
                    - argument
                    = .      current directory
                    = ..     parent directory
                    = mkdir  make directory
                    = ls     list
                    = rmdir  remove directory
                    - Option Flags
            1.3.1.3 Files
                    = touch  touch
                    = cp     copy
                    = mv     move
                    = rm     remove
                    = alias  alias
                    = chmod  change mode (file permissions)
            1.3.1.4 Running Programs
                    = &
                    = bg, fg
                    = Ctrl-c, Ctrl-z
                    = ps
                    = kill
    1.3.2.  Summary
    1.3.3.  More Information
            1.3.3.1 man
            1.3.3.2 apropos

1.3.1 Tutorial

1.3.1.1 What is Unix?

Unix is an operating system. An "operating system" is basically a program that manages the hardware of a computer and provides a platform for running application programs. You, the user, use operating systems like Unix to interact with the hardware and software components of a computer.

Unix features a command line interface, called a "shell". That just means the shell prompts you with a "command prompt" and you type in commands to tell the computer what to do.

Although there are several different types of shells, each with its own quirks, the basic commands described on this page should work similarly in all of them. The EECS Intructional accounts are set up to run the shell called csh, also known as the C-shell. Other popular shells are bash, tcsh, ksh, and zsh. Once you get familiar with Unix, you may be interested in customizing your shell (see How to Customize the Shell section). That's when what shell you use will matter more, because each has its own scripting language. But for now, these Unix commands should work the same for most shells.

The command prompt may look something like">", "%", or some other symbol. There may also be additional information before the prompt such as the server name, user name, or the line number of the command you are about to enter. Here are some examples of command prompts:

cory.EECS.Berkeley.EDU%

rshiao@ocf>

pentagon[12]

Using a shell is in contrast to using a "graphical user interface" (GUI) where you get around by clicking and dragging with a mouse. You may be familiar with the GUIs in Windows or Macintosh. As a side note, even in Unix there are some GUI programs that can do the equivalent of what we do with the shell here. But you're going to still have to use the shell for a lot of stuff in EECS, so learn this stuff now. =P Read on.

1.3.1.2 Path and Directories

Unix organizes your files using directories. A "directory" is like a folder where you put your files. You can put a bunch of files in a given directory. You can also put directories themselves into a directory.

These directories are organized using a "tree structure". In general, a tree stucture means there is a top level node called the "root node", which has "children nodes" underneath it. And those children nodes can have children themselves, and so on. Thus, each node has a directory containing it, called the "parent node" (all nodes, that is, except for the root node, since it's on top). The entire tree can have many levels of depth, but only one root.

Now, specifically for Unix, the "root directory" is a top level directory that contains all other directories and files on a given file system. In Unix, the children directories are called subdirectories. Thus, all directories have parent directories, except the root directory because it's on top.

All files and directories have to be located somewhere, and we describe where something is using its "path". We start at the root, and sequentially list the necessary subdirectories that lead us down the path to the object we want. We separate the names of each parent directory from its child directory with a slash "/".

The path for root itself is a single slash /. Makes sense if you think about it, since root has no parent directory, so it's just "/".

Let's do some examples. Let's say there is a directory called "bin" in the root directory. Its path, then, would be "/bin". That's one slash for root, then the name of the directory. Easy. Here's another path: "/users/rshiao/report.txt". This is the path for report.txt, which happens to be a text file. From the path, you can see that report.txt sits in a directory called "rshiao", which is in a directory called "users", which is in the root directory.

Note that paths are case-sensitive. That means upper-case letters and lower-case letters are treated as distinct. You could have a file called report.txt and Report.txt in the same directory (though that could be confusing, so is not recommended). Also, if you want to specify a file, you need to do so precisely; /users/rshiao/report.txt is different from /users/rshiao/Report.txt.

Every user account has a "home directory", which contains all the files and subdirectories of that user. When you log in, you start out in your own home directory. The path to your home directory could be long, something like: /home/cc/cs61a/fa03/class/cs61a-zz if your login is cs61a-zz. The basic format of a home directory's path is /blah/blah/blah/your-login.

* So for the rest of these examples, just make the appropriate modification to fit your own login and home directory path. *

Typing this all out every time is okay, but could get tedious. Here is the shortcut: use special variables that will have the same effect as if you typed the whole thing out. One such variable is the tilde "~". Another is the variable "$HOME". They both represent the path from root to your home directory, and the shell understands them.

Got all that? Earlier, we learned how to write paths in "absolute" terms. You list the path starting from root, and hit each subdirectory until you stop at the object you are referring to. For example, to access a file called report.txt in your home directory, you would say "/home/cc/cs61a/fa03/class/cs61a-zz/report.txt". Then, we learned a way the same path in "relative" terms, that is, relative to our home directory. In our example, we could access the file by saying "~/report.txt". The effect is the same.

Okay, you're off to a good start to learning about the background of Unix. Now let's try some hands-on stuff. For all exercises to follow, type in what you see after the prompt (in this case, we are on cory.eecs.berkeley.edu, so our prompt looks like "cory%"). After each command line, press Enter. Any line that doesn't begin with a prompt is program output to the screen. You can just look at those lines and see if your screen output matches accordingly. (Some path name specifics may be a little different since we're using different accounts, but that's okay). Alright, so here is your first task (Type pwd and press Enter):

cory% pwd

"pwd" is an example of a command. It stands for "print working directory". When you called this command, the shell printed to the terminal your working directory, or, in other words, where you doing stuff at the moment. Anytime you need to be reminded where you are, just use pwd. Since you just logged in, you should be in your home directory. So here, pwd should have printed out the full path of your home directory. Your screen should now look something like this:

cory% pwd
/home/cc/cs61a/fa03/class/cs61a-zz

Let's verify that is in fact your home directory. Before we do that, we need to learn another command: "cd", which stands for "change directory". This command is how you move around, going up to a parent directory, down to a subdirectory, or to any path. The format of this command is "cd" followed by the path of the directory you want to change to.

cory% cd ~

Remember, the tilde ~ is shorthand for your home directory. So you just changed directories into your home directory (even though that's where you should've started out). So let's verify that your path is still the same.

cory% pwd
/home/cc/cs61a/fa03/class/cs61a-zz

Sure enough, your current location is the same. (Or at least, it should be! If not, then, you somehow started out in a directory other than your home. In any case, your current location is ~, your home directory).

As a side note, you will see other variables as well. For example, ~cs61a stands for the home directory of the class. You can cd into that directory now and then pwd to see the actual path.

cory% cd ~cs61a
cory% pwd
/home/ff/cs61a/  <- or whatever this may be

This is where you will find files for your projects. Often you will be instructed to copy stuff from here into your own directory.

On another side note, you should know these commands are programs. You may not think something so small and simple as pwd can "count" as a program, but that's what it is. It's a program like emacs is a program.

And programs can take in arguments, which are values the program operates on. Like if you type emacs foo.txt, foo.txt is the argument to the program emacs, and you will open file foo.txt with emacs. In our above examples, pwd takes zero arguments; cd takes in one argument (the new path).

Now, let's learn about two other special symbols that will help us with relative paths. The first is a single dot ".", and it stands for the your current location (the same value as what pwd prints to the screen). The second is dot-dot "..", which stands for the current directory's parent.

As a side note, don't confuse the single dot "." when it's by itself versus when it's part of a file name. For instance, in the file name "report.txt", that dot just part of the file name. Only if a dot is by itself in a path does it mean "current directory". For example, the path "./report.txt" refers to a file called report.txt which is under . (our current directory).

Let's try some examples:

cory% cd ~
cory% cd ..
cory% pwd
/home/cc/cs61a/fa03/class/

Here, we start out back in our home directory. Then, we changed directories into our current parent directory. The effect is to go up one level in our directory tree. Then, we printed out the path. As you should expect, the path is the same as before execept we stop one level before where we used to be. By extension, you could keep doing the same, and you'd get all the way up to root.

cory% cd ..
cory% pwd
/home/cc/cs61a/fa03
cory% cd ..
cory% pwd
/home/cc/cs61a
cory% cd ..
cory% pwd
/home/cc
cory% cd ..
cory% pwd
/home
cory% cd ..
cory% pwd
/

Now, to get back to your home directory, you would just say "cd ~". But as an exercise, let's play with cd, going down one directory at a time. (Can you remember how you got up here to root? Now just go back down in reverse order.) If your path is different, just plug in the appropriate directory names. If you get lost, just cd ~ to get back to your home directory.

cory% cd home
cory% cd cc
cory% cd cs61a
cory% cd fa03
cory% cd class
cory% cd cs61a-zz
cory% pwd
/home/cc/cs61a/fa03/class/cs61a-zz

Let's go through another example to wrap up our learning about path. Let's say your in your home directory /home/cc/cs61a/fa03/class/cs61a-zz. We will talk about how to create directories and files in the next section, but for now, let's pretend you already have some stuff in your home directory. Let's say you have a file in here called "index.html". The following paths are all equivalent:

1) /home/cc/cs61a/fa03/class/cs61a-zz/index.html
2) ~/index.html
3) ./index.html
4) index.html
5) ../../../fa03/class/cs61a-zz/index.html

Let's look at each one. The first option should be straightforward. This is just the full path, starting from the root all the way down to the file we want.

The second option uses the tilde as a shorthand reference to our home directory. It still refers to the same file.

The third option works because a single dot represents the current directory. And right now, we happen to be in our home directory, so it works. If you were in a different directory, you would not be able to use the third option to refer to index.html. You'd have to use the first or second options.

The fourth option is just a convenenient shortening of the third one. If the path you want is to something in your current directory, you do not need to specify "current directory" with a dot. You can just say the name of the file you want. This method is probably the easiest to use in our example, since we are already in the home directory and the file is right here.

Lastly, the fifth option is purposefully convoluted and inefficient, but is useful for learning. Notice how there is a sequence of three dot-dots. That means we go up three levels. Since we start in our home directory, we go up first to the directory class, then to fa03, then to cs61a. So, if it was just three dot-dots, we'd be referring to the directory cs61a. But next, we see that we are going to directory fa03 (Yes, we're going back down the same path we came up). Then, down to class, then to cs61a-zz (our home directory), and finally, the file we wanted: index.html.

That's about it for now on paths. Feel free to take some time a review this section before going on. But don't worry if you're a little confused. Once we start using files in the next section, you will get a better feel for path.

Let's learn how to make directories. The command is mkdir, which stands for "make directory". The format is to feed in the name of the new directory after mkdir. First, make sure you're in your home directory. If neccessary, cd ~. Now, in your home directory, make a directory called "tutorial".

cory% mkdir tutorial

Great. Now, we've got our very own subdirectory. But how do we know it's there? We use the ls command, which lists the contents of your current directory. Go ahead and do that now. You should see your tutorial directory that you just made, along with any other stuff that already was in your home directory.

cory% ls
273books.txt  Desktop     Library  mail    tutorial
AdobeFnt.lst  LOST+FOUND  Mail     nsmail

Some of these are files, others are directories. How do we tell the difference? If we add the -f flag, we can see that directories are marked with an ending slash.

cory% ls -F
273books.txt  Desktop/     Library/  mail/    tutorial/
AdobeFnt.lst  LOST+FOUND/  Mail/     nsmail/

You should see the directory you just created. To remove a directory, we use the rmdir command. Let's try that now on the directory we just created.

cory% rmdir tutorial

If you list the contents of your current directory again, you will notice your tutorial directory is gone. rmdir only works on directories that are empty.

Okay. Now, make a new directory, and call it tutorial again. Change directories into it and list its contents. Try to do these things before looking at the answer below.

cory% mkdir tutorial
cory% cd tutorial
cory% ls
cory%
So far so good? Notice that when you listed contents of the new directory tutorial, nothing was listed. That's good, since as expected, there is nothing inside our new folder. So let's put stuff in it. We continue in the next section and learn about files.

1.3.1.3 Files

Right now, the tutorial directory you made is empty. We're going to make some dummy files, so we can play around with file operations. (These files are nothing meaningful, but allow us to learn the file manipulation commands that you can apply to real files just the same).

We use the touch command, which creates an empty file and names it using the name you give (if such a file doesn't exist already). So, touch foo.scm creates an empty file called foo.scm in the current directory. (touch is actually a command that is used to set a file's "Last Modified" timestamp properties. But if the file doesn't exist, touch creates a blank file - this is the feature of touch we're using right now).

cory% touch foo.scm
cory% ls
foo.scm

Cool, now we have our own folder and our own file. Even though the file is empty, let's pretend the file foo.scm has some Scheme code that you wrote for your project.

To copy files, we use the cp command, which stands for copy. It takes in two arguments: the first is the path of what you want to copy, the second is the path of where you want to copy to. In the second argument, if you specify a new file name, your copied file will have that new name. If you just specify the directory location, your copied file will have the same name as the original.

Now, you want to make a copy of your foo.scm and call the copy newfoo.scm. Then, you would be able to edit newfoo.scm while keeping a back up of your original file.

cory% cp foo.scm newfoo.scm
cory% ls
foo.scm    newfoo.scm

Notice the copy is now here in our directory, and it's called newfoo.scm. Now, make a new directory inside tutorial called project. Then, let's copy foo.scm into project, this time, keeping the same name.

cory% mkdir project
cory% cp foo.scm project

Notice how since project already exists as a directory, Unix knows that you want to make a copy of foo.scm and put it in project (as opposed to making a copy of foo.scm called project, and placing that file in the current directory). As a side note, no two things (any file, any directory) in the same directory can share the same name, since that would lead to ambiguous paths.

Let's copy one more time. This time, we're copying foo.scm into the project directory again, but let's rename it to bar.scm.

cory% cp foo.scm project/bar.scm

Pretty good, so far, right? Change directories into project and list the files there.

cory% cd project
cory% ls
bar.scm    foo.scm

Now, let's learn how to move files. The command is "mv", which stands for move. It takes two arguments like cp: the first is the path of what you want to move, the second is the path of where you want to move to.

The "mv" command is actually used for both moving files and renaming them. While those two things may seem completely different tasks, the command accomplishes both, while still staying consistent with itself, as we shall see shortly.

But first let's just think about moving files. Let's say you want to move newfoo.scm from your tutorial directory down one level into your project directory.

cory% mv ../newfoo.scm .
cory% ls
bar.scm    foo.scm    newfoo.scm

Here, the first argument to mv is ../foo.scm, which is relative path to newfoo.scm using dot-dot to refer to our current parent directory, tutorial. The second argument is a single dot, the symbol for current directory. Thus, mv moved that file to our current directory.

Let's move newfoo.scm back up. This time, let's use a different way to address the path.

cory% mv ~/tutorial/project/newfoo.scm ~/tutorial
cory% ls
bar.scm    foo.scm

The shorthand way would have been to say "mv newfoo.scm ..". But see how this method of specifying more of the implicit path also works. If you wanted, you could even expand the tilde and use the absolute path. The effect would be the same.

Okay, at this point, your folders should look like this:

cory% cd ~/tutorial
cory% ls
project/    foo.scm    newfoo.scm
cory% cd ~/tutorial/project
cory% ls
bar.scm     foo.scm

Now, let's talk about the other function of the mv command, and this is, renaming. Let's rename ~/tutorial/project/foo.scm to ~/tutorial/project/weird.scm. There are several ways to do that. Let's first try the longer way.

cory% mv ~/tutorial/project/foo.scm ~/tutorial/project/weird.scm

Yep, that's it. A very literal translation of the task given. But it works. And if you examine the structure, even though the effect is a "renaming" of files, the syntax is still consistent with the "mv" command. How? Well, you literally are moving the object of the path given by argument one to the path given by argument two. The fact that the final directory for both paths is the same, is how we create the effect of "renaming". The file ends up with a new name and stays in the same directory. Make sense?

By extension, you could specify a different directory and a new file name in the second argument. What do you think would happen then? Try it! Here we go:

cory% mv ~/tutorial/project/weird.scm ~/tutorial/superweird.scm

If you don't get too caught up with the different uses for mv, this should still make sense. Fundamentally, you are still moving the object at path one to the object at path two. Here, path two is in a different directory and has a different file name than the original. The result is weird.scm gets renamed to superweird.scm and gets placed in ~/tutorial.

At this point, if you followed all the prompts above, you should still be in your project directory. Notice how you can manipulate files and folders in other places of the file system, even though your working directory remains the same. All you had to do was spell out the whole path beginning with ~.

Now, given that you are in the current directory of project, there is a shorthand way of renaming files. Similar to ways described above, can you guess what it is? Let's say you want to rename ~/tutorial/project/bar.scm to ~/tutorial/project/soapbar.scm.

cory% mv bar.scm soapbar.scm

Yep, that's it. Since you're currently in the project directory, Unix understands that if you just say the file name, the path implicitly starts with your current directory's path.

To summarize what we learned about the "mv" command, just remember the general form is moving something you list in the first argument to the path listed in the second argument. For "real" moving, the first argument is the file and the second argument would be the new directory path, with no file name specified. For "renaming" something, the first argument is the file and the second is the new directory path (implicit or explicit) along with the new file name.

You now know how to see your current path with pwd, move around with cd, make directories with mkdir, make files with touch, copy files with cp, move files with mv, and rename files with mv. Your files should look like this:

cory% cd ~/tutorial/
cory% ls
foo.scm       project/    
newfoo.scm    superweird.scm
cory% cd ~/tutorial/project/
cory% ls
soapbar.scm

Next we learn how to copy directories. We use the same command that we use for copying files, except we use the "-r" flag, which stands for "recursive".

Recursive, as applied to directories, just means going into each directory and applying the same command. So in this case, we're copying all the files in a directory, then all the files in the subdirectories, and so on, until we copied all files and subdirectories in the given directory.

Let's copy the project directory and call it project_backup:

cory% cd ~/tutorial
cory% cp -r project project_backup

As you can see, we follow the same format as before. The first argument is the path of the object we want to copy. The second is the new path. In this case, we're copying project to the same directory, and calling it project_backup.

Now, let's learn how to delete stuff. We use the command rm, which stands for remove. Let's delete soapbar.scm, the one in project. The format for this command is rm, followed by the path to the object you want to remove.

cory% cd project
cory% rm soapbar.scm

If you are on the EECS machines, using default settings, then using rm will cause a message to appear. It will say something like, "Are you sure you want to remove FILENAME (y/n)?: ". Then, you either enter y if you are sure you want to remove it, or n to cancel the command.

So that's how you remove a file. How about a directory? Well, you can use rm, and add a flag -r, just like you did for cp above. Or, you can use rmdir which we learned about earlier. The effect is the same. Let's try using rmdir:

cory% cd ..
cory% rmdir project

Note that the project directory was empty. Normally, directories must be empty before you delete them.

But let's try what we learned about rm with the -r. This time, we add the -f flag too. This "forces" the deletion, despite the fact that there may be contents in a directory. We delete our other directory, project_backup, a directory that still has a file inside.

cory% rm -r -f project_backup

As a side note, when you use two flags together, you can put them together after the dash, like -rf.

Let's take a quick tangent to talk about aliases. We'll return to your regular programming shortly.

To alias something means when you type in something, the system replaces it with something else. Your account comes preset with certain alias settings. To see a list of such settings, just type in alias.

cory% alias
bye     exit
cp      (cp -i)
hi      history
j       jobs
l       (echo !*{*})
ll      ls -al
mv      (mv -i)
quota   quota -v
rm      (rm -i)

So, this means, if you type in bye, the shell understands it as exit. cp gets expanded to cp -i, and so on. This is where you set up convenient shorthand.

You can learn about all the flags of a given command and what they do by looking up the manual entry for that command (we will talk about how to do this in Section 1.3.3.1 below, so don't worry, we'll get there).

The "-i" stands for information, which means the system will prompt you if you are sure you want to complete the action. So, for rm -i, if you are trying to rm something, the shell will prompt you to be sure before it actually deletes it.

Next, let's try to add something to the table. Let's say we are tired of typing ls -F all the time when we want to see the trailing slashes at the end of directories in the listings. So let's alias ls to ls -F.

cory% alias ls "ls -F"

Notice the need for quotation marks. That's because the space in "ls -F" could confuse the system into thhinking it's two separate arguments. But we use quotations to group them together as one argument.

That's it. Try it out.

cory% mkdir abc
cory% ls
abc/  foo.scm  newfoo.scm  superweird.scm

However, any alias you change during a session only lasts as long as that session. Your changes are gone when you log out.

If you want your changes to be permanent, you need to modify your log in scripts. For information on how to do that, you can review Section 3.1 "How to Customize the Shell" when you get the chance.

Okay, that's it for our tangent on aliases. Now back to files.

To wrap up our section on Files, we talk about file permissions and the "chmod" command. File permissions dictate which users on the computer can access your files. There are three types of access, and there are three types of users.

The three types of access are "read", "write", or "execute". If you have Read permission, you see a file's contents but not necessarily save changes to disk. If you have Write permission, you can save changes to disk. If you have Execute permission, you can execute that object. The types of files that need to be executable are programs (they need to run), and directories (if you want to cd into them).

The three types of users are "user", "group", and "world". The first, "user", refers to you. Next, "group" refers to members in the same group as you. We won't go into the details of how to set groups, but you can look that up if you like. Lastly, "world" refers to anyone left over - anyone other than you or your groupmates.

If you type ls with the -l flag, you can see file permission information on files.

cory% cd ~/tutorial/
cory% ls -l
total 1
drwx------    2 rshiao   users         512 Sep  1 20:20 abc/
-rw-------    1 rshiao   users            0 Sep  1 19:46 foo.scm
-rw-------    1 rshiao   users            0 Sep  1 19:47 newfoo.scm
-rw-------    1 rshiao   users            0 Sep  1 19:48 superweird.scm

You should see something like that above. On the far left, there are ten columns: the first indicates whether the item is a directory (with a "d") or a file (with a "-"). There other nine columns store file permissions. The first three are "Read", "Write", and "eXecute" permissions for you. The next three are the same permissions for your group. The last three are for the world. If the column has a letter, then that permission is enabled. If it's a dash instead, then that permission is disabled.

Now to change permissions, we use the format chmod ### file.txt, where each # represents a octal number, derived from the following table:

R W X   Binary   Octal
0 0 0   000      0
0 0 1   001      1
0 1 0   010      2
0 1 1   011      3
1 0 0   100      4
1 0 1   101      5
1 1 0   110      6
1 1 1   111      7

This isn't as complicated as it may seem. See, on the far left, we list the three types of access. A permission can either be "enabled" (1) or "disabled" (0). And so, in each row, we list all the possibile combinations of accesses. In total, there are 8 such combinations. Then, for each row, we put together the bits and we get the binary representation in the middle section. Lastly, we translate the binary into octal.

So, for any RWX combo we want for a user type, we just look up the Octal code. And we do this three times, once for each user type.

Example: Let's make foo.scm readable and writable by you, just readable for your group, and not accessible by the world. For you, it's R=1, W=1, X=0, or 110 in binary, or 6 in octal. By the same method, it's a 100 in binary = 4 in octal for user. And it's a 000 in binary = 0 in octal for the world. Thus, the three numbers we got is 6, 4, and 0. So we write:

cory% chmod 640 foo.scm
cory% ls -l
total 1
drwx------    2 rshiao   users         512 Sep  1 20:20 abc/
-rw-r-----    1 rshiao   users            0 Sep  1 19:46 foo.scm
-rw-------    1 rshiao   users            0 Sep  1 19:47 newfoo.scm
-rw-------    1 rshiao   users            0 Sep  1 19:48 superweird.scm

Note that foo.scm has Read ability for the group, now. The other files remain the same.

Here's a way to remember the numbers. Note that Read-Only is a 4. And Write-Only is a 2. And Execute-Only is a 1. We can now combine abilities, and all we have to do is add their numbers. Thus, Read (4) and Write (2) is 6 (4 + 2). Read, Write, and Execute is 4 + 2 + 1 = 7.

Now, we learn about star *. Let's say we want to change permissions for all files in this directory. It'd be slow to do each file individually. So, we use the asterisk (*), called "star", to help us. It is a wildcard character and is used for pattern matching. Specifically, a star (*) matches any text string. For example, if you say:

cory% chmod 640 *

then you are applying the command to all files that match *. Everything matches a plain star *, so everything will be affected by the command. Verify this happened.

cory% ls -l
total 1
drw-r-----    2 rshiao   users         512 Sep  1 20:20 abc/
-rw-r-----    1 rshiao   users            0 Sep  1 19:46 foo.scm
-rw-r-----    1 rshiao   users            0 Sep  1 19:47 newfoo.scm
-rw-r-----    1 rshiao   users            0 Sep  1 19:48 superweird.scm

Indeed, all items have been affected when you use the star *.

It's use is not limited to chmod. We can use * anywhere it applies. Let's say you make another directory ~/tutorial2. Then to copy contents from tutorial to tutorial2, you'd say cp ~/tutorial/* ~/tutorial2.

In addition, you can use * in combination with other patterns. If you say *, all files will match. If you say "*.*", then only files that contain a period in them will match. If you say "*.scm", then only files ending in ".scm" will match.

Try this idea now. Make three text files, each named after the .scm files in this directory, except ending in .txt. Let's pretend these are README files for the scheme code. Then, set their permissions to be rw-rw-r--.

cory% touch foo.txt
cory% touch newfoo.txt
cory% touch superweird.txt
cory% chmod 664 *.txt
cory% ls -l
total 1
drw-r-----    2 rshiao   users         512 Sep  1 20:20 abc/
-rw-r-----    1 rshiao   users            0 Sep  1 19:46 foo.scm
-rw-rw-r--    1 rshiao   users            0 Sep  1 20:39 foo.txt
-rw-r-----    1 rshiao   users            0 Sep  1 19:47 newfoo.scm
-rw-rw-r--    1 rshiao   users            0 Sep  1 20:39 newfoo.txt
-rw-r-----    1 rshiao   users            0 Sep  1 19:48 superweird.scm
-rw-rw-r--    1 rshiao   users            0 Sep  1 20:39 superweird.txt

Notice how "*.txt" only matches the text files, and so only those files are -rw-rw-r--. The other files are unchanged.

Next, we learn about running application programs.

1.3.1.4 Running Programs

To run a program, simply type the name of the program. When you do so, Unix looks for the location of the program in its PATH variable. The PATH variable keeps a list of locations containing your programs. Once Unix finds a program with a matching name in one of the locations, it runs the program. If no match is found, Unix will let you know it can't find it. In that case, you'll have to change your PATH. (More on changing PATH in Section 3.1 Customizing the Shell).

For example, let's try running emacs now. Type emacs into the shell.

cory% emacs

If you are using a terminal, emacs should appear in the shell. If you are using the lab machines (or have X11 forwarding turned on if you're using SSH and Exceed on a PC), then emacs will appear in a separate window. If you have problems when using this command (you get some error about X connection), then you probably need to turn on X11 forwarding (review Section 1.2).

Emacs is a text editor with a ton of features. For a more thorough intro to it, see Section 2.1. For now, we won't be doing much with emacs.

Notice that when you started this program, you lost your prompt. That is because emacs took control of the terminal. If you wanted to start emacs, but still have your prompt available to you, you would need to append an ampersand (&) at the end of the line. Let's quit emacs and try that.

To quit emacs, you can type Control-x, then Control-c (if you have the default emacs setup). Or you can go under the menubar and choose "Exit Emacs".

Once you quit emacs, you get your prompt back. Now, let's do:

cory% emacs &
cory%

Notice now that emacs opens in another window (assuming you have X11 enabled). And you still have your prompt available so you can issue other commands and start other programs.

Sometimes, it's better to not use the ampersand. When you want the program to take over the whole terminal, you don't use the ampersand. For example, when you use the mail program, pine, you'd say:

cory% pine

Notice how pine takes over the whole terminal. Follow the on-screen prompts, and press the Q key to quit. If this is your first time starting pine, it may ask you if you want to send an anonymous email to the authors of pine. It's up to you.

Now, what if you've started a program and it gets stuck? There's no way to exit from it normally. One way to end it would be Control-c, if it's running in the terminal.

Or, you can try Control-z to suspend the program and get back the prompt. From there you can issue other commands.

The command fg and bg stand for foreground and background. Let's say you meant to start a program with the ampersand, but forgot it. Then, you're stuck with no prompt. However, you can type Control-z to suspend the program; then type bg to put that program in the "background". Now you've got your prompt back. Thus the net effect is that you get your program up and running and you get your prompt back. Furthermore, if you had suspended a program, then want to bring it back, you can type fg.

Next, here's a quirky thing. Let's say you're writing some C code, and you compile it, and you get the executable file (See Section 2.8 for Compiling details). Let's say you called that executable file snapple. To run it, sometimes typing snapple is enough. Other times, you need to specify that the program exists in the current directory. Hence: "./snapple" is what you'd type. I'm guessing this probably has to do with settings in the PATH. But yeah, just a reminder to try using "./" if the first way doesn't work.

Lastly, what if there's a program that's really messed up, and you want to force it exit? Type ps first, to see a list of the processes running. Note the PID number of the job you want to kill. Then, type kill ####, where #### is the PID number. For example, let's say emacs is hung, and I can't exit normally, so then, I kill the process from the shell like this:

cory% ps
   PID TTY      TIME CMD
 11334 pts/18   0:00 emacs
  7461 pts/18   0:00 csh
cory% kill 11334
[1]    Terminated           emacs
cory%

1.3.2 Summary

This table summarizes most of the commands discussed above. Yet, there are some extra tips here and there, too.

/
  • By itself, a single slash is the path for root.
  • Don't confuse this for when a slash is used to separate directories within a path.
~
  • By itself, a single tilde is the path for your home directory.
  • Don't confuse this meaning with when it is used in conjuction with other symbols. For example, ~cs61a used at the beginning of a path means it represents the home directory of the class.
  • Yet another meaning is when the tilde is part of a filename. In that case, the tilde doesn't expand to anything. A common use for the tilde in filenames is when Emacs creates automatic backup copies of file (the backup is named the same as the original, but has a tilde at the end). foo.scm~ would be the backup file for foo.scm
.
  • By itself, a single dot represents the current directory.
  • Of course, dots are used in filenames as well, like report.txt. Here, they are simply part of the filename.
..
  • Dot-dot almost always is used only to represent the current parent directory.
  • (Although technically, I guess it is possible to use it as part of a filename.)
*
  • This is the wildcard character used in pattern matching. It matches all files. Use it in conjunction with other symbols to match files exactly.
  • For example, r*.txt will match all files in the current directory that start with r and end in .txt.
program arg1 arg2 &
  • This is the general format for all programs/commands in Unix.
  • Here, program represents the program name. Some program name examples are pwd, ls, cd, emacs, pine, etc. Note that both simple operating system commands (like listing contents of a directory with ls) and complicated applications (like emacs) can be thought of as "programs".
  • Next, notice that programs can take in zero or more arguments. You would just tack them on after the program name, separated by spaces.
  • The ampersand (&) tells the shell to keep prompting you for more commands (for example, when you use emacs &). Omit the & when you want the program to monopolize the shell until the program finishes (for example, when you use pine).
  • All the following commands can be thought of as programs, with specific arguments.
pwd
  • stands for "print working directory"
  • prints to the terminal your current location
ls
  • stands for "list"
  • lists the files in your working directory.
  • Use "ls -F" to mark directories with a trailing slash.
  • Use "ls -l" to list files with additional details.
  • Use "ls -a" to list all the files (including hidden ones).
  • You can combine options, "ls -Fal".
cd new-directory
  • stands for "change directory"
  • changes current working directory to "new-directory"
  • "cd .." takes you up one level (to parent directory).
  • "cd ~" takes you to your home directory.
  • Tip: a shortcut for "cd ~" is plain "cd" with no arguments.
mkdir foo
  • stands for "make directory"
  • makes a new directory named "foo" in the current directory.
  • If you want to make a directory at another location, just specify path before foo.
cp foo bar
  • stands for "copy"
  • foo is the path to the file you want to copy.
  • bar is the destination. If bar is the name of an existing directory, then foo will be copied into bar. If bar does not exist, then a file called bar will be created, and it will be a copy of foo.
  • If bar is the name of a file that exists already, then you will be prompted to see if you want to overwrite it.
mv bar foo
  • stands for "move" (but is also used for renaming)
  • bar is the path to the file you want to move (or rename).
  • foo is the new path (which may include directories and/or a new file name).
  • To move: "mv ~/hw1/file.txt ~/hw2" moves the file from one directory (hw1) to another (hw2).
  • To rename: "mv ~/hw1/file.txt ~/hw1/junk.txt" keeps the file in the same directory, but renames it from file.txt to junk.txt.
  • To do both: "mv ~/hw1/file.txt ~/hw2/junk.txt" both moves and renames the file.
  • Note: You don't have to specify the full path starting from ~. You can omit the directories in the path, and just say the file name if the file is in your current directory.
rm foo
  • stands for "remove"
  • deletes the file specified by the path foo
  • Use "rm -r foo" to delete foo if foo is a directory. This also deletes "recursively" all subdirectories inside foo, too.
  • Use "rm -i foo" to get information that the file really is going to be deleted (so you can confirm the action).
  • Use "rm -f foo" to force the deletion (no prompts).
  • Note: See section above on aliasing if you have problems with the -f flag, because it may be because your account has rm aliased to "rm -i".
rmdir foo
  • stands for "remove directory"
  • removes the directory specified by the path foo.
  • The directory needs to be empty first, unless you use the -f flag.
chmod ### foo
  • stands for "change mode"
  • changes the file permissions for a file foo, where ### represents three digits. From left to right, the first digit specifies the permissions for the "user" (yourself), the second for the "group", and the third for the "world".
  • See table and explanation above.
  • Note: There are alternative ways to use chmod. You can use "man chmod" to read up on them. If for you, those ways are more intuitive, feel free to use them instead.
logout
  • logs you out of the computer when you are done working.
  • It's important to do this so that other people know the station is available for use, and so that no one messes with your account.
> < |
  • Okay, so these were not covered in the walkthrough above, but consider this a bonus then. =P
  • > redirects output of a program to a file (instead of printing it to the screen). The format is program-and-args > file.txt. The output from the left gets put into the file on the right. This is useful if you want to save your output for examination later.
  • < works the other way. It redirects contents of a file as input to a program. The format is program-and-args < file.txt, where file.txt contains your input (what you'd normally type in through the keyboard). The input from the right gets fed into the program on the left. This is useful if you want to run programs with the same input many times, so you can just save your input in a file beforehand.
  • | is for piping output of one program into another program. For example, you can run "cat file.txt" and pipe that output to the program "more", by saying: "cat file.txt | more". More on these programs below.


1.3.3 More Information

1.3.3.1 man

If you need to know more about how a certain program works, type:

cory% man program-name

man describes how to use a program, including option flags.

Below are some commands/programs that are kinda neat. They weren't important enough to get long, drawn out explanations like the commands above. But they're still pretty cool and useful. And hey, this is the perfect opportunity for you to try out your "man" skills, right? =P

Go ahead and man any of the commands below that seem interesting to you.

  • cat - concatenate files (put files together and display output to screen, or you can redirect output to a new file)
  • more - a program to display long files one screen or one line at a time.
  • less - a program to display long files, like more, but you can go backwards and you have to press "q" to quit.
  • quota - find out how much disk space you have. (Try: quota -v $USER And, if you're having quota problems, delete large files you don't need. If you can't find anything, check your Netscape cache.)
  • uptime - find out the load of the machine you're on. (If the machine has a high load, another machine may offer faster performace.)
  • who - find out who is currently logged on
  • finger - find out information about a given user
  • which - find the path to a program
  • find - find the location of a file
  • grep - search text files (Example from CSUA: grep "print-cards" *.scm will search for where you used the function print-cards in your .scm files. Use -r to search recursively through directories).
  • talk - online chat with other users
  • write - send an instant message to users
  • xterm - opens an additional terminal (on the X desktop), which is a window with a shell. Remember to use the ampersand: xterm &

1.3.3.2 apropos

If you need help with a certain subject, but don't know the appropriate command, type:

cory% apropos subject-name

apropos will give you a listing of commands related to a subject. Use man to find out more information on the command once you have located it using apropos.


Well, that should get you off to a good start. Let me know if you found this tutorial helpful, or what parts you found confusing or in error. That's all we have for this section, but check out the other sections on this website for more tips. Good luck with your classes. =)

©Copyright 2001, Richard Shiao. All rights reserved. Please send comments to: [email protected]
Hosted by www.Geocities.ws

1