Most things on computers are best first learnt by doing rather than reading about them, so here is a set of tasks for you to get up to speed with linux. Some are very explicit, others less so to make you look them up. I assume that you start in your 'home directory' ('directory' synonymous with 'folder'), the one you are in after you login, and which you can always return to by typing cd. Its a bit dull, but go through it once to get a smattering of the basics. Remember to hit 'enter' after typing a command to get it to do anything. To do any of the following, you need to start by opening some form of "terminal" into which you can type. Look at the menus reachable from the bottom-left button for these. I usually a "konsole".
- Create a directory, move into it and create a directory inside it (a subdirectory) and move into that as follows:
- Show where you are by typing pwd ("print working directory")
- List the contents of the "parent directory" (i.e. the one that contains the one you are in) by typing ls .. (look up what the '..' means). You should see the 'child' directory listed.
- Move up a directory in the hierarchy and list what is inside it with cd .. followed by ls. Again you should see the 'child' directory.
- Move back to your home directory with cd (you could also have typed cd ..)
- Move directly to the 'child' directory with cd parent/child
In all the above you are changing and listing directories by specifying their position relative to the directory that you are in at the moment, your "working directory". You can also specify a directory by its "absolute path". For instance:
- Change to the 'root' directory and once you are there, list it with cd / followed by ls. This directory is as high up as you can go.
- Then try cd /usr and cd /usr/bin. List the directories each time (lots of files).
- Rather than typing cd to get home, try cd /home/astro/your_username where you will have to substitute your username, phuxyz, or whatever. [Warning: your home directory may miss out the 'astro' part of the path; it depends how it has been set up.]
- Try looking around other directories using these commands. If you get stuck or lost, always remember cd to take you back to your home directory.
Creating and manipulating files
- Go to the directory you created 'parent', and start editing a file with the 'emacs' editor, e.g. emacs testfile&. You should immediately be able to start typing. This is more akin to MS 'Notepad' than 'Word': you only get what you type, with no hidden characters to tell a printer to display in boldface etc. This is exactly what you want for writing programs and scripts and files readable by programs, thus you will only ever need 'emacs' or an equivalent such as 'pico' or 'kwrite' if you prefer.
- Try typing in
This is a test sentence
and this is another test sentence
where you must hit the 'enter' key at the end of each line including the second blank line and the last.
- Now save the file to disk by typing Ctrl-X then Ctrl-S (or using the mouse to access the 'File' menu in the top-left corner). You can then quit 'emacs' in the same way or by typing Ctrl-X followed by Ctrl-C, or you can leave it running if you wish. ('Ctrl' here means press the 'control' key down while typing the letter following it.)
- List the directory you are in. You should see your new file. Type its contents to the terminal by typing cat testfile.
- Try listing the contents with cat Testfile and CAT testfile. Why don't these work?
- For longer files you should use the command less rather than cat
- Copy the file by typing cp testfile testfile_copy; list the directory to see the results.
- Rename ('move') the file copy using mv testfile_copy differentfile
- Delete ('remove') the file using rm differentfile. NB Be careful with 'rm'!
- Try removing the 'child' directory in the same way. It won't work; you need a 'recursive' removal rm -r child.
- Copy testfile to files called tst1, tsts2 and tstst (i.e. use the cp command three times). Now delete these with rm tst*. The tst* here means match anything starting with 'tst'. This can be used in other commands too to select multiple files, but you must take even more care when using it with rm, e.g. if you made a mistake typing and inserted an extra space to write rm tst * you would delete all the files in the directory, which could be a disaster.
Searching a file
- List all the lines of your file containing 'test' with grep test testfile
- List all the line of your file with at least two 'o's with grep 'o.*o' testfile. The '.' here means 'match any character' and the '*' means 'any number of times', so '.*' matches anything, while the 'o' at the start and end mean that it will match any line with two 'o's somewhere in it. This is an example of something called a 'regular expression' which can be very useful.
Changing a file on the command line, re-direction.
- Edit a file called 'storm' with the following:
Many years ago there was a big stoorm in which 1 million trees
were uprooted. The storm passed over the Centrl Europe
doing signifcant damage. Such big stoorms occur rarely
The 'stoorm' in the file above is deliberately mis-spelt when it comes after 'big'. The 'Centrl' is also deliberate.
- Type sed -e 's/big stoorm/big storm/' storm
- Save the results of the previous command by repeating it (use the up arrow for instance), but then adding >! newstorm. 'cat' the result. The > 're-directs' the output to the file
sed is a 'stream editior' and is a powerful way to modify files automatically, and also to modify the output from programs when used with what are called 'piped commands'.
Only the 'stoorm' fault was corrected above. Leaving 'Centrl' uncorrected. If you type
sed -e 's/big stoorm/big storm/' storm | sed -e 's/trl/tral/'
you can correct both faults. The output from the first invocation of 'sed' is piped to the input of the next. The crucial character here is the '|' between the two separate commands. This can be used to build up powerful sequences of commands. The examples here could trivially have been corrected with an editor, but there are many circumstances where this is not the case. Also note that it would also have been possible to achieve this with one application of sed followed by two -e 's/match/replace/' statements.
Scripts allow you to save lists of commands to a file and can save a great deal of time. Anything you can type on the command line you can typoe in a script, but scripts also allows you to loop and branch (possible but tedious from the command line). You create scripts simply by editing a new file, e.g. type emacs script& and enter the following lines:
echo "Hello world" >! testfile
echo "This is a test file" >> testfile
Save the file as before, and then make it 'executable' by typing chmod +x script, and then run it by typing ./script. You should see it run through the sequence of commands. Scripts are a major reason to use the command line at most times rather than GUI-based 'point-and-click' alternatives because you will find that you remember commands after a while.
For much more on shell scripts, see this C-shell cookbook. Be aware that there are other scripting languages, for instance there are other 'shells' within UNIX, and also fairly fully-fledged programming languages such as Perl and Python. You may want to consider looking into these if your work is likely to involve many repetitive manipulations of large numbers of files.
Scripts are not always the method of choice. For raw speed you may need to use a compileable programming language such as FORTRAN, C, C++ etc. This guide is not meant to teach you these, but to get you going here is how you can get a C program to compile and run.
Edit the following program by typing emacs testprog.c&:
Note that the '.c' on the end of the file name triggers 'emacs' to recognise that you are going to write a C-program which means that it helps you with language-sensitive editing. Equivalent endings are '.f' for FORTRAN77, '.cc' for C++ and '.java' for Java. Note also that unlike default windows behaviour, linux does not hide such endings which you will always see when you list files.
Compile and link the program with
gcc -o testprog testprog.c
and finally run it by typing ./testprog. There is more to it, but these are the basics of how one writes, compiles and runs programs. If you know C, stick a 'for' loop in to make the program write out the line 10 times. If you know FORTRAN, write yourself a program and use 'f77' in place of the 'gcc'.
For some more on programming, in particular how to write, compile and link a program which makes a plot, see how to make a plotting program.
Command options and on-line help
Most commands have options which modify their behaviour. An example up above was the -r option of rm. Try out the -l and -a options of ls. You can also combine options as in ls -al. Use the man ('manual') command to find out more, e.g. man ls. Try xman& to throw up a GUI-based help browser (a case where point-and-click is to be preferred). If you click on 'manual page' and then 'Sections', choosing (1) User commands, you can access the help on 'ls' and many other commands, although note that 'man' pages take a bit of getting used to and can be fairly useless for some of the lowest level commands such as 'cd' which are built into the 'shell' (the thing the provides the whole command line look-and-feel). You may at some point want to delve into the rather long man page on the shell by typing man csh, although the linux tutorial covers most of what this has to say.