Computer introduction for FYTA11
The purpose of this computer introduction is to give you a brief introduction to GNU/Linux, Emacs, and Java programming. You will gain some experience with a powerful computing environment used by professionals and enthusiasts alike. We give this introduction because a large part of the hand-in exercises for this course (and other courses given by Theoretical Physics) require you to do some independent work on a computer.
Overview
Linux
The operating systems distributed under names like Debian, Ubuntu, OpenSUSE or Fedora are based on the Linux kernel, which has drivers for many types of hardware and sets the stage for running programs in a modern Unix-like environment. GNU is a free software project which encompasses many of the programs needed to make a useful system, such as file system tools, text editors and compilers.
The text editor Emacs
Emacs is a GNU text editor which is usually distributed with Linux. It is incredibly powerful, which also means it can be a bit confusing to new users. Like with other text editors, its primary function is to let you edit the contents of text files. These files may e.g. be source code for programs, in which case Emacs may be able to assist you through syntax highlighting.
Gnuplot
Despite its name, gnuplot is not GNU software. It is, however, a program for plotting, both on the screen and to files of various formats.
A very brief introduction to programming in Java.
You will learn to write and compile a basic Java program that outputs some text.
LaTeX
Learn to write reports using LaTeX.
Linux
For many years we have used a text called Introduktion till Unix
och X på EFD-datorerna
for this computer introduction, but apart
from the difficulty that that text is in Swedish, larger
and larger portions of it have gradually become less and less
relevant. I therefore prepared this new introduction to Linux
a few years ago, and have been updating it regularly.
I hope it still makes sense.
Note: I have marked literal strings like filenames and
commands like this
. Some computer terms that may need further
explanation look like this.
Background
Unix is a family of operating systems derived from a common ancestor in the early 1970s. Linux and GNU are projects aimed at doing everything the Unices of old can do, except better and as free / open source software. Linux is the kernel, which knows about computer hardware, file systems and how to run programs and let them interact. The GNU project provides many programs, most in the form of small commands. The Unix way of doing things is to have one command for each simple task, and string commands together to perform more complex tasks. We will soon see some examples of this.
File system
Unlike on many other operating systems, everything resides in a
single hierarchy of directories and files. For
instance, the file /home/carl/public_html/index.html
is my
homepage.
/
(the root)
/home
(home directories)
/home/carl
(my files)
/home/carl/public_html
(my web stuff)
/home/carl/public_html/index.html
(my homepage)
/home/carl/src
(my source code)
/bin
(some programs)
/dev
(devices: disks, partitions, ports, ...)
As you can see the root of this file system tree is
called /
, and /
is also used to separate directory
names in a path such as /usr/bin/man
. (For someone
with a background on other systems, it should perhaps be pointed out
that there is no direct connection between the path to a file and
where that file is stored physically. Different parts of the tree may
reside on different storage media.) By
convention, directories called bin
(such as /bin
,
/usr/bin
and /home/carl/bin
) contain executable
files or, in other words, programs. There are many similar conventions,
but there is no point in learning them all now.
The shell
I will assume that you have already logged in and are currently sitting
in front of a computer.
Open a terminal window — this might be called something like
shell
, terminal
or
console
depending on what system you’re using. With some luck
you’re now looking at a mostly empty window with a prompt that looks
something like this:
yourname@computer:~>
Good. What you’re seeing is a program — the shell — which wants you to make it do something. The window itself belongs to a program that emulates the computer terminals of old, in a nice demonstration of the principle of letting each program do one thing (managing the window versus being a shell). So what does a shell do?
A shell is, roughly speaking, a program that takes command names
and runs the corresponding commands. Every process (instance of
a running program), including the shell, has the notion of a
current directory; where in the directory tree the process is
at the moment. The current directory is usually shown in the
shell’s prompt — in this case it is ~
(tilde), which
is the shell’s way of saying your home directory
. To see where
that really is, try the pwd
command:
carl@min:~> pwd
/home/carl
You can list the contents of the current directory (or any
other directory!) with ls
. To see a bit more than
just the names of files and directories, try ls -l
(that's dash ell).
This long list format tells you whether things are files or
directories, who owns them, how big they are, and a few more
things.
To change the current directory, use the cd
command, e.g., cd mydir
. If you just type cd
with no arguments, you will move to your home directory, just
as if you typed cd ~
. The parent of a directory is
called ..
so the command cd ..
will move you
one level up in the directory tree. And you may use more
complicated paths; either absolute (beginning with /
)
as in cd /usr/bin
or relative as in cd ../../usr/local
.
One extremly useful feature of the shell is tab completion. If you start to type the name of a file or directory, press the tab key to have it automatically completed. If there is more than one possible match, press tab again to get a list of the matches (this is the normal behavior, but it can vary with your settings). Using the tab completion saves time and is a great way of ensuring that you don’t mistype filenames.
Another thing should be mentioned regarding the shell:
there is a subtle difference between commands that are
built into the shell, such as cd
, and those that
are actual programs, such as pwd
and ls
.
If cd
were a program, it would be started by the shell,
then this new process would change its current directory, but it would
then terminate and leave the shell just like it was before.
Because processes cannot manipulate each other,
commands like cd
are part of the shell itself.
A few commands
You have seen pwd
, cd
and ls
.
You can have more fun with these if you use mkdir
to create new directories, e.g., mkdir mydir
or
mkdir mydir/mysubdir
. You can also remove files and
directories with rm
. For instance, rm foo
removes the file named foo
. However, to remove the
entire directory mydir
(and all its contents) you need to use
rm -r mydir
. You can also use rmdir mydir
but
only if mydir
is already empty.
Be very careful with rm -r
, or you might end up removing
too much. We’ve all been there, and there is no easy way to recover
deleted files.
The command man
brings up manual pages, so if you
ever wonder how, say, the command ls
works, just type
man ls
and you will learn. There is also the newer
info
command which is slowly replacing man
and which in some cases gives you more information.
For those commands built into the shell, there is the
help
command. (Assuming that you’re using the
bash
shell. Other shells exist, but let’s ignore
them.) Of these three, man
is the one you
will use the most. Get used to looking up things in the manual —
it’s much easier than trying to memorize all commands and their
options.
>
, <
and |
Try typing echo Hello
. You see what echo
does.
Now try echo >myfile Hello
. Where did it go?
Does ls
give you a hint? Try less myfile
.
To quit less
, press q. (If you press something
else and get stuck in a strange state, try pressing Esc a few times and
type :q.)
So adding >filename
to a command makes the output
of the command go to that file. Similarly, adding
<filename
makes the program read input from the
specified file. You can try it with wc <myfile
to see how many lines, words and characters there are in
your file.
What really happens here is that the shell opens a file for
writing (>
) or reading (<
) and passes
it to the command’s process. Many commands will read from this
standard input only if not given any filenames as arguments.
Thus wc myfile
does the same thing as the example above,
albeit in a subtly different way.
More special is |
, the pipe sign, which makes the
output from one program the input to another. This allows
arbitrary commands to be connected in useful ways. An instructive but
rather silly example is echo Hello world | wc
.
Even more commands
You have already seen less
, which is used to view
the contents of text files. There is also an older program called
more
which does the same thing, but is less capable.
A simpler and somewhat different command is cat
, which
merely outputs the entire contents of a file at once. It can be useful
for viewing small files but also for merging or duplicating files,
as in this example: cat myfile myfile myfile >triplefile
.
The mv
command is used to rename a file or directory, e.g.,
mv oldname newname
. To copy a file use cp
, as in
cp fromfile tofile
. If you try to copy an entire directory you
will find that cp
does not want to do this without the -r
option: cp -r mydir mynewdir
. Note also that if the destination
(here: mynewdir
) is an existing directory, cp
creates a
copy in that directory (here it would be mynewdir/mydir
).
At this point a note about options is due. Most, if not all,
commands accept a multitude of options in a short (e.g., -r
) or
long (e.g., --recursive
) form. Most of these options affect
details of how the commands operate, and you can read all about the
options on the manual pages for the different commands.
The --help
option is accepted by most commands, and will
print a short help text that is often more useful than the full manual
page.
Traditionally the options had to be given before any
additional arguments, as in cp -r foo bar
, but the modern
GNU commands accept options anywhere in the command line, as in
cp foo bar -r
. Because options may appear anywhere, it could
be difficult to deal with files whose name start with a -
,
and the --
option therefore can be used to turn off option
parsing. So rm -- -z
is the way to remove a file called
-z
.
Searching in files
Create a file with a few lines of text in it. One way to do this is
to type cat >myfile
, and then enter a few lines of text,
followed by ctrl-d (which signifies end-of-file). Now
myfile
should contain the text you entered.
Try grep word myfile
. Replace word
with some word
that is in the text you entered. What you should see is that grep
is
a tool for searching for text in files. It can be used to search
for complicated patterns, as in the example grep 'h[ae]llo' myfile
(a regular expression that matches both hallo
and
hello
).
grep -i word myfile
performs a case-insensitive search in
myfile
, so both word
and WoRd
will match.
grep word *
searches for word
in all files
in the current directory. Similarly, grep -r word .
will
search for word
in the current directory (.
) and
recurse into all subdirectories and files.
grep
may default to case-insensitive searches, depending
on the current language setting. One way to force a case-sensitive search is this:
LANG=C grep word myfile
.
It is really the shell itself that expands *
into the names of all files. So echo *
will print the names
of all files in the current directory, ls -l ../*.txt
will
list all files in the parent directory whose names end in .txt
,
and so forth. That the shell performs this expansion (and more!)
also means that arguments with funny characters in them need to be
quoted in ""
or ''
. For example, echo '*'
prints a single *
.
Searching for files
There is a command called locate
, which can be used to locate
a file if you know its name. As a demonstration, locate resolv.conf
should find /etc/resolv.conf
and a few other files, assuming that
locate
has been installed (it might not be). The database which
locate
uses to find files quickly is updated regularly, but
very new files will not be found. It is then better to use the more powerful
find
. find . -name 'myfile*'
finds all files whose names
start with myfile
in the current directory.
Concluding remarks
You have probably already received more information than you can digest
in one go. If you’re going to remember something, remember that manual
pages are your friend. info coreutils
brings up a list of many
of the most common commands that you may need.
Exercises
- Create a directory called
myfiles
. - In
myfiles
, create two files calledfoo
andbar
. The filefoo
should contain the text stringThis is a test
andbar
should containThis is not a TEST
. Now, make sure you’re in the directorymyfiles
, so that a directory listing shows the two files. - Where in the directory tree are you now?
- Find all files that contain the word
test
. - Find all files that contain the word
test
regardless of whether it’s in lowercase or UPPERCASE. - How big are the files?
- What does the command
wc
do? - How many words do
foo
andbar
contain? - How much free disk space is there (hint:
man df
)? - Rename
bar
tobaz
and removefoo
. - Go up one level (that is, leave
myfiles
) and then removemyfiles
and its contents. - How much free disk space is there now?
Useful things to know
- Some commands that you should definitely know: cd, pwd, ls, mkdir, rm, less, mv, cp, man.
- Some commands that are very useful: grep, du, df, cat, echo, info.
- Some commands that are useful too: locate, find, sort, cut, head, tail.
- Some networking tools: ssh, scp, wget, unison
- Some file viewers: gv (for postscript, .ps / .eps), acroread / okular / kpdf / evince (for PDFs), less (for text files), xv / ee / display / eog (for images)
Emacs
Emacs is a text editor that some people like for its flexibility. It is exteremly powerful and customizable, and can in fact be used for just about any purpose, including editing text.
Start Emacs from the shell by typing
emacs
or
emacs files_to_edit
possibly followed by an &
(to have Emacs run in the background).
Emacs will open a window of its own, and you can go to work. Basic editing
such as removing and inserting text or moving the cursor around
should need no further instructions. Operations such as copying
blocks of text or searching and replacing can be accessed through
the menus, where keyboard shortcuts for those operations are also
listed. To some extent the mouse can be used for editing too. Note
that the way to copy and paste text is not the same as on
certain other systems, and in particular ctrl-c does not
copy text.
Saving, loading and quitting are other operations that can be accessed
through the menus, or you could learn their (somewhat cumbersome) keyboard
shortcuts: ctrl-x ctrl-s to save, ctrl-x ctrl-f to load,
and ctrl-x ctrl-c to quit. Another extremely useful shortcut
is esc esc esc (or ctrl-g) to escape without harm
if you have accidentally started to enter a command.
One very powerful aspect of emacs is its knowledge of the syntax of different programming languages. When you edit a Java file, you will notice that emacs does syntax highlighting, possibly making it easier to read the code. Emacs will also help you indent the code properly – press tab to have the current line indented according to emacs's rules.
Gnuplot
gnuplot
is a useful and popular program for generating plots. In this
section we will go over some basic gnuplot commands.
Feel free to test more advanced stuff on your own afterwards.
Type gnuplot
in a terminal window, and you should see a
copyright notice followed by a gnuplot prompt, like this one:
gnuplot>
Plot
At the prompt, type
plot x * 5 + 1
plot sin(x)
Try the following:
show xrange
show yrange
As you can see, xrange and yrange control the area to plot. These values can easily be changed. For instance,
set xrange [-2:6]
The plot is not automatically updated, so you’ll have to issue another plot command:
plot sin(x)
A range of *
tells gnuplot to try to figure out a
reasonable value for every plot command. To restore the
x range to this behavior, you can type
set xrange [*:*]
It’s also possible (and usually more convenient) to set the range directly in the plot command:
plot [-5:15][-2:1.5] sin(x)
There is a replot
command, which will let you add more
plots in an existing window:
replot cos(x)
Feel free to play around with different plot commands. You could for instance try these:
plot sin(x) * 5*sin(20*x), 5*sin(x), -5*sin(x)
plot [0:10] x**1.5 title "up", x**-1.5 title "down"
plot exp(-x*x)
3D Plots
It’s possible to plot in three dimensions, using the splot command:
splot [-10:10][-10:10][:] x*x + y*y
Plotting data from a file
It is of course also possible to plot data from a text file.
Use Emacs to create a file called data.dat
and enter
the following table into it.
1 1.21 2.22
2 1.10 2.50
3 1.05 2.75
4 1.30 2.66
5 1.26 2.88
To plot the data from this file, you can type
plot "data.dat" using 1:3 with linespoints
This will create a plot where x coordinates are taken from
column 1 and y coordinates from column 3. If you don’t specify
the columns (i.e., you remove
), the first columns
will be used.using 1:3
linespoints
means that the data points will be plotted
as points connected with lines. Some other
useful styles are lines
, points
, dots
and yerrorbars
. With yerrorbars
you need to have
a column with error values, and the using
clause should
specify three columns (e.g.
). To set the plotting
style for all subsequent using 1:3:5
plot
commands, use
set style data
and/or set style function
, e.g.:
set style data linespoints
In older versions of gnuplot, set style data
was instead called set data style
, and so on.
Title and labels
To give the diagram a title and set labels on the axes, you can type:
set title "Title goes here"
set xlabel "My x label"
set ylabel "My y label"
You can also give the functions/data titles:
plot sin(x) title "Sine", cos(x) title "Cosine"
Most commands and options can be shortened to just one
or a few letters, as in plot "data.dat" u 1:3 w lp ti "data"
.
Log scale
Once in a while you may need to plot something with a logarithmic scale on either axis. For instance, if you want a logarithmic y axis:
set logscale y
And to set the scale back to linear:
unset logscale y
Or, with older versions of gnuplot:
set nologscale y
If you don’t specify an axis (e.g. set logscale
), both
axes will be affected.
Exercise: Try to fit an exponential function (aebx,
written in gnuplot as a*exp(b*x)
) to the data below,
using the plot
command.
0.75 1.85
1.25 2.98
1.75 4.23
2.25 6.57
2.75 10.3
3.25 16.5
Printing
To print a plot or include it in a report or presentation, you may want to save it to a file. The other option is to take a screenshot, but the low screen resolution makes that a bad option for plots that are to be printed. It is better then to generate a file in postscript (ps/eps) or PDF. This is done by telling gnuplot to output in postscript or PDF, rather than to the screen. Commands needed for this are:
set terminal postscript monochrome
set output "yourfilename.ps"
You will then have to re-run the plot
command (or simply
replot
). The postscript plot will now be in the file
yourfilename.ps
. To print or view this file, open another terminal
window. There, you can start the postscript viewer gv:
gv yourfilename.ps
It’s possible to print from gv, or you can do it directly from
the shell using lpr yourfilename.ps
. Depending on what
computer you are using and what printer you want to use, a different
command (or a -P argument to lpr) may be needed.
After generating a postscript file you’ll probably want to go back to plotting to the screen:
set terminal X11
To see other available output formats in your
version of gnuplot, use help set term
.
The contents of an output file are not necessarily written
until the next set output
or set terminal
command.
Also note that some formats, such as PNG images, only work properly with a
single plot command per output file.
Getting help
gnuplot has a help
command which lists all of the available
commands and their options, and provides some useful examples.
It may be a bit cumbersome to use, but once you get used to it it’s
not that bad. To get a list of all help topics, type
help
If you want a list of the available commands:
help commands
If you want help with a specific command:
help plot
help set key
help set term post
There is also a meaty gnuplot manual available here.
Another exercise: Play around with gnuplot!
Java programming exercises
Here is a simple exercise to get you started on writing, compiling and running Java programs.
Hello World
Write a program that writes Hello World
on the screen.
Save the following lines into a file called HelloWorld.java
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello world!"); } }
Java programs are run by a program called the java virtual machine (JVM).
The JVM can’t use the source code directly; first you need to compile the
code with the Java compiler javac
. Do this by typing the
command
javac HelloWorld.java
The compiled program is now in a file called
HelloWorld.class
. You can now run the program
with the command
java HelloWorld
which starts the JVM and tells it to run the compiled program found in
HelloWorld.class
To further explore what you can do in Java, see the FYTA11 lecture notes.
LaTeX
LaTeX is a document preparation system that is useful when writing text with lots of equations. It is widely used by mathematicians and physicists for writing scientific text. This introduction will only show you how to get started. See the text Att skriva rapporter med LaTeX for many more the details.
The file Report-template.tex is a template LaTeX document that will serve as an example here. Download and save this file in a directory and open it with an editor, like emacs.
The first few lines of the document are headers specifying different
settings. After that there are a few lines where you can enter the title of the
document, etc. The document itself consists of the text between the tags
\begin{Document}
and \end{Document}
. Each section begins by
the line \section{SectionTitle}
.
Enter some text into this document, change title, author and date, and type something in one of the sections. Then compile the document by typing the command
pdflatex Report-template.tex
in the directory where you saved the template. This creates a PDF document
called Report-template.pdf
which you can view with a PDF viewer
such as kpdf
(like this: kpdf Report-template.pdf
).
You could also create a postscript file and then a PDF file through the following sequence of commands:
latex Report-template.tex
dvips Report-template.dvi
ps2pdf Report-template.ps
We recommend that you use this template when writing reports for the different exercises in this course. All reports should be handed in by sending a PDF file, also if you decide to write them using some other program (like MS Word or OpenOffice). The MiKTeX package is a Windows implementation of LaTeX that can be downloaded for free.
Links to manuals, tutorials and other stuff
gnuplot
Linux
- Linux Knowledge Base and Tutorial
- The Linux Documentation Project Homepage
- LowFat Linux
- The Linux Journal
- Linux Online
Java
LaTeX
A brief history of Linux and the GNU project.
The GNU project was started by Richard Stallman (RMS) just over 20 years ago, with the goal of providing a free alternative to the Unix operating system and its various clones. Back then, as now, it was a major problem that software companies made their customers highly dependent on the companies’ continued existence and good will. This good will would seldom stretch so far as to allow customers the means to fix problems, and intentional incompatibilities made switching vendors a practical impossibility. As a countermeasure, the GNU General Public License (GPL) was created. Starting in 1984, the GNU project grew to include many of the parts needed in an operating system (the text editor Emacs, many file system tools, compilers, standard libraries, and so on).
The last major piece missing from GNU was a kernel — the part that talks to the computer’s hardware and sets the stage for everything else. In the early 1990s, Linus Torvalds started working on a Unix-like kernel now known as Linux. He released it under the GPL and it soon became highly popular.
Nowadays, the combination of the Linux kernel and software from the GNU project is packaged and distributed by a number of companies and non-profit organizations, under names such as Debian GNU/Linux, Ubuntu Linux, Red Hat Linux, Gentoo Linux, Mandriva Linux, SuSE Linux, and many many more. Over the past decades, many of these distributions have matured to the point of being useful to a wide audience.