Part I Introduction to Linux 7
Chapter 1 Linux operating system Goal-of-the-Day Familiarisation with basic Linux commands and creation of data plots. 1.1 What is Linux? All astronomical data processing is done with the help of computers. Each of you have been assigned a private area on a disk, which will be your working environment. Interaction with astronomical data and software in your account is done by means of an operating system. The operating system that we will use during this lab course is called Linux. This open source operating system is extremely versatile and as such is the operating system most commonly used by the astronomical community. Each one of you already has a Linux account; you will have to login by specifying your Keele username and password. Within Windows we will use a virtual environment that simulates all the properties of Linux and allows us to use astronomical software Exercise 1.1 (a) Start or re-start your computer and login to your Keele student account; (b) In the Physics folder in your Desktop, start-up Scientific Linux VM. Select Scientific Linux from the menu and press Start. Follow any on-screen instructions as necessary; (c) Once the Virtual box has launched, login using your usual name and password; wait until various icons appear on the left-hand side of the working environment. The Linux operating system sets up the screen with a variety of tools. Commonly-used tasks are grouped together in the toolbar at the top of the screen. There is also a menu of tools which are activated by a click of the right mouse-button. However, the majority of the tasks we are going to perform require detailed instructions to be provided. These commands will be implemented via a designated window on the screen, called a shell or terminal, using the command line. The shell window displays in the corner an abbreviated combination of username and diskname, followed by a prompt, where commands are typed. It may be necessary to first activate the window by clicking with the left mouse-button anywhere within the window. The window features quite a few extras. The size and position of the window can be manipulated by using the left or right mouse-button on the rim of the window. Terminating the window can be achieved by typing the command exit, using the quit 9
10 CHAPTER 1. LINUX OPERATING SYSTEM option from the session menu on the window, or clicking the little button displaying a cross in the top-right corner of the window. However this last option does not cleanly terminate tasks and can cause problems. Exercise 1.1 (b) Start a shell/terminal window by right-clicking on the mouse and selecting Open in terminal or click on terminal icon on top left tab; (c) Move and manipulate the window to familiarise yourself with it; (d) Terminate the window. 1.2 Internet The development of the World Wide Web has had a massive impact on modern research including Astronomy and Astrophysics. It allows astronomers amongst other things to easily access information and vast data archives, to remotely control the operation of telescopes and to quickly and effectively react to interesting astronomical events like gamma-ray bursts or supernova explosions. WewillalsousetheWebtoaccess materialrequiredforthislab. Weuseaweb-browser called Firefox. The website dedicated to this lab is at www.astro.keele.ac.uk/astrolab where you find the lab scripts, downloadable data archive and the results from the lab sessions. File contents can be saved on your local disk by right-clicking on the mouse (beware of where exactly on the disk the data will be saved to). Exercise 1.2 (a) Proceed to the data archive on the lab webpage and download the file called gnuplot. 1.3 Files and directories Linux requires users to be mindful of the structure under which information is kept on the disk. Coherent data structures called files are generally stored under a directory structure on the disk. A file s location is given by its path. In reality, the directory structure of a disk is a virtual one, and the path does not directly correspond to a physical location on the disk: it is merely a (very useful) way of book keeping. The command pwd when typed in a shell displays the path to the current directory. The command ls lists the contents of the current directory, comprising a number of files and/or sub-directories. The contents of the sub-directory can be viewed by typing ls followed by the name of the sub-directory (separated by a blank space). Sub-directories can be created or removed (if empty) with the commands mkdir and rmdir, respectively, followed by the name of the sub-directory. To change location to the sub-directory, use the command cd followed by the name of the sub-directory. Type cd.. to change one directory level back. By default the outcome of a particular command will be displayed on screen. This output can however also be written into a file by adding > [filename] after the command. The command man followed by a command name can be used to get help on the syntax and options of a particular command. Be mindful that syntax is important in Linux, for instance blank spaces separate commands and the required arguments but are not recognised in a directory path. Previously typed commands can be recovered using the keyboard arrows or using the left and middle mouse buttons.
1.4. DATA TRANSFER AND DATA MANAGEMENT 11 Exercise 1.3 (a) Open a shell window; (b) What is the full path of the current directory? (c) What are the contents of the current directory? (d) Create a sub-directory called plots within our home directory/users/students/username (not in the Desktop directory). Files may be moved or copied from one directory into another(sub-)directory by means of the commands mv and cp respectively. The way these are used (syntax) is: mv (or cp) [filename] [destination directory]/[destination filename] the square brackets are not typed. If the destination filename is the same as the original filename, then it may be replaced by a dot (.). If the destination directory is the same, then only the filename needs to be specified. The command mv moves the file from the original directory to the destination directory by changing its path. Hence, mv may lead to the loss of data if a mistake is made. The alternative, cp copies the file to the destination directory, leaving the original file intact. The original file (or any file, for that matter) may be removed with the command rm [filename]. Exercise 1.3 (e) Copy the file gnuplot.gpi from its original directory to directory plots; (f) Change to directory plots. 1.4 Data transfer and data management Another way of transporting electronic data through the Internet is via File Transfer Protocol, by typing ftp within a shell window. To open a connection, type open, followed by the address of the FTP site. The address consists of four numbers or a combination of words (alias). Once the connection is established, you will need to log-in to the FTP site. Often, data archives provide the opportunity to use the FTP service anonymously, in which case you type anonymous when prompted for your username, and as a password you type your e-mail address. You may use commands like ls and cd. Before starting the transfer, type binary to ensure that the transfer mode is binary as most data are stored in binary format to reduce their size on the disk. A file may be retrieved with the command get, followed by the filename. Multiple files may be retrieved by the single command mget, in which case a wildcard (the symbol *) may replace part or all of the filename. For each file, you will be asked whether or not to transfer that particular file (answer with yes or no ). You may type prompt to toggle between being prompted and not being prompted for each file to be downloaded. To terminate the FTP session, type bye. Exercise 1.4 (a) In the directory plots, open an anonymous FTP connection to ftp.astro.keele.ac.uk; (b) Change to directory /pub/astrolab; (c) Retrieve all files which names start with gnu ; (d) Exit the FTP connection. When working with large amounts of data, it is not only good practice to maintain
12 CHAPTER 1. LINUX OPERATING SYSTEM a tidy and logical directory structure, but also to keep track of the size of the data compared to the maximum capacity of the disk. The command df lists the available disk units, together with their capacity and use in units of kilo-byte (1024 bytes). The command du -k * lists all files in the current and linked directories, together with their size in units of kilo-byte. The more elaborate ls -l displays a wealth of information about the files in the current directory, amongst which are the file name, date of creation, size in units of byte, owner, and a 10-digit code summarising certain privileges to handle the file. If running out of disk space, use ls -a to check amongst the hidden files (preceded by a leading dot) for any files that were meant to be temporary and which may be removed. Exercise 1.4 (e) What is the total size of the contents of the directory plots in kilo-byte? (f) How does this compare with the maximum capacity of the corresponding disk? (g) And with the free space remaining on the disk? 1.5 Editing files The contents of a file can be examined by typing the command more [filename]. If the contents exceed the height of the window, the return key and the spacebar can be used to scroll through them, whilst any other key will quit from the command. The command wc -l displays the number of lines in a file. A convenient way of examining the contents of a file, which at the same time allows you to modify these contents, is by means of a text editor. There are many different editor tools around, and many of these look like the editor that we will introduce here: nedit. You can start up the editor by typing nedit, which allows you to open an existing file or to create and write a new file, or by typing nedit [filename], which opens the specified file and creates it if it did not already exist. It is often convenient for tools that use separate display windows to be run in the background, to keep the original window available for other uses. This is done by trailing the command with the symbol & (e.g. type nedit &). Exercise 1.5 (a) Open the file gnuxample.dat using the text editor. What does the first line read? Delete this line; (b) Save the modified file as gnumod.dat, and exit from the editor; (c) Check that the file gnumod.dat has one line less than the file gnuxample.dat. 1.6 Graphs with gnuplot Amongst the many tools for plotting data in graphs, we here introduce gnuplot. It is command-line based, and can be entered by typing gnuplot. The commands may also be written within a file, which can then be invoked from within gnuplot by typing load [filename], or fed directly into gnuplot by typing gnuplot [filename]. This makes it much easier to repeat a graph, or to design a different but similar graph. Help can be obtained from within gnuplot by typing help. Also, the files that you have retrieved via FTP include the gnuplot manual in two different formats: as an html document, that can be viewed with Firefox, or as a PostScript (.ps) file. Within Unix, PostScript is a
1.6. GRAPHS WITH GNUPLOT 13 commonly used format for figures. These may be modified using any type of text editor, and can be viewed with a tool called Ghostview by typing the command gv followed by the file name. An almost identical but slightly more versatile version of PostScript is encapsulated PostScript (.eps). Exercise 1.6 (a) View the gnuplot.html manual with Firefox; (Hint: in this case, the address is a concatenation of the path and the filename.) (b) View the gnuplot.ps manual with Ghostview. The file gnuplot.gpi is an example of a set of commands that make a graph of the data in gnumod.dat. It has a lot of comment lines describing the various commands and a variety of plot options like plot symbols, labels, plot ranges etc. Close examination of this file, in combination with the gnuplot help, should suffice to get you started designing your own graphs. Exercise 1.6 (c) Make yourself familiar with gnuplot by studying the information described in the file gnuplot.gpi. This file can be edited and loaded, or alternatively similar commands can be typed from within gnuplot to produce plots. By default gnuplot will produce plots in a graphic window on the screen. Gnuplot can also be made to produce an (encapsulated) PostScript file if the output device is set accordingly with the gnuplot command set terminal postscript, and the output filename is then specified with the gnuplot command set output [filename].eps. This PostScript file can be sent to the printer with the Unix command lpr [filename].eps. If there is no (or a wrong) default name for the printer, one may have to accompany the printing command with the option -P[printer name]. Exercise 1.6 (d) Generate a personally customised graph, as a PostScript file; (e) Send the PostScript file with the graph to the printer. Toreturntodisplayingoutputonthescreen, typeset outputtoclosethefile, followed by set terminal X11. Feel free to experiment with gnuplot. IMPORTANT: Do not forget at the end of each astrolab session to log out from your account. On the System tab at the top left of the working environment select shut down. This shuts down the VirtualBox Linux environment and returns to the Windows workspace. Logout from Windows as usual.
14 CHAPTER 1. LINUX OPERATING SYSTEM