Getting started with Linux in Örebro

By Thomas Padron-McCarthy (e-mail: thomas.padron-mccarthy@oru.se ). Last change: October 15, 2021.

This tutorial is intended to provide a simple introduction to how to use Linux, and especially on the computers in the computer rooms that the Department of Natural Sciences and Technology has in the Technical House at Örebro University . The introduction is primarily aimed at readers who have not previously used Linux or any other Unix system. However, it assumes that the reader has some computer experience, especially from Windows computers.

Linux

Linux is an operating system, like Windows 10 or Mac OS X, i.e. the basic program or basic system that the computer runs, and that the other programs run, as they say, "under". Linux is a variant of Unix, but developed from scratch with its own source code.

If you want to run Linux, you can do it in different ways, for example like this:

To start Linux and login

If you install Linux yourself, either directly on your computer or in a virtual computer in VirtualBox, you must choose a username and password that you then log in with. Keep in mind that Linux is case sensitive.

If you download a ready-made Linux virtual machine, and run it in VirtualBox, there is probably username and password information in the same place you downloaded the virtual machine from.

If you use the computers in the regular computer rooms at Örebro University, you log into Windows as usual, and then start the ThinLinc client program . You must enter the server thinlinc.oru.se , and the same username and password that you just logged into Windows with. You can click on Options and choose whether you want to run Linux in a window on the Windows desktop or in full screen mode.

If you want to use ThinLinc on your own computer, you must first download a ThinLinc client and install on your own computer from http://www.cendio.com/thinlinc/download . As you might guess from the name, the client is quite small, so it doesn't take up much space. Then connect in the same way as above.

If you use the computers in T002, only Linux is installed on them. These are local installations, so regular student accounts won't work. There is no server with shared storage of files, so if you log into two different machines in T002 with the same username, they are completely different accounts, with different files and (if someone changed it) different passwords. Use the same computer each time.

Similarities between Windows and Linux

Many people have a perception of Unix as difficult to use and only for experts. Cryptic commands are entered into a text interface, and produce equally incomprehensible responses from the computer. For example, listen to this unix course (an MP3 file).

It is true that Unix contains many tools that facilitate the expert, and that require reading some manuals before using them for the first time, but modern Unix systems, such as Linux, are quite easy to use even for the novice. A lot of work has been done to make tools and window systems that should be easy to get started with.

The window system is not part of the operating system in Linux, as it is in (newer) Windows, but if you want, you can run Linux in a plain text mode. But there are several different windowing systems for Linux, and almost every user runs one of them.

Here is an example of what the screen might look like in Linux. The image is taken from a freshly installed computer with Ubuntu 18.04. (Click on the picture to see it in large format.)

Full screen in Gnome

This window system is called Gnome , and is modern right now. It is quite similar to the desktop in Windows and on Mac. There are a number of icons on the desktop and in the bar on the left, which you can click to start programs or open folders and files. There is of course a "file browser" (bottom right) where you can open directories and look at files, like on Windows and Mac.

Here is a slightly different window system called Xfce , which is what is used when running Linux via ThinLinc from the machines in the regular computer labs in Örebro:

Full screen in Xfce

Man kan använda andra fönstersystem, och en del går att konfigurera väldigt mycket. Som ett exempel ser min egen skärm ut så här, med en fönsterhanterare som heter ctwm. De sexton rutorna till vänster representerar sexton olika skrivbord, som man kan växla mellan. Det är praktiskt när man arbetar med flera olika saker.

My own screen

Om man vill kan man dessutom köra Linux i ett rent textläge, utan något fönstersystem alls. Då får man, så att säga, ett enda stort textfönster att arbeta i. Det är vanligt att man gör så på servermaskiner, där man ganska sällan sitter vid datorn utan för det mesta styr den från en annan plats.

Några skillnader mellan Windows och Linux

One difference between Windows and Linux is that Linux has text interfaces that can actually be used. Windows users sometimes call the text interface the "DOS prompt" (even in Windows NT and its successors, which contain no DOS), and like to avoid it. It's understandable, considering how bad the text interfaces of the various Windows flavors are.

Unix has always had powerful text tools, so-called shells , where you can work efficiently with the help of commands and self-written program snippets. In this tutorial, we're only going to look at some of the simplest commands.

Man startar ett skal i ett "terminalfönster" eller "skalfönster", som är motsvarigheten till kommandofönstret ("DOS-prompten") i Windows. I Gnome kan man starta nya program, som Terminal, genom att klicka på prickarna nere till vänster.

To start the terminal program

