Unix Tutorial

From Biowiki
Revision as of 22:43, 1 January 2017 by Move page script (talk | contribs) (Move page script moved page UnixTutorial to Unix Tutorial: Rename from TWiki to MediaWiki style)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

(Thank you to the course staff of Bioengineering 144 for this great UNIX tutorial!)

Lab Assignment 1 – Welcome to the world of UNIX

Goals for this lab – By the end of this lab assignment, you will:

  • Know how to login and out of the DECF machines in Etcheverry.
  • Feel comfortable with basic UNIX commands

Key to following this lab: Everywhere there is text in Courier font means that it is displayed on screen. Anything in bold Courier that follows the $ prompt, you are expected to type in.


0. General tips to use UNIX commands
  • Type man <command> to get command documentation.
  • Google online (open your browser).
  • Get a UNIX book on rarely used commands.
1. Login and Logout, Change your password
  • Get your login name and temporary password from the GSI
  • Choose a machine and login
    • The screen should ask you for your login and password, type them in carefully
    • Make sure CAPSLOCK is OFF, your password is case sensitive
    • While you are at it, make sure NUMLOCK is also off
  • You should now see the Red Hat Linux desktop. To get to the command line window that you will come to know and love, click the icon on the bottom left that looks like a blue screen. You should see:

    [yourname@machinename] $ or
    bash2.5 $

    This is the prompt, where you will type in the commands. We will use the $ symbol from now on.
  • Change your password to something you will remember:
    • At the prompt, login using the ssh command to the password server. Then type the command passwd to start the password changing program.

      $ ssh passwd.decf.berkeley.edu

      Type in your current password when requested. Once you see the $ prompt, type passwd

      $ passwd

      you should see:

      Old Password:

      Enter the password you just used to login and follow the instructions, you should end up back at the $ prompt. Type logout to exit the password server and get back to the original machine. It should display something like this:

      [be131-1234@kepler]$ logout
      Connection to passwd.decf.berkeley.edu closed.
      [be131-1234@machinename]$
  • To logout once you're done, click on the red hat icon in the lower left corner and select the Log Out option.
2. What do I do now? / Where am I? Who am I?
  • Type this command to remind you of your login name, then press enter

    $ who am i
  • Like other operating systems, UNIX operating systems organizes files into folders, called directories.

To see what directory you are in, type the command pwd

$ pwd

