Ntroductory Linux Tutorial
Ntroductory Linux Tutorial
On This Page:
Introduction
Changing from FVWM to Xfce
The Desktop Environment: Xfce
A Terminal or an xterm
The Command Prompt
Introduction to Editing
Navigating The File System
Typing Faster: Tab Completion and History
File Manipulation
Compilation
Redirecting Output to a File
Terminating Stuck Processes
Using man Pages
Learning More
Introduction
This tutorial will introduce you to Linux, and some of its tools. Linux is an operating system,
akin to Microsoft Windows, but much different in appearance and in how you use it. The
tools introduced here are programs available in Linux that you can use to support your
programming during 201.
A brief introduction to Xfce will be given. Xfce is a desktop environment, which essentially
provides a graphical user interface (GUI) to make using Linux easier. You may need to
switch from an older desktop environment, FVWM, to Xfce. After this, the bulk of the
tutorial will be spent covering commands typed in at the command prompt.
It's best to try the commands as you go through the tutorial, asking questions as you go, as
opposed to just reading it.
Note that Linux offers many more tools than described here, and tools in this tutorial often
have many more features than described here. The Learning More section at the end of the
page points to sources of further information. Links to these sources also appear throughout
the tutorial.
You will be using FVWM if your desktop looks similar to this when you first login:
(click to see a larger version)
1. You will be doing some typing in an xterm, which is a window likely with "xterm" in
its title, with a prompt to type at (the prompt ends in ~>). If an xterm is not available,
press Alt-F1, and click New Xterm with the left mouse button. Hover the mouse over
the xterm, and type the following:
test01@ug00:~>copyrcfiles
(only type in the text after the ~>, so only type in copyrcfiles)
3. Choose b, for backup, and press Enter for the .emacs, the .xinitrc and .Xresources
files. Press s to skip all other files. (The .emacs file is a configuration file for the
Emacs editor, which you can use to edit C programs, and which will be discussed
later on. It is okay if it does not prompt you for a .emacs):
4. /home/dsk07/misc/test01/.cshrc already exists!
5. Shall I o)verwrite, b)ackup, s)kip, a)bort or w)hackall?s
6. Skipping.
7. ...
8. /home/dsk07/misc/test01/.emacs already exists!
9. Shall I o)verwrite, b)ackup, s)kip, a)bort or w)hackall?b
10. ...
11. /home/dsk07/misc/test01/.xinitrc already exists!
12. Shall I o)verwrite, b)ackup, s)kip, a)bort or w)hackall?b
13. /home/dsk07/misc/test01/.Xresources already exists!
14. Shall I o)verwrite, b)ackup, s)kip, a)bort or w)hackall?b
15. /home/dsk07/misc/test01/.xsession already exists!
16. Shall I o)verwrite, b)ackup, s)kip, a)bort or w)hackall?s
17. Skipping.
Install complete.
Note this will copy the existing files, for example .xinitrc, to .xinitrc.bak,
overwriting .xinitrc.bak if it already exists.
Now, log out by pressing Alt-F1, choosing Exit/Restart FVWM, then Yes, Really Quit. Log
back in, and if all went well, you will be using Xfce, which will look like this:
Xfce allows you to do many things in Linux via menus and a mouse. It is the default window
manager, and the following gives you an idea how to use it. Unless specified, "click" means
to press the left mouse button once.
There many ways you can manipulate windows, many of which are similar to the
Windows operating system.
o To use a window, for example to type in it, click on the window. This also
brings the window to the front, making it completely visible.
Choose a window by clicking on the associated button in the task list at
the top of your screenl. (You can also use Alt-Tab.)
o To move a window, click the top horizontal bar of the window, and drag it
(i.e. hold down the left mouse button).
o Resize a window by clicking on one of its corners or sides and dragging.
o To maximize a window, click the square button in the window's upper-right.
To minimize a window, click the bar button to the left of the maximize button.
(What does the up-arrow button do? Give it a try).
o Close a window by clicking the X button in the upper-right.
You have access to four workspaces, which basically means four screens. Only one is
visible at a time. You are likely in the left-most workspace to start with. To move to
other workspaces, click one one of the four boxes in the upper-left of your screen (or
press Ctrl-Alt-leftarrow or Ctrl-Alt-rightarrow).
There's a button bar at the bottom of the screen from which you can start various
applications
o To access applications, like Adobe Reader, VMWare Player or Xfce help,
click on the Xfce Menu button, which is the left-most button, with a mouse's
silhouette over a blue X.
o To start the web browser, click on the globe button.
o When away from your workstation (ie. your lab computer) at any time, you'll
have to lock it (or logout if you are leaving). Do so by clicking the xlock
button (looks like a padlock).
To copy text, highlight it by clicking and dragging over the source text (or, you can do
that, and often right-click and choose Copy). Paste by going to the target area and
clicking the middle mouse button (or, you can often instead right-click and choose
Paste).
To exit, close all your applications, then click on the button bar's Quit button (It's the
common symbol for a power button, which is a circle with a vertical bar. It's beside
the clock at the right-side of the button bar.), or press Alt-F4. Then choose Log Out.
Experiment, and give things a try. Note that clicking the right mouse button often
brings up handy menus, and at times clicking different mouse buttons, or holding
down the Shift or Control keys and then clicking on the mouse buttons bring up other
menus. It varies depending on the window, or if you're not in a window.
For more information, you can see the local documentation at the Xfce Menu by
clicking Help, and also the Xfce web site
A Terminal or an xterm
To open a terminal, go to the Xfce Menu, choose System, then choose Terminal (for the
Terminal Emulator). This provides you with a command prompt. You can also click on the
black square button 2nd from the left on the button bar to open an xterm (or, you can choose
Terminal from the Xfce Menu).
The Terminal has a scrollbar, and an Edit menu for copying and pasting (Note you
can copy and paste into an xterm, too, and menus are available in an xterm when
holding the Ctrl key and pressing the mouse buttons.) So, in the opinion of this
tutorial's author, a Terminal is a bit more user friendly.
Terminal takes up significantly more memory than xterm, so may slow down the
responsiveness of your session.
Terminal has not been tested as much, and is not currently supported by the
Helpdesk ... if something isn't working in a Terminal, you may have to use an xterm.
test01@ug00:~>
You can also use an xterm. For more information on the two, and for how to open a Terminal
or xterm, see the Terminal or xterm section. A Terminal will be used for the rest of the
tutorial.
What you type will always come after the > in the prompt. Your command prompt will
have your login id instead of test01. Basically, you type in a command and press the Enter
key. Then something happens, like output being displayed to the screen. Then you can type
something again at the prompt. You will soon be able to use the command prompt to
efficiently make use of many programming tools.
The rest of the tutorial will cover some basic commands, but there are some general things
you should be aware of first:
The terms stdin, stdout and sterr will be used in Linux, and when programming in C.
They are referred to as standard input, standard output and standard error. Generally,
stdout is normal output from a program shown in a Terminal, stderr are errors from a
program shown in a Terminal, and stdin is input to a program that is typed in at a
Terminal. For more information about all three, type in man stdin at the command
prompt, and see the section on redirection later in this tutorial.
Commands are case sensitive, so typing in Man stdin, for example, will cause an
error. (Don't worry, though. Errors just give you feedback, and rarely hurt anything.
Experiment, and don't be afraid to cause errors ... that's how you learn!)
The term flags refers to command options that are normally preceded with a -. They
are often referred to simply as options. See the ls command for an example.
The Delete key does not work by default when editing commands. You can use the
Backspace key instead, and you can use the arrow keys.
It is likely not important now, but it will eventually be good to know that the bash
shell is used by default to customize how your command prompt and many other
things work.
Introduction to Editing
You will be editing many files in 201, such as files containing C source code. Type in the
following two commands at the command prompt
test01@ug00:~>cd ~
test01@ug00:~>emacs hello.c &
The cd command will be discussed later. The emacs command opens the emacs editor on the
hello.c file (note the file naming guideline). The & runs emacs in the background, allowing
you to keep using the command prompt for tasks such as compiling code. In the emacs
window, type the following code:
#include <stdio.h>
int main()
{
printf("Howdy!\n);
return 0;
}
You can also copy and paste the code. Once done, save the code by going to the File menu
and choosing Save (current buffer). Then exit emacs by going to the File menu and choosing
Exit Emacs.
Once you are finished this tutorial, you can see the Emacs Tutorial for an introduction to
Emacs.
pwd (print working directory): This prints the path of your working directory, which
is the directory you are currently in.
test01@ug00:~>pwd
/home/dsk07/misc/test01
You are one of many users on the system. Each user has their own home directory,
which is the same as the login id (test01 in this example). Use pwd to see which path
your home directory is in.
mkdir: To make a directory to organize your files, you can type a command similar
to the following:
test01@ug00:~>mkdir C201
This makes the C201 directory. It is best to ensure directory and file names start with
a letter, and contain only letters and numbers.
cd(change directory) :To change your working directory to the C201 directory you
just made, type:
test01@ug00:~>cd C201
Make a directory named "Assignment1" inside the C201 directory. Then cd into the
Assignment1 directory. To move back into the C201 directory, type
test01@ug00:~/C201/Assignment1>cd ..
This moves to the parent directory of your working directory. Can you figure out how
to get back to your home directory? Note that from the Assignment1 directory, you
could have instead typed
test01@ug00:~/C201/Assignment1>cd ../..
test01@ug00:~/C201/Assignment1>cd ~
Note, too, that . indicates your working directory. It is used, for example, when
running an executable in the current directory.
ls: List your files and directories by "cding" to your home directory and typing
test01@ug00:~>ls
You can get more information, like the size of a file, by typing ls -l , where -l (an
el, not a one) is a flag.
You can get more information about ls by typing man ls, and can go to this web site
that goes into more detail about ls.
test01@ug00:~>ls hello.c
Instead, you can use word completion. Assuming you have no other files or directories
starting with 'h', type ls h, then press the Tab key. The rest of hello.c will be "typed" for you.
Then press Enter. If multiple files start with 'h', press Tab twice for a list of those files. You
can do this for any file, directory or command.
Also, to easily repeat a recent command, use bash's history by pressing the up arrow
repeatedly. Give it a try to understand this further. For more information go to the gnu site's
pages on bash history.
File Manipulation
There are some basic commands to move (mv), copy (cp), rename (mv and cp) and remove
(rm) files:
mv: cd to your home directory then move hello.c to the C201 directory you made:
cp: cd into the C201 directory, make a directory named Assignment2, and copy
hello.c to the Assignment1 and Assignment2 directories.
test01@ug00:~/C201>cp hello.c Assignment1/
test01@ug00:~/C201>cp hello.c Assignment2/
test01@ug00:~/C201>cp hello.c ..
You can type man cp to find out more about cp's flags, such as the useful -r and -p.
Note that this is done by having a file instead of a directory at the end of the target
path.
test01@ug00:~/C201>rm hello.c
To remove a directory, type rm -r. Look at the rm man page to see what -f does, but
use it very carefully.
Compilation
You will compile hello.c, fix an error, compile it again, then run the resulting program (also
known as an executable). Ensure you are in the same directory as hello.c, and compile
hello.c:
This should make an executable called hello, indicated by hello following the -o flag. Yet an
error is encountered:
The error message normally indicates the approximate line number the error is
occurring on. The error is usually within 1 line, or bang on . emacs shows the line
number at the bottom of its window, and you can also go to a line by its number.
Error messages can be misleading at times. Trust that it is pointing you to the right
place, but if the message makes no sense, look for something that is out of the
ordinary.
Code incrementally, save your changes, and compile often. If you've only made a few
changes since the last successful compile, you will have a better idea where the error
is.
Only fix the first compilation error. Subseqent errors may change or disappear as a
result of fixing the first.
The first error from hello.c indicates that something around line 7 is causing a problem.
Looking at the code for a while reveals a forgotten closing double quote ("). Edit hello.c with
Emacs, fix the error, save hello.c, compile it again, and run the hello program (i.e. the hello
executable)
test01@ug00:~>./hello
For more information, you can type man gcc, and you can go the official gcc web site (for
the lab's version, which is 3.4.6 as of July 2007).
You can now edit helloOut using emacs. To overwrite an existing file, change the > to a >|.
For more information, see the page on redirection. Also, go to GNU's page on pipelines, a
concept related to redirection.
test01@ug00:~>sleep 120
To terminate sleep and get the command prompt back, press and hold down the Ctrl key, then
hit the C key.
test01@ug00:~>sleep 120&
you can terminate it by first finding out what number identifies the target process. Put your
login id after the -u flag:
test01@ug00:~>ps -utest01
test01@ug03:~>ps -utest01
PID TTY TIME CMD
7243 ? 00:00:00 Xsession
7289 ? 00:00:00 .xsession
7291 ? 00:00:00 bash
7292 ? 00:00:00 .xinitrc
7311 ? 00:00:00 fvwm2
7312 ? 00:00:00 FvwmButtons
7315 ? 00:00:00 xterm
7316 ? 00:00:00 FvwmPager
7317 ? 00:00:00 xclock
7319 pts/0 00:00:00 bash
7326 pts/0 00:00:00 xterm
7328 pts/1 00:00:00 bash
7336 pts/1 00:00:00 ssh
7344 pts/0 00:00:00 sleep
7345 pts/0 00:00:00 ps
The number at the left beside sleep, 7344 in the above example (almost for sure different for
you), is the number you use to terminate the sleep process:
test01@ug00:~>kill 7344
Use the man pages to find out more: man ps and man kill. Also, go to GNU's page on job
control, a related concept.
test01@ug00:~>man cp
If you know what a command does, try the -k flag with a description of the command,
such as "copy" for cp:
test01@ug00:~>man -k copy
Sometimes -K is needed to do a more thorough search. Note this is very slow, and
almost always too thorough. For information on cd, you could try
(Want to know what nice does? Try man nice. Note also that the quotes are needed.)
You can forcefully terminate the search if needed.
For C commands, always use 3 to specify the "C library functions" section:
test01@ug00:~>man 3 printf
You can even type man man to find more information on the man command.
When in a man page, the following "man page commands" are useful
o Ctrl-f (hold down the Ctrl key and press the f key) moves forward a page, Ctrl-
b backwards 1 page.
o Type /, then enter a word to search for and press Enter.
o Type h to get a list of more "man page commands". (Another program, less, is
actually used to display man pages.)
o Type q to quit.
You can use some web-based man pages, too
o Linux online man pages. These may not be as accurate as the system man
pages; always confirm things by using man command.
o Open BSD online man pages. Open BSD is an operating system similar to
Linux. Open BSD has better written man pages, but its commands can differ a
bit from Linux commands. Always use the man command to get the official
information.
Please ask your TA for help with using man pages.
Learning More
How do you learn more? There are many different ways:
If you have read this tutorial from "head to toe", followed all the examples, and have
asked your TA questions about anything you did not understand, you are off to an
excellent start.
Experiment. Give things a try.
Use man pages.
Use various online resources:
o Go to the U of A Libraries' Computing Science Database site, scroll down and
click Safari Tech Books Online. This site has many books about Linux and its
tools available online to U of A students. (This site seems to change without
notice, so post to the 201 newsgroup if you're having difficulties). If you are
off campus, you will likely have to enter your CCID and password.
o The GNU web site has a lot of information, specifically at its online
documentation page. The information at the GNU site is fairly detailed,
though.
Note that information on many commands like ls are available from the
coreutils documentation.
o For more information, you can see the local documentation at the Xfce Menu
by clicking Help, and also the Xfce web site
o The following Linux tutorials may be of use:
o http://www.ee.surrey.ac.uk/Teaching/Unix/
o http://steve-parker.org/sh/sh.shtml
o (bash scripting)
o http://www.cs.ualberta.ca/doc/UNIX/BASH_Guide/index.html
(advanced bash scripting)
o Use Google to find other online sources of information about specific Linux
topics.
Back to top
Contact Us
Careers
Site Map
Site Index
Feedback
Intranet
Computing Science Home
Faculty of Science