UNIX BASICS

 

UNIX is a kernel or a program that controls the resources of a computer and allocates them among its users (Kernighan & Pike 1984).

 

Working with the kernel, there are a number of different shells that can be used.  The shell is the interface between the kernel and the user.  It's the "command interpreter".  Some commonly used shells are: csh (C shell), tcsh (an updated C shell), and bash (the bourne again shell, an updated bourne shell).  Each shell interprets commands a bit differently and has its own way of defining different things.  To change to a particular shell from the xterm window, type the name of the shell you want.

 

To login to a UNIX system, you'll type your name and password.  You can then type commands within the xterm window.  UNIX file systems are organized in a specific way.  For instance, executable programs that every user may commonly use are typically located in the /bin/ directory (these are essentials).  Additional programs/executables that people use (like python) are found in /usr/bin/.  When you log in to the system you are automatically placed in your home directory, the space set aside for you to add your own files. 

 

To see the location of the directory you are currently in, type the command pwd.  On the astronomy system, you should see the path name of your home directory (/n/a/username).  To see the contents of your home directory, type the command ls.  This will list all files and directories in your home directory.  (Some of these files are hidden, and you'll need to type "ls *.*" to see them.)

 

Before I mentioned the directory /bin/.  Now, let's go see what files are listed there.  To change directories from the home directory to the /bin directory, type cd /bin.  Now type "ls" to list the contents of the directory.  Let's try an easy command in this directory, echo.  The echo command just repeats or echoes anything that you type.  Type "echo Hello".  You should see Hello repeated.

 

Ok, now lets work on some file manipulation.  Go back to your home directory.  You can simply type "cd".  Now lets make a file containing all the directory names in the /n/a/ directory (this will be the usernames in the department).  To get a long listing of the files in a directory, we use the -l option with ls.  There are many options for various commands.  To learn more about a specific command and their options, type man command name.  Try this with ls.  To quit out, press "q".  Now let's use the ls -l command to copy the file names and directories into a file in your home directory.  Type "ls -l .. > myfile".   A double period signifies one level up or one directory up.  The symbol > is a re-direction symbol that tells the shell to print the output of ls -l .. into a file called myfile.

 

Now for some basic commands that you may use in every UNIX session:

cat : cat filename lists the contents of the file

wc : wc filename number of lines, words, and characters in the given file

head : prints first ten lines of a file, or you can specify the number of lines with, i.e. head 20 file (for 20 lines)

tail : opposite of the head command, last 10 lines of a file

cp : cp myfile myfile2 produces a copy of the first file

rm : rm file deletes a file be careful with this, rm * could delete everything in your directory

mkdir : mkdir dir_name makes a new directory

mv : mv myfile2 dir_name/new_name moves a file from one place/name to another

grep : grep user_name myfile2 searches myfile2 for the specified string and prints all lines that contained that string

 

Let's talk about what the ls -l command gave us.  Looking at the results from the grep command on myfile2, I have:

drwxr-xr-x   52 lwinter    graduate      4096 Sep 16 22:34 lwinter

 

The first string of letters tells us about the permissions on the file in this case a directory (as indicated by the first letter, d).  There are three types of permissions you can have for a file: read, write, and executable.  There are also three types of groups that can have separate sets of permissions: the user, the group, and general users.  This shows that I have rwx privileges, but that all others only have read and execute privileges.  You can change the permissions on your files with the chmod command.

 

To test this, let's write a simple shell script.  We can use the commands we already learned.  First, open up an editor (a few are vi, pico, and emacs/xemacs).  Open xemacs by typing xemacs simple.sh &.  This opens the editor and creates a file called simple.sh. Now, let's write a command to count how many people are listed in the graduate group.

 

cd

ls l .. | grep graduate | wc -l

 

That's it.  The symbol | is called a pipe.  It pipes the output of one command into the next.  We need to make this script an executable.  First, check its permissions with an ls -l command.  To make it an executable for everyone, type chmod +x simple.sh.  Alternately, each permission has a number associated with it, execute =1, write = 2, read = 4.  If we want everyone to be able to read it but ourselves to be the only ones with all permissions (4+2+1), type chmod 744 simple.sh.  Now, run the script by typing ./simple.sh. 

 

This is a beginning to UNIX, there is a lot more you'll need to learn as you use it more and more.  To learn more, here is a good tutorial to teach you more of the basics:

http://www.ee.surrey.ac.uk/Teaching/Unix/