This is the complete path to your home directory, the place where you can organize all of your files. We’ll talk more about that later. Note that your prompt is customized to display the current working directory.

  • Try making a new directory using the command mkdir followed by the name of the new directory
    • (note #1 – don’t put spaces in directory and file names, use the underscore, _, if you want clarity)
    • (note #2 – UNIX “programs” you run, like mkdir, are called commands, and the following instructions [in this case the name of the directory you want to make] are called arguments )

      $ mkdir mynewdirectory
  • Now, enter that directory, using the “change directory” command cd

    $ cd mynewdirectory

    to go back to the previous directory, use the “..” argument, which refers to the directory one level up from your current location

    $ cd ..
  • Here are some good tips to learn right away:
    • Tab completion of names - Try changing back to the directory you created, by typing cd myne and then pressing the TAB key, followed by enter to execute the command. You should end up in the mynewdirectory. Check that you are there by executing pwd, and stay there for the next tip.
    • The ~ tilde AKA “twiddle” – The tilde has a special use in the UNIX directory system, it refers to the home directories of users. Instead of using cd with the .. argument, try using the argument ~/ which refers to your home directory. Check to see that you are in your home directory with pwd

      $ cd ~/
      $ pwd
    • Copy and paste, and more about referencing directoriescd back to your new directory, using the tab completion, and execute pwd

      $ cd mynewdirectory
      $ pwd
      /home5/be131-1234/mynewdirectory

      Now take your mouse and highlight all of the output of pwd using the left mouse button as you would normally. It is automatically copied into the “clipboard”! Now cd .. to get back to your home directory and then type cd and then hit the middle button on the mouse to paste what you just copied.

      $ cd ..
      $ cd [click the middle button now, it should look like the next line]
      $ cd /usr/home/be131-1234/mynewdirectory
    • Also good practice is what to do if you want to type a command similar to the one you just typed. At the prompt, hit the up arrow and see what that does. Try hitting the up arrow multiple times. If you want to get a new prompt if you made too many errors to fix, hit CTRL-c.
      Or use CTRL-u to delete the current line content and retype in new command.
      If you want to delete a word in a line, use CTRL-w. [NOTE - this shortcut behaves just like CTRL-u in the flavor of linux installed in the lab, and will delete the entire line]
      if you want to delete current cursor pointed letter, use CTRL-x. [NOTE: If this doesn't work, don't worry about it.]
      if you want to delete the previous letter of current cursor pointed letter, use CTRL-h, or use the Backspace key on the right of your keyboard.
  • To make a copy of a file named filename1 and copy it to a new file called filename2:

    $ cp directorypath-of-filename1/filename1 directorypath-of-filename2/filename2

    If filename2 already exists, you will get a warning:

    cp: overwrite ‘directorypath-of-filename2/filename2’?

    You will need to answer yes or no by typing y or n.
  • If you just want to rename your file from filename1 to filename2, instead of making a copy of this file, use the command:

    $ mv directorypath/filename1 directorypath/filename1
  • To delete a file named filename1, use:

    $ rm directorypath/filename1
  • To delete a directory called ‘currentdirectory’, which must be empty (no files or subdirectories) use:

    $ rmdir directorypath/currentdirectory
  • REVIEW –
    So now you also learned that you can refer to directories in many ways.
    Relative reference by name – cd mynewdirectory
    Relative reference shortcut, up one directory – cd .. or cd ../mynewdirectory
    Relative reference shortcut, back to home directory – cd ~/ or cd ~/mynewdirectory
    Absolute reference – cd /usr/home/be131-1234/mynewdirectory
    Note that this method of referencing files and directories is useful for arguments for commands other than cd
3. Let’s do something real with some bioinformatics data!
  • Start in your home directory. Type cd without arguments to get back there.
  • Make a new directory for the files we’re using in this lab, and enter that directory.
  • Copy a compressed archive file that contains multiple files to the lab1 directory. The cp command takes the source (file that you want to copy) as the first argument and the target (new location) as the 2nd argument.

    $ cd
    $ mkdir lab1
    $ cd lab1
    $ cp ~be131/lab1.tar.gz . (the . here is required and means 'this directory, where I am now')
  • Now unzip the archive file, and then extract the files from the archive

    $ gunzip lab1.tar.gz
    $ ls
    $ tar xvf lab1.tar
    $ ls
  • Use the ls command to list the files in your directory to make sure the extracting from the archive went according to plan. Try ls with arguments to get more info (type man ls to see what the arguments mean).

    $ ls
    $ ls –lta
  • First, let’s look at one of the files.

    $ cat Hamp.pfam

    Whoa, too much information! Try instead using more and using space bar to see one screen at a time, or the up and down arrow keys, or the pageup pagedown keys.

    $ more Hamp.pfam

    That text is a multiple sequence alignment, but looking at a bunch of text doesn’t help much. Type q to quit more.
  • Let’s use belvu an alignment viewer to see the alignment. This will open an “X Window” where you will be able to use the mouse.

    $ belvu Hamp.pfam

    Quit belvu right away (Tip - use right click to operate the menus)
  • You might have noticed that your terminal window was not operative while belvu was running. Now we are going to run belvu in the background by using the & which means you will still be to use that terminal window. This is good practice for any X windows program.

    $ belvu Hamp.pfam &
  • Play around with belvu (in future labs we’ll show you how to use it, so just explore for now) and note the amino acid sequences for each protein horizontally and the columns that match up, indicated in blue and gray.
  • When you’re done, quit belvu.
4. Editors and Cleaning up, pico and rm
  • To edit a file, use an editor (like a word processor). The easiest one to use for the terminal window is pico. To edit a file, type the editor name followed by the name of the file you want to change. To write new files, just type the editor name without an argument. Practice editing a file and creating a file.

    $ pico sample.fa or
    $ pico

    The bottom of the screen shows you what options you have. The “^” means “push the CTRL key and hit the letter shown”. Try this out, then quit.
  • Other recommended editors are emacs and vi. They have alot more features, but have more of a learning curve. Another simple editor is gedit. It works like Notepad in Windows. This is an X windows program so it won't work if you are logging in remotely unless you have X windows running (more on this below).
  • To remove files, use rm. To remove directories, rm the files first, then rmdir
    Warning - To remove all files type rm * (This can be dangerous!)

    $ rm sample.fa

    Tired of having to say yes to each file you want to delete? Type man followed by the name of the command you want to read the manual page for (in this case rm). It explains all the options, but be careful!

5. Working from Home – do this part of the lab from your home computer

If you would like to try to work from home on any assignments, this is a good thing to try. This isn’t mandatory and sometimes is difficult to get working. The labs here in Etcheverry are open 24hrs with cardkey access, so if you can’t get it to work, it’s ok. You can also ask the DECF staff if you have trouble at consult@newton.berkeley.edu. The GSIs will not be able to help you with these problems

  • Setup

    For Windows machines: Follow these instructions.

    For Mac OS X machines, if you don't have X11 installed, download it from here or use the installer disk.
  • Once you get a terminal window, similar to the command line window you use in the lab, connect to the lab machines using your account in place of the account name listed below. Try with and without the -X

    $ ssh –X <noautolink>be131-1234@kepler.berkeley.edu</noautolink>

    You should be prompted for your password and then you will find yourself in your home directory.
  • To check to see if you will be able to open the X windows programs, try this out, then close the window.

    $ xterm &

    If this opens a new terminal window, you are in business – close the window and proceed. Otherwise, contact the GSI and/or consult@netwon.berkeley.edu
  • If you are successfully logged in, you are currently on kepler. Your prompt should look something like

    [be131-1234@kepler] $

    Now you must pick a machine to work on because kepler is the gateway to all the lab machines and not to be used for work (“running jobs”). Go to the webpage http://www.decf.berkeley.edu/ganglia/ and click on 1111 Cluster, 1171 Cluster, or Reindeer Cluster. This page tells you how the individual lab machine’s load average (how busy the processor is crunching away at the complicated program someone is running.) It is critical to pick one that has a low load average (ones near the bottom of the load page, they are sorted from highest to lowest load). Zero load is best, 1 means it is full to capacity. Anything over 1 means that it is already overloaded. You can pick machines from any of the three clusters!
  • Once you have picked a machine (we’ll use diamond as an example), then log in to that machine using ssh.

    [be131-1234@kepler] $ ssh diamond

    You should then be asked for your password and eventually get a prompt from the lab machine. Check that the prompt looks like this to be sure:

    [be131-1234@diamond] $
  • Now it’s just like you were at the machine in the lab… though a lot slower for the X-Windows applications. Note for Macs with 1 button mouse - you can still use the middle and right click by option-click and command click, respectively.