Unix Tutorial Haverford Astronomy 2014/2015 Overview of Haverford astronomy computing resources This tutorial is intended for use on computers running the Linux operating system, including those in the Observatory lab and the Stokes 012 astronomy lab. This tutorial is also relevant to using the terminal environment in the OSX operating system run by Macs. The Strawbridge Observatory lab contains 6 computers: 5 computers with the Ubuntu Linux operating system and one with a Mac operating system. The Stokes 012 student astronomy research lab has ~9 computers: 5 Ubuntu Linux computers and 4 Mac computers. To conduct your research, you will be using these machines. If you sit at a Linux machine, you will be doing your actual calculations and analysis on the processors in the machine you are sitting at. If you sit at a Mac, then you will use that Mac to log into a Linux machine for your calculations. All computers have web browsers that you can use for email, blogging, and research. If you are already comfortable working with a Linux operating system, you should still read through this entire tutorial to check your familiarity, and then you may skip the tutorial except the Changing your password, Creating directories, and Edit your.bashrc file sections. Logging In If you are sitting at a Linux machine (anything that is not a Mac), log in using the username and password given to you by your advisor. The Macs have a different login system. The password for the student account on the Macs in Stokes 012 is TBD. Ask one of us and/or click the Hint button. You can only log into the Mac in the Observatory if you are a Haverford student. Linux and the terminal working environment Begin by logging into the machine in front of you. Working From a Mac To open a Terminal from a Mac (whether your laptop or a desktop), you can find the application in the Finder. Depending on the operating system of your Mac, you may also need to manually open X11 or Xquartz.
If you logged into a Mac, then you need to open a secure connection to Frank or another Linux computer. To do this, go into a terminal window and type (using your username instead of mine): ssh -X yourname@frank.astro.haverford.edu If you d like, you can open multiple terminal windows/tabs and log into Frank multiple times with this method. If you ssh to a Linux machine from a Mac and have display issues, be sure that X11 or Xquartz is running as well as terminal. Make sure the line ForwardX11Timeout 596h is at the end of your /etc/ssh_config file. Working from a Linux machine You will use terminal windows to enter and execute commands to the operating system. To open up a Terminal window, click on the circle icon on the upper left of the screen that is called Dash Home. Then select the Terminal icon from the submenu or search for Terminal. You can use the menubar that opens up when running terminal to open more than one terminal window, either by using tabs or by opening separate windows. Please try this now. Changing your password Once you are happily logged into a Linux machine and have a terminal window going, change your password with kpasswd. File Directory Structure The directory architecture used by Unix is a hierarchical tree structure, much like what you may have used in a Windows or other computing environment. When you first log onto a Unix system, you land in your home directory. Instead of clicking on folders, you navigate through the directory structure by changing directories using the command cd at the prompt in your terminal window. The directory that you are currently sitting in at a prompt is your working directory. The top of the tree is the root directory, designated with a slash sign (/). Directory names build on the root directory, with a slash designating a new branch in the directory structure. For example, the full (absolute) path name for my home directory would be /homes/bwillman/. Directories can also have relative path names, which are interpreted as starting from the present working directory. If we were in the /homes/ directory, the relative path name to my home directory would be bwillman.
Changing Directories Learn where you are in the directory structure by typing pwd which is an abbreviation for print working directory. The cd command moves you around in the directory structure, and takes an argument that is the desired destination directory. The argument can be either a relative or an absolute pathname. Try switching into one of my directories to see what is inside. Type: cd /courses/bwillman/ To switch in to one of my directories. You could also try: cd /data03/bwillman/ Unix provides some useful shortcuts for navigating directories. A single dot,., always refers to the current directory. Double dots,.. refer to the parent directory. You can therefore move into the directory /courses/bwillman from /courses/ bwillman/astr206 by simply typing: cd.. Creating Directories Create a new directory in which you will begin your research by using the command mkdir. You should make this directory in /students/username/. You can call this directory anything you d like, but for simplicity avoid whitespaces and name it something that makes sense, like code or data. mkdir code Now list the contents of your current working directory to verify that your new directory has been created.
Directories can be deleted with the rmdir command. Try creating a removing a couple of directories. Listing the Contents of a Directory The command ls is used to list the contents of a directory. Under Unix, files, directories and even devices are treated as philosophically equal, so a directory could contain any one of these types of items. The ls command can be invoked with a variety of options, which modify its action. Under Unix, command options are preceded by a hyphen. A few of the more useful forms of ls include: ls ls l ls p ls a ls t lists all filenames, including directories generates a full listing, including dates, sizes, etc distinguishes between directories and regular files lists all files, including those that begin with a., which are otherwise hidden files. lists contents by date of their creation, latest first. Try each of these ls commands in order and notice the way they indicate your home directory s contents. You can combine different options when executing a Unix command, for example: ls apt Switch into one or more of my directories and list the contents. Then use what you ve learned above to explore the contents of this directory. Practice changing into and out of directories. You can always get back to your home directory by typing cd with no arguments. Copying Files The command cp is used to copy files: cp source destination For example I could make a second copy of the file foo.txt by typing cp foo.txt foo2.txt
(There is no file named foo.txt. Its just an example.) Use what you ve learned to copy a program from one of my directories into your home directory. Some Basic Linux Commands and Programs Viewing and editing the contents of a file The tasks cat file and more file print the contents of a simple text file to the terminal window. The cat task scrolls the entire thing past with no pauses. The more task more allows you to page through the file, using the spacebar to move one screenful and the Enter key to move down one line at a time. You are probably used to using Microsoft Word to edit and save text files in.doc format. We use different programs to edit text files in a Linux operating system. I use the program emacs. You open up a file to edit with emacs by typing: emacs myfile You can do this whether or not the file yet exists. Its better if you open a file using emacs by typing: emacs myfile & The & will run emacs in the background of your terminal, freeing up your terminal prompt for you to execute commands. If you have a problem with emacs display, try using emacs2 instead of emacs. Go to the directory that you created for this class. Use emacs to create a file in which you will keep notes. Write a couple of notes, and save and close the file. Edit your.bashrc File Your.bashrc file in your home directory contains commands that you want to be executed each time you open up a terminal window. I want you to add an alias to your.bashrc file that will help prevent you from accidentally deleting files that you want. Add the line: alias rm= rm -i Save and quit this file. To compile your edited.bashrc file, type source.bashrc
Now when you try to use rm to delete a file, you should be automatically asked whether you want to actually remove the file. Filenames and Wildcards Files in the current working directory can be referred to simply by their filename. As we ve already seen, a file can also be referred to using its full directory path /homes/bwillman/foo.txt where the last element, foo.txt, is the file name. When manipulating files, Unix allows you to use * to select a subset of the objects of interest. For example, to obtain a listing of all files that end in.pro, you would type ls *.pro You could also move all files that fit a particular pattern with mv../*.pro. Redirection One of the most useful aspects of Unix is the ability to redirect streams of information, input and output to and from files and processes. The symbols >, < and >> are used to accomplish this. For example, to send the listing of all the files in the current directory into a file called foo, one would type ls a > foo This has the feature that if a file called foo existed already, it would be overwritten with the new information. You could instead append the directory listing to a preexisting file called foo by using ls a >> foo Alternatively, you can pipe the results from one process into the input stream of another process. For example, he output of the ls command can be piped into the more command using the pipe symbol, which is a vertical bar: ls l more
Who is logged in? What are they running? You can determine who s logged onto a system by typing who. You can see what processes are actively running (and what resources they are consuming) by typing top. Type q to quit the top process. You can see the status of processes running (even if they aren t appearing with a top command) using ps, process status. The man page for ps gives many different options. This is a good way to diagnose whether you are running IDL or have a web browser running without you realizing it. For example: ps -aux grep idl Will tell you whether anyone is running idl on the machine. ps -aux lists all of the processes running, and grep searches a set of information for a specific string, in this case idl. To kill a process that you can not stop another way, do: kill -9 pid where pid is the process ID number returned by top or by ps -ux (or a variant thereof). Online help under Unix The on-line documentation for Unix can be accessed through man pages, short for manual. If you remember the relevant work, you can type man k keyword Which will produce a listing of all the man pages that pertain to that topic. You can then type man foo to get specific information on the topic foo. For more detailed information you can use the info command, e.g., info foo. Because foo isn t actually a Unix command, there isn t a manual page for foo. Instead try reading the manual page for some other command that you ve used today. A Multi-tasking and Multi-user operating system
The Linux and Mac operating systems are somewhat different from what you might be used to in your own computer s system. Multiple users can be logged in at once. Also, the computer can be running multiple tasks at the same time. A single CPU can only execute one instruction at a time, but it rapidly switches between tasks so they appear to be running simultaneously. You can determine who s logged onto a system by typing who. You can see what processes are running (and what resources they are consuming) by typing top. Type q to quit the top process. You should think of the Unix system as taking input from one or more files (which includes the keyboard, as it treats devices as a kind of file). This information can then be passed through one or more processes to produce an output file. Rather than having to store intermediate results in temporary files you can pipe the results from one process into the input stream of another process. This is one of the really powerful aspects of Unix, but it takes a while to embrace this perspective. An example might help. The output of the ls command can be piped into the more command using the pipe symbol, which is a vertical bar: ls l more mini Unix Survival Guide cd name changes directory to name (relative or absolute path) cd.. changes directory up one level cd ~ changes to home directory cd (with no argument) changes to home directory pwd print current working directory (i.e. where am I?) ls lists contents of current directory ps lists processes running on the machine mkdir name creates a directory called name rm i file deletes file, but asks first. rmdir directory deletes directory, but it must be empty first more file types the contents of file to the screen, one page at a time cp file1 file2 makes a copy of file1 called file2 > file sends output stream to file, overwriting if it exists >> file appends output stream to end of file task1 task2 pipes the output of task1 into task2 man topic produces help listing on topic. info topic produces help listing on topic