Ett skalfönster startas, och där kan man ge kommandon, till exempel ls som helt enkelt listar vilka filer som finns i aktuell katalog:

The terminal

Prompten tpy-e@live:~$ betyder att det är användaren tpy-e som är inloggad på datorn live, och att aktuell katalog är den användarens hemkatalog (visas som ~). I Unix kan man enkelt logga in på andra datorer via nätverket, till exempel för att administrera en server, och då är det bra att ha datornamnet i prompten så man kommer ihåg vilken det är.

Man skapar nya kataloger med mkdir och byter aktuell katalog med cd:

The terminal again

Filer och kataloger i Unix

Here we use the words directory , file directory , folder and folder as synonyms. A directory is a collection of files, or other directories, and together the directories form a tree. A library , however, is something else.

In Windows, the files are divided into several different drives, C: , D: and so on. Under each such unit, all files are gathered in a tree.

I Unix finns bara ett enda träd av filer, som börjar i rotkatalogen. Den heter /. Diskar, usb-minnen, kataloger på servrar och allt annat stoppas in i trädet. Till exempel är det vanligt att användarnas hemkataloger ligger som underkataloger under katalogen home. I stället för till exempel M:\, som man kanske är van vid från Windows, heter hemkatalogen kanske /home/kajsa, om ens användarnamn råkar vara kajsa.

I Windows har alla filer normalt en ändelse, och vilken ändelse filen har avgör vad det är för typ av fil. Till exempel är filer som slutar på .exe körbara filer. I Unix behöver man inte ha filändelser, även om många enskilda verktyg tittar på ändelsen för att avgöra vad det är för fil.

Physical devices, such as disks, are also included in the file tree. For example, the computer's main hard drive is often named /dev/sda or /dev/nvme0n1 . One can open the hard drive as any file, and read its contents directly. (If you have read access to it, which you probably don't.)

Specifying file names

Every process (ie running program), even the shell you work in, has a current directory, which is the directory where you can open files directly.

Filnamn kan anges absolut, som till exempel /home/kajsa/foo, eller relativt till aktuell katalog. Att namnet börjar med ett snedstreck, /, betyder att filnamnet är absolut. Annars är det relativt till aktuell katalog. Om vi antar att aktuell katalog är /home, kan filen med det absoluta namnet /home/hejsan kallas hejsan. Om aktuell katalog fortfarande är /home, kan filen med det absoluta namnet /home/kajsa/foo kallas kajsa/foo. Om aktuell katalog är /home/hjalmar, kan filen kallas ../kajsa/foo. Katalogen .. anger katalogen ovanför aktuell katalog i (det uppochnervända) trädet. Katalogen . anger aktuell katalog, så foo och ./foo är samma fil.

Linux skiljer på stora och små bokstäver i filnamn.

De flesta skal förstår jokertecken ("wildcards") i filnamn. Som vanligt betyder ? ett godtyckligt tecken, och * står för noll, ett eller flera godtyckliga tecken. Det fungerar som i Windows, men med fler möjligheter och mer konsekvent.

Hakklamrar, som i [abcde], kan användas för att ange ett av flera tecken. Samma sak kan skrivas [a-e]. Fiskmåsklamrar kan användas för att ange alternativa delar av ett namn: {abc,de} betyder antingen abc eller de.

Tilde-tecknet, ~, som står för användarens hemkatalog. ~bengt står för användaren bengts hemkatalog.

