E-learning Unix® Written by Steven Dobbie Overview Unix is a wonderfully powerful operating system that can be used as a simple environment to run software through to a programming environment itself. It is a very stable system and so very reliable to use for research computing. The of this course is to enable you as a researcher to understand the Unix operating system and to know how to use its basic functions. Unix is often the environment of choice for researchers performaing analysis using programs that they have written in languages such as FORTRAN, C, etc., and it is the system through which you will access high performance computing (HPC). [ Footnote: UNIX used throughout this course is a registered trademark of The Open Group. ] Aim The aim of this e-learning course is for you to learn the basics of the Unix (and Linux) operating system to a basic level to enable you to work in the Unix environment. We have the view that this course will form the basis of other courses that you will use in your research, such as programming using FORTRAN, and so it is designed as a prerequisite for many programming courses.
Now let us copy a file into the new directory called research. We will make a copy of ‘data1backup’ and put it into the research directory. You can do this by using the ‘cp’ command as: cp data1backup research We used this command to create a new copy of a file. In the above command, the last name is a directory so this command will make a copy of ‘data1backup’ and put it into the ‘research’ directory. The name of the file in the research directory will also be called ‘data1backup’ and will have the same content. mv to move files around directories If you want to now move a file into the ‘research’ directory then you can do this by using the mv command. Type: mv data1 research This will move ‘data1’ from the current home directory and put it into the directory called research. No copy of ‘data1’ remains in the current directory for the mv command. [Info: When using the ‘cp’ or ‘mv’ commands, if the last word names a directory then the command will place the file in that directory. ‘cp’ leaves a copy in the original directory whereas ‘mv’ does not.
Method 2 continued You can do it also by specifying the full path. Say you home directory is ‘/nfs/see/username/’ then you would do it the following way: ‘cp /nfs/see/username/data1 /nfs/see/username/research/documents’ You can obtain the full path to the directory you are currently in by typing ‘pwd’ and hitting return. If you are in the home directory then type ‘pwd’ and it will return something like: prompt$ pwd /nfs/see/username The path varies depending on what computer you are on and what username you are and you are in your directories. You can type ‘pwd’ from anywhere. Task time [ Task 5 Go into the documents directory and type pwd. Is it what you expected? Are the longer?
File1 File2 File354 So the ‘*’ symbol means any number of other characters after the word FILE (or nothing more, as in the case of File). This is a very useful wildcard. You can also try using more than one ‘*’. For example, try ls *le* will give the same result of File File1 File2 File354 wildcards As it is matching ‘anything’, followed by le (which is part of the word File) and followed by anything (in the above case either nothing, 1, 2, or 354). We are now ready to analyse some files. Unix is modular and so you will be taught various Unix commands that allow you to do one task each. We will show you later on how to join Unix commands together but that will be after you learn some of the Unix commands.
The diff command When researching using numerical models, it can be handy to compare the output of one run to the output of another. Sometimes I will compare the output and in many cases I will compare the input files that start models running to see what I changed since the last time. There are two commands relating to diff (short for difference) that help you to look at differences in files. The diff command First, make a copy of ‘file1’ and call it ‘file4’. Now use emacs to change the ‘4.3’ in the second column/second row to a 5.3. Now you can use diff to highlight the difference that you just made. Type: diff file1 file4 The result will look like 2c2 < 0.2 4.3 --- > 0.2 5.3 See the next line for a description of the results.
Try for example using a cat command along with grep commands and a redirection: cat file7 | grep 3 | grep 6 > out.file7 As you progress with Unix, you will see that pipes are one of the most powerful commands that provides that essential joining up of the modular nature of Unix commands. If we return to the example of the input file in Figure 8 then what if one of the input files had spaces as in: ‘JJP = 800’ Then it be missed with the command we used before: grep ‘JJP=800’ * Working with We can’t search all possible combinations but you can do the grep command twice to get a likely unique answer in this case. So first run grep ‘JJP’ * and this will find all files that have ‘JJP’ in them. Then pipe this result to another grep command that searches for the number ‘800’, so