Exempel: Om din hemkatalog heter /home/kajsa, så matchar mönstret ~/*/a*?.{c,cc} filerna /home/kajsa/koi/ax.c och /home/kajsa/C++/arbete.cc, men inte /home/kajsa/koi/labb1/ax.c eller /home/kajsa/koi/a.c.

Be aware that it is the shell, and not the operating system, that understands and interprets wildcards and tilde characters. This means that you can print a file in a terminal window by typing cat ~kajsa/foo*.{txt,docx} , but you cannot open the same file in your C program by typing fopen("~kajsa/foo *.{txt,docx}", "r") !

Absolute and relative filenames, even with . and .. , is handled by the operating system, see they can also be used inside programs.

The simplest commands

Much of the day-to-day work in Linux can be done via the window system and various graphical tools, but here we will concentrate on how to issue commands in a shell window.

Command Feature Example
etc List files ls
ls bilder
ls prog*.c
ls ~/prog*[1-4].{c,C,cpp,cc}
mv Byt namn på en fil mv gammal ny
mv prog1.c pro2.c katalogen
mv *.c c-programskatalogen
cp Kopiera filer cp program.c kopia.c
cp prog1.c prog2.c katalogen
cp *.c c-programskatalogen
cp -r Kopiera rekursivt cp -r en-katalog en-annan-katalog
rm Ta bort en eller flera filer rm foo
rm apa.txt foo blaj.c
rm /tmp/blaj.c
cd Byt aktuell katalog cd koi/labb2
cd /usr/local/bin
cd ..
mkdir Skapa en katalog mkdir min-nya-katalog
rmdir Ta bort en katalog rmdir koi
rmdir koi ../gammalt
cat Skriv ut en fil i skalfönstret cat foo
cat /etc/passwd
more Skriv ut en fil i skalfönstret en sida i taget more foo
more koi/*.c
exit Avsluta ett skal exit
man Titta på manualbladet för (exempelvis) ett kommando man rmdir
man -k time
alias Definiera nya kommandon alias ll='ls -l'
alias e='emacs'

Varning: Kommandot rm tar bort filer. Det flyttar dem inte till en papperskorg, utan det tar bort dem. "Bort" som i "borta för evigt". Man kan visserligen omdefiniera kommandon i Linux, och det är ganska vanligt att man omdefinierar just kommandot rm så att det flyttar filerna till en papperskorgskatalog. Men om ingen varit snäll och gjort det, så kan man inte få tillbaka en fil som man raderat.

Omdirigering av in- och utmatning

You can send the output of a program or command to a file with the greater-than sign > and to another program with the pipe sign | . A program or command that normally reads from standard input can read its input from a file if you use the less-than sign < . It works like in Windows, but more consistently.

Example:

ls > my files
ls | more
locate hobotest.c | xargs ls -alFtr

To compile, run, edit and debug programs

Just as you can work in Visual Studio on Windows, or Xcode on Mac, you can use an integrated programming environment in Linux. A common one is Eclipse . But you can also work step by step, giving separate text commands to compile your program, to run it, and so on.

Compilation

The C compiler is called gcc (often with cc as an alias) and the C++ compiler is called g++ .

Both take, among other things, these flags:

En stor hjälp är programmet make, som kan göra mycket av detta automatiskt. Om man skriver make foo, och det finns en källkodsfil som heter foo.c, inser make att foo.c ska kompileras för att skapa det körbara programmet foo. Om foo är nyare än foo.c, har det tydligen inte gjorts några ändringar sen senaste kompileringen, och då behöver filen inte kompileras på nytt.

Kommando Funktion Exempel
gcc Kompilera och länka C-program gcc foo.c -o foo
gcc -c foo.c
gcc -c -Wall foo.c
gcc -g foo.c fum.o fie.o -o foo
g++ Kompilera och länka C++-program g++ foo.cpp -o foo
g++ -c foo.cpp
g++ -c -Wall foo.cpp
g++ -g foo.cpp fum.o fie.o -o foo
husband Compile and link programs make foo

Driving

The programs are run by typing the program name in the shell window. For security reasons, many Unix systems are configured so that you have to type ./ before the program name if you want to run a program in the current directory. So ./foo instead of just foo , to show that you really mean to run a program in the current directory.

Here below, we change the directory with cd to the debug-exercise directory , where we then debug the metamatic program using the make command . Finally, we start the program with the command ./metamatic (so not just metamatic ). "Segmentation fault" means the program crashed. It's a debugging exercise!

The program metamatic in the terminal

Editing

There are several different editing programs ("editors") to choose from. An editor that is popular with many Unix users, but can be a bit awkward at first, is Emacs . Type the command
the emacs file.c &
to start editing the file the file.c . The & sign means that the command runs in the background, so you can issue new commands in the shell window. With programs that open their own windows, it is often practical to do so.

This is what an Emacs window looks like in a windowing system called Unity. Emacs is known for its many short commands, such as CTRL-B to move the cursor back one character, ESCAPE b to move the cursor back one word, and CTRL-X ( to start defining a macro, but as seen in the image there are also regular menus ( File , Edit and so on) that you can click on.

The program metamatic in Emacs

Another editor, which is more like Notepad, is gedit . Start it with the gedit command :

Edited

Debugging

If you have compiled your program with the -g flag , you can run it in the gdb debugger . It has a fully text-based interface.

Eclipse

You can also work in an integrated programming environment, such as Eclipse. It looks like this, and anyone who has worked with Visual Studio will probably see some similarities:

Eclipse

Some thoughts about windows

The Windows users, including Windows programmers, that I come into contact with tend to have most windows maximized, so they cover the entire screen. This means they can only look at one thing at a time. For example, if they want to look at prints in a command window, and edit the program that makes the prints, then they have to switch back and forth between the two windows, because the window with the program code in it covers the whole screen. (And when you click in that window to do something, it pops up and covers the command window.)

Jag själv, och andra Unix-människor jag känner, har i stället flera små fönster. Det kan vara ett fönster att editera i, ett fönster att köra programmet i, ett fönster för att läsa manualen i en webbläsare, och kanske ett kompileringsfönster. Varje fönster är bara så stort som det behöver vara, och eftersom alla får plats på skärmen behöver man sällan bläddra mellan fönstren.

Därför upplever jag det som frustrerande när jag ska kommunicera med någon Windows-person vid datorn, och hon ägnar den mesta tiden åt att bläddra bland sina (onödigt stora) fönster.

Jag tror att det kan finnas flera orsaker till den här vanan med maximerade fönster, bland annat att Windows-människor är vana vid integrerade system (som Visual Studio i stället för Emacs + xterm + gcc + gdb), vilket gör att de oftast inte har mer än ett enda fönster att hålla reda på.

Keep in mind that it can be practical to have several small windows next to each other on the screen. Especially when programming.

The program husband

The make program can be used to automate compilations. By writing a Makefile , you can control which steps are to be executed.

A good tip: Always automate as much as possible when programming. Manual steps, such as remembering to run bison or renaming a file, sooner or later you always forget at some point. And then you sit there for six hours at night looking for errors that don't exist.

We assume that we have a C program, with source code in the file foo.c , that can be compiled into the executable file foo . A simple Makefile for this might look like this:

foo: foo.c
	gcc -g -Wall foo.c -o foo
(Note that the gcc command line must start with a tab character, not a space!)

This means that we can create the file foo from the file foo.c by running the command gcc -g -Wall foo.c -o foo . The file foo also depends on the file foo.c , so if foo.c has changed since foo was created, the command must be run again.

If we now issue the command make , in the directory where the files foo.c and Makefile are located, the compilation will run:

tpy@live:~/koi$ make foo
gcc -g -Wall foo.c -o foo
tpy@live:~/koi$
If we write the same command once more directly, without having changed the file foo.c , then make is smart enough to realize that the program does not need to be recompiled:
tpy@live:~/koi$ make foo
make: `foo' is up to date.
tpy@live:~/koi$
Here is a slightly more advanced Makefile :
# Makefile for the debug exercise task
# Just type "make" to compile the metamatic program!

OFILES = metamatic.o ao abc.o abscissa.o absurdum.o ack.o

CPPFLAGS += -g

metamatic: $(OFILES)
	g++ -g $(OFILES) -o metamatic

clean:
	rm -f $(OFILES) metamatic
Driving example:
tpy@live:~/koi$ husband
g++ -g -c -o metamatic.o metamatic.cc
g++ -g -c -o ao a.cpp
g++ -g -c -o abc.o abc.cpp
g++ -g -c -o abscissa.o abscissa.cpp
g++ -g -c -o absurdum.o absurdum.cpp
g++ -g -c -o ack.o ack.cpp
g++ -g metamatic.o ao abc.o abscissa.o absurdum.o ack.o -o metamatic
tpy@live:~/koi$ make clean
rm -f metamatic.o ao abc.o abscissa.o absurdum.o ack.o metamatic
tpy@live:~/koi$

But what exactly is Linux?

It is usually said that Linux was created by Linus Torvalds in 1991, while he was studying at the University of Helsinki. What Linus Torvalds created is the operating system kernel itself, written in C and assembler. The kernel is the innermost part of an operating system, and it manages all the other programs and processes, and it communicates with the hardware. Later, many others contributed code to the kernel.

Men när man laddar ner och installerar "Linux" är det inte bara kärnan, utan många andra program, som skalet ("shell"), fönstersystemet (normalt fönstersystemet X), olika kompilatorer, och så vidare. Även systemprogram som "cp" (som kopierar filer) och "rm" (som tar bort filer) finns med, men ingår inte i kärnan. Många program kommer från GNU-projektet, och därför vill en del använda termen "GNU/Linux" hellre än bara "Linux".

Det finns många olika paketeringar, eller distributioner, av Linux. Linux-kärnan och många av de centrala systemprogrammen är gemensamma, men de kan variera en del bland annat i vilka tilläggsprogram som skickas med, och i hur användargränssnittet ser ut. Några vanliga distributioner heter Ubuntu, Debian och CentOS.

One should also mention Android, which contains the Linux kernel and many system programs, but with extensions adapted for mobile devices.

More information

Here is a movie from 1982!

The UNIX Operating System


Thomas Padron-McCarthy ( thomas.padron-mccarthy@oru.se )

Original text