lab01: part igarryowen.csisdmz.ul.ie/~cs4023/resources/lab00.pdf · p t a l i n u x s e r i e s...

126
PTA Linux Series Copyright Professional Training Academy, CSIS, University of Limerick, 2006 © Lab01: Part I Introduction to Linux Professional Training Academy Linux Series

Upload: others

Post on 28-Jan-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Lab01: Part I

Introduction to Linux

Professional Training Academy Linux Series

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

What is Linux?• Linux is an operating system. Specifically, Linux is the kernel or the core of the system

• Originally developed by Linus Torvalds at the University of Helsinki, Finland

• Today, the Linux kernel is more commonly deployed with a set of tools

• Various distributions of Linux exist which package different sets of tools and environments for the various types of users. For example:

– Red Hat: for system administrators– Ubuntu, Fedora: for desktop users– Others include: SuSE, Debian, Knoppix, Slackware, Mandriva, Gentoo, etc.

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Linux History

Source: http://en.wikipedia.org/wiki/Linux

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Linux History

• By the early 1990s, PCs became powerful enough to start running UNIX

• Linus started to develop a freely available academic version of UNIX for PC machines (UNIX was developed at AT&T's Bell Laboratories in the early 1970s) • Minix (by Andrew Tanenbaum at the Vrije Universiteit, Amsterdam) existed at the time and was a smaller academic version of UNIX for teaching, but new features were not allowed to be added

• Consequently, Linus started his own project

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Linux Popularity

• In late 1991, first version of Linux was released

• Two years after beginning, Linus had 12000 users

•Today, Linux is well known for its stability and reliability

• Linux powers the majority of web servers in the world (Apache)

• The Linux counter project roughly estimates that there are 29 million Linux users (Source: http://counter.li.org/estimates.php)

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Some Common Terms

• X-Windows: (X11 or X) a toolkit and protocol for developing desktop window environments (modern desktops are built using X)

• KDE: a free desktop environment, originally developed by Matthias Ettrich in 1996

• Gnome: another free desktop environment, originally developed by Miguel de Icaza and Federico Mena in 1997 in response to KDE

• Directory: is another term for a folder

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Some Common Terms

• Login account: Every Linux user has their own login name and password. When you login, all your files will be stored in your personal home directory

• Shell: also called the terminal, the command line, the console, or the prompt. This is the program where you type in your commands. It looks like DOS

• Bash: is a type of shell and stands for the Bourne Again Shell

• Binary: an executable program. Similar to an .exe file on Windows

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Some Common Terms

• GUI: is the Graphical User Interface, which is your Desktop with all its windows and icons

• Kernel: is the main part of the operating system. It looks after the hardware, memory, and sharing of resources, etc.

Root: is the most powerful user on the system, and is similar to the Windows administrator. Linux users do not user their root account all the time, only when installing new software, hardware, or configuring • Script: a series of shell commands stored in a file which can be run like a program in order to automate a task

• Widget: a component belonging to the graphical environment, e.g. a scrollbar, button, etc.

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Linux Philosophy• Linux exists due to an open and accepting philosophy for learning and

exploring

• This philosophy encompasses new users as well as experienced users

• Every user and developer is welcome

• The more users the better, so your problems and experiences are valued

• Make use of the helpful online forums, mailing lists, irc channels

• Chances are that somebody else had a similar problem to you and it is

already documented

• A great resource is “The Linux Documentation Project” at

http://www.tldp.org

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Lab01: Part II

Transferable Skills & Common Tasks

Professional Training Academy Linux Series

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Demonstration: Logging In/Out

• You should always log in with your normal login account and not the root account e.g. ict

• Each account on a Linux computer has its own home directory

• The root account is equivalent to the administrator account on windows. This is a privileged account which can install hardware and software for all other accounts

• Using the root account when not installing hardware/software can cause security risks as this account has privileges over all other accounts

• Always remember to shutdown your Linux computer correctly. Turning off the power directly can damage your filesystem

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Demonstration: Web Browsers

• Firefox is one of the most popular browsers on all platforms (Mozilla)

• The Konqueror web browser is also available on the KDE desktop

• Other browsers include: Opera, Chromium

• On the command line, you can also use lynx, which is a text based web browser

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Demonstration: Email

• Thunderbird is an email client which you can download with Firefox (Mozilla)

• Kmail is also available for the KDE desktop

• Other popular email clients include: Evolution

• If you are looking for text based email clients on the command line, try: pine or mutt

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Demonstration: Microsoft Office Documents

• To create, open, and edit Microsoft Office documents you can use LibreOffice

• LibreOffice supports Word, Excel, PowerPoint, and access files

• When creating documents in Linux which you wish to open in Windows, make sure that you save as .doc, .ppt. etc.

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Demonstration: File Manager

• The equivalent of My Computer on Windows is:

– Nautilus for the Gnome desktop

– Konqueror for the KDE desktop

• You can access your file manager on Gnome from the Applications menu as follows:

Applications->System->File Browser

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Demonstration: Opening Common Files

• .doc, .ppt – Microsoft Office files

libreoffice filemane.doc

• .pdf – Adobe Acrobat files

okular filename.pdf

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Demonstration: Opening Files from USB, CD, and DVD

• Plug and play is now default in most Linux distributions

• When you insert a USB key, CD, or DVD, you can read, copy, and write files to/from these devices by accessing the following location in your filesystem: /media/*e.g. /media/cdrom, /media/dvd

• If your Linux does not automatically mount the device to /media/ you can use the mount command, e.g.

mount /dev/sda1 /media/usbkey

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Demonstration: Searching for Files

• To search for file names on the command line, use:

find ./ -name filename –print

• To search for words within files use:

grep “search pattern” filename

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Demonstration: Printing Files

• If using OpenOffice and your printer setting are installing you can use:

File->Print

• On the command use, you can use the lpr (line printer) command, for example:

lpr –P printername filename.pdf

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Demonstration: File Sharing

• To share a drive or folder in Linux in the same manner as Windows, you can use the samba program

• For example, to map a network drive:

smbmount /network/drive /local/mountpoint –o username=remoteusername

• A graphical program called LinNeighborhood exists for reading and writing to network drives; this is not installed on the lab machines, however

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Demonstration: Desktop Power

• Virtual Desktops allow you to open multiple windows and start a new desktop for a different task without closing down windows

• Creating an icon for an application on the desktop requires that you know the location of the command you wish to run

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Demonstration: Graphics

• Graphics Package: The Gimp

• Diagram Package: xfig, inkscape– e.g. create postscript/pdf graphics

– Inkscape allows you to create svg (scalable vector graphics)

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Demonstration: Running Programs

• Java Programs:

javac filename (compile)

java filename (run)

• Perl Programs:

perl filename.pl (run)

• C Programs

gcc filename –o filename.c (compile)

./filename.c (run)

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Demonstration: IDEs

• Eclipse and Netbeans are among the most popular

www.eclipse.org

www.netbeans.org

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Exercises

1) Log onto your Linux system with the course username and password; how far do you get? :-)

2) Use Firefox to find one pdf file in google and save it to your home directory

3) Use OpenOffice to create a word document and save it in your home directory. Spend some time examining the other types of documents which you can create with OpenOffice, e.g. Impress will create PowerPoint presentations

4) From your desktop, use your system file manager to create a directory called project1 with a file called report.txt inside it. Edit report.txt and add a couple of lines of text. Copy report.txt to your home directory

5) Using the pdf that you downloaded in ex. 2, use the pdf reader (okular) to open it.

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Exercises

6) On the command line, use the grep command to search your report.txt for the word “the” or any other word which is in your file

1) On the command line, use the find command from your home directory to find the location of the report.txt file

2) Use your virtual desktops to open a web browser on desktop 1 and openoffice on desktop 2

3) Use the gimp to create a .jpg graphic

4) Use xfig to create graphic; export it as a .pdf file

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Lab01: Part III

Shell Commands

Professional Training Academy Linux Series

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Why Use the Shell?

• The command prompt on Windows is also called the shell, the terminal, or the console on Linux

• It is also worth noting that you can use Linux without using the shell

• However, all tasks can be completed using the shell

Q. Why should you use consider using the

command prompt?

A. You can perform your tasks quicker, access

more powerful programs with more options,

and have an ability to automate your tasks

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Some Notes

• Everything in Linux is treated as a file by the operating system, e.g. processes, executables, text files, etc.

• If you are logged in as a normal user, you should see a dollar sign in your shell $ or a right arrow >

• If you are logging in as the super user or the root user, you should see a hash sign in your shell #

• On Windows you may have been used to using the term folder, on Linux folders are more commonly called directories

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Some Notes

• To make your life easier in the shell, you should avoid creating files and directories with spaces in them

• Instead Linux users usually use a dash, underscore, or capitalise first letters of words. For example:

project_results, project-results,

projectResults

• Finally, always keep your files and directories organised. In particular, give your files meaningful names

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Commands: Navigation

• pwd: print working directory– To find out where you are in the filesystem, type: pwd

• ls: list files– e.g. to list the files in a directory type:

ls

– To list extra information about files (long listing), type:

ls -l

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Commands: Navigation

• cd: change directory– e.g. to change into the projects directory, type:

cd projects

– If you ever get lost in the filesystem, you can type cd on its own:

cd

– To go back a level you can use:

cd ..

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Commands: Navigation

– Note: The location of the current directory is represented by a single dot .

– Note: The location of the previous directory is represented by 2 dots ..

– Note: The location of your home directory is represented by a tilde sign ~ and this allows you to use the shortcut cd ~/myprojects instead of cd /home/ict/myprojects

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Commands: Viewing

• cat – concatenate file contents all at once– e.g. to output the contents of poetry.txt type:

cat poetry.txt

• more – view file contents by screen

• less – view file contents by screen

• For example, if your poetry file is very large, you can use more or less to output the contents one screen-full at a time

• They are more or less the same! Except that less allows you to go backwards

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Commands: Creation

• touch: create an empty file

– e.g. to create an empty file named project_results, type:

touch project_results

• mkdir: make a directory

– e.g. to create an empty directory named course_work, type:

mkdir course_work

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Commands: Editing

• vi – text editor basics:• The vi editor is available on all Linux and Unix systems. For

this reason it is worth learning• You must choose a mode to enter, for example, insert mode

to insert new text• To exit a mode use the Esc key, e.g. Press i to enter insert

mode– Usage: vi filename

– Search forward: /searchword– Search backward: ?searchword– Undo: u– Save and exit: :wq– Exit without saving: :q

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Commands: Editing

• emacs – text editor basics:

• While emacs is available on the majority of Linux and Unix machines, it is not guaranteed like vi

– Usage: emacs filename

– Save: Ctrl-x Ctrl-s

– Search forward: Ctrl-s

– Search backward: Ctrl-r

– Undo: Ctrl-shift-underscore

– Help: Ctrl-h

– Exit: Ctrl-x Ctrl-c

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Commands: Editing

• gedit – text editor

• gedit is an editor available on the Gnome desktop similar to WordPad on Windows

• The equivalent of gedit on the KDE desktop is kwrite

• Both these editors support syntax highlighting and other useful intuitive features

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Command Line Exercises

1) Using the command line, list the contents of your home directory

2) Check that you are in your home directory using a command

3) Create a directory called projects and change into it

4) Use emacs to create a file called poetry.txt inside the projects directory and save a few lines from a poem

5) Exit emacs and check the contents of poetry.txt in the terminal

6) To finish, return to your home directory using the least amount of keystrokes possible

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Lab Week02 - Part I

Shell Commands

Professional Training Academy Linux Series

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Commands: Manipulation

• cp : copy a file– To copy a file you need to give a source and then a

destination– e.g. to copy the file poetry.txt to poetry_backup.txt, type:

cp poetry.txt poetry_backup.txt

• cp –r : copy a directory– The –r stands for recursive which copies every file

within a directory– e.g. to copy the directory course_work to course_work_backup, type:

cp –r course_work course_work_backup

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Commands: Manipulation

• mv: move a file or directory– As with copy, the move command also needs a source and

a destination– e.g. to move the course_work directory to new_course_work, type:

mv course_work new_course_work

– Note: To rename a file or directory in Linux, use the mv command

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Commands: Removal

• Warning: Removing files and directories is very easy and you may not be asked to confirm. Be very careful!

• rm: remove a file– e.g. to remove a file called poety.txt, type:

rm poetry.txt

• rm –r: remove a directory– e.g. to remove a directory named course_work_backup, type:

rm –r course_work_backup

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Commands: Removal

• rmdir: remove an empty directory

– rmdir can be used as a check. If you want to be sure that the directory you are deleting is empty.

rmdir my_empty_directory

You can also remove a directory (empty, or not) with the command rm -rWhich tells rm to remove recursively the file and any of its contents.

In UNIX a directory is just a special type of file. Just as a text file may contain words, a directory (file) contains other files.

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Commands: Searching

• find: search for a file or directory– e.g. to search for a file named project_results within

the current directory, type:

find ./ -name project_results –print

- to find all of the .cpp files in my entire directory modified in (or within) the last 2 days

find ./ -name '*.cpp' -mtime -2 –print

• locate: search for a program– e.g. to search for the location of the gedit text editor,

type:

locate gedit

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Commands: Searching

• which: print the location of a program

– e.g. to find out where the ls command is stored in the filesystem, type:

which ls

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Commands: Searching

• grep – search for regular expressions in files

– e.g. to search for the word “and” in the poetry.txt file, type:

grep and poetry.txt

– Note: If the word is not found within the poetry.txt file, grep will not print anything. However, if the word is found, grep will print the line where the word was found

• To search for the word “and” in all files in all directories (recursively), type:

grep –r and *

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Commands: Searching

• To make the search case-insentive, type:

grep –i and poetry.txt

• To search for the word “and” inside words, type:

grep *and* poetry.txt

• You can also search for strings, e.g. grep “to be or not to be” poetry.txt

- search all sub-dirs for C files with 'open' find . -name '*.c' -exec grep open {}\;-print

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Commands: Help

• man: lookup a manual page for a command

– Note: All commands in Linux have separate manual pages. You can use these to figure how to use a command or to find extra options. These options are called arguments. For example, when we used ls –l, the –l was the argument to the ls command

– e.g. to look up the manual page for ls, type:

man ls

• Use the space bar to browse page by page

• To quit type q

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Commands: Help

• To search for a word within the manual pages, type forward slash /

– The bottom left of the man page will then allow to you type a search word

– All occurrences of the word will be highlighted

– You can type n (for next) or p (for previous) to move to the next or previous occurrence

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Exercises

1) Change into your projects directory and copy the poetry.txt to poetry_backup.txt

2) Copy your projects directory to a directory called projects_backup

3) Rename the projects_backup directory as projects_completed

4) Change into the projects_completed directory and delete the poetry_backup.txt file

5) Search the poetry.txt file for the word “tHe” or any other mixed case word in your file

6) Lookup the manual page for the copy command and search for the word recursive

7) Use the 'find' command to find all your .cpp files that were modified between 2 and 5 days ago.

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Lab02 – Part II

The Shell Environment

Professional Training Academy Linux Series

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Shell Types

• There are many types of shells available on Linux. Most of which have a history from UNIX

• To check which types of shells are available on your system, type:

cat /etc/shells

– Examples include: bash, sh, ash, bsh, tcsh, csh, ksh, and zsh

– You can switch shell by typing it's name. Although there would be very few reasons for you to need to do this

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Shell Types

• The most popular shells are:– bash: Bourne Again Shell (we are using this shell)– tcsh: TC Shell– ksh: Korn Shell

• The shell is responsible for:– Parsing the command line and input– Executing programs– Evaluating special characters, e.g. *– Managing pipes, redirection, signals, and background

processes

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Shell Configuration Files

• The bash shell looks for a series of configuration files when starting up

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Shell Configuration Files

• The system wide configuration file is in /etc/profile

• As a user, you can customise your own shell using one of the hidden configuration files in your home directory, for example, ~/.profile

• Note: Hidden files are any files beginning with a full stop .*

• These files are the place to store your environmental variables (explained later) or any other shell customisations you wish to make

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Command Line Input

• If you have typed in a long command and notice a typo at the start of the line, you may use ctrl-a to return to the start of the line

• Likewise, you may use ctrl-e to return to the end of the line

• The shell also supports auto-completion of command and file names– Type in the first few letters of a command and use tab to

auto-complete the name

– If there are multiple commands beginning with these letters, the shell will print back a list which you can choose from

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Command Line Input

• The shell also records your command history

• You can access your previous commands using the up arrow and the down arrow

• A useful shortcut is to use the exclamation mark which looks up matches of your previous commands

• For example, if you had previously edited a file as follows: emacs reallylongfilename, you could use the following shortcut:

!emacs r

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Executing Programs

• If you execute a program, such as emacs from your shell, you will notice that you cannot type anymore commands until emacs is finished

• To avoid this, you can tell the shell to run any program such as emacs as a background process using the ampersand sign &

emacs&

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Executing Programs in your PATH

• The bash shell uses the $PATH environment variable to locate commands

• If you cannot run a program, you can echo the PATH variable as a check, for example:

echo $PATH

• To add a directory to your path, separate it by a colon, for example:

export PATH=$PATH:/my/cpp/programs:

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Creating an Alias for your Programs

• The bash shell allows you to create an alias or a shortcut name for programs

• The command alias will return a list of defined aliases in your shell

• Aliases can be useful for shortcuts, typos, and adding extra options, for example:

alias e=emacs

alias emcas=emacs

alias ls=‘ls -lh’

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Executing Programs with Environment Variables

• Environment variables can be set from the terminal or the shell configuration file

• For example, to set-up your proxy in the bash shell, type:

export http_proxy=http://student-proxy.ul.ie

• The export command sets the value of environment variables

• You will need to put this line into your shell configuration file to make it more permanent, e.g. ~/.profile

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Executing Programs with Environment Variables

• You can check the value of a variable using the echo command and placing a dollar sign before the variable, for example:

echo $http_proxy

• You can list the complete set of environment variables using the printenv or the env command

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Special Characters

• Special characters will be evaluated by the shell

• For example, if you want to search all files in your current directory for the word “password”, type:

grep password *

• The wildcard character (*) is evaluated by the shell and expanded to mean all file occurrences found

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Special Characters• Other special characters can be combined to form regular expressions.

Examples of frequently used special characters include:

• . : Match a single character

grep ‘c.e’ filename

• ^ : Beginning of line

grep ‘^coffee’ filename

• $ : End of line

grep ‘coffee$’ filename

• \w : Alphanumeric character [a-zA-Z0-9]

grep ‘c\w*e’ filename \w* matches 0 or more

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Redirecting Output

• The shell allows us to redirect the output of one command to the input of another command

• For example, if we do ls in a directory with hundreds of files it is difficult to keep track of all the files. However, the shell will let us take the output of the ls command and send it to a more useful viewing command such as less

• We can link the two commands using the pipe operator as follows:

ls | more

• The output of the command on the left becomes the input for the command on the right hand side

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Redirecting Output

• The shell will also allow us to redirect the output to a file, which can be useful for creating log files

– e.g. to save all the listed files to a logfile, type:

ls > logfile

• A double arrow operator can also be used for appending, e.g.

ls >> logfile

• Input can be taken from sources using the opposite arrow, e.g.

mail [email protected] < test_results

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Shell Customisation – Prompt Messages

• In order to customise your shell, you need to alter the Prompt String 1 environment variable, which is called PS1

• For example, if you wish to only have a right arrow for your prompt, type:

export PS1=“>”

• Or any other message:

export PS1=“beware of my shell >”

• Special characters can also be used, for example:– \t : current time in HH:MM:SS– \w : current working directory– \u : the current user– \h : the hostname– \a : the ASCII bell character

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Shell Customisation – Colouring Prompts

• To add colour to your prompt, we add a numeric colour value between \e[ and m. Numeric codes are separated by semicolons if more than one is needed

• You can control the background, foreground and bold settings in your terminal

• Note: You will need to reset the colour codes at the end of your prompt to avoid the colour being applied to typed text. The numeric code 0 is used to reset the background, foreground and bold settings, as follows: \e[0m

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Shell– Colouring Customisation Prompts

• The following chart can be used to choose your colour and bold settings:

Source: IBM Developer Works: www-128.ibm.com/developerworks/linux/library/l-tip-prompt

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Shell Customisation - Colour Prompts

• The rows indicate text colour, while the columns indicate background colour

• For example, to set your text to be white on a blue background, we need numeric colour codes: 37 for white text and 44 for blue background (\e[37;44m). We will apply these settings to the current user, time, and directory with a dollar sign for the end of our prompt (\u\t\w$)

export PS1=“\e[37;44m\u\t\w$”

• Unless we want to keep this colour scheme for our typed in text, we will also need to reset our terminal (\e[0m)

export PS1=“\e[37;44m\u\t\w$ \e[0m”

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Shell Customisation – Finishing Touches to Our Colour Prompt

• In order to make the text more readable, we can set the bold face by adding a 1 as follows:

export PS1=“\e[37;1;44m\u\t\w$ \e[0m”

• Note: When we customised our prompt word-wrapping was turned off, in order to fix this we need to escape all non-printable characters so that long commands of text are wrapped onto the next line again. The bash escape sequences for this are \[ and \]

export PS1=“\[\e[37;1;44m\]\u\t\w$ \[\e[0m\]”

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Exercises

1) Change into your project1 directory inside your home directory, re-edit your report.txt file using the least amount of keystrokes possible

2) Check to see which directories are searched for programs that you type in

3) Create a shorter alias for emacs4) Create a file called shopping_list with the following 3 lines:

bread, butter, tea, biscuitsapples, 6 oranges, strawberrieschocolate, ice-cream, 1 cake

Now, search for a line beginning with “apples” Search for any line which contains the letters “ea” Search for a line ending with “s”

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Exercises

5) Save the output of ls to a log file for later use6) Customise your terminal to have green text on a black background

with the current time and current directory as the prompt7) Edit your bash configuration file in your home directory to save your

settings for your customised prompt8) Edit your $PATH shell variable so that when when you issue a

command from the prompt, the shell goes to look for a program of that name by first looking in your current directory. This means modifying the shell variable called $PATH so that it starts with '.:'. The colon is used as a separator and the dot is shorthand for “current directory.” First you will need to find what shell config file the $PATH variable is defined in!

This exercise is useful because it means that instead of having to type './myprog' to run the program of that name in the current directory you can now simply type 'myprog'. Doesn't seem like much now but wait until your back is to the wall and you're tearing your hair out trying to get an assignment working...

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Lab02 – Part III

Files and the File System

Professional Training Academy Linux Series

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

File Ownership

• There are 3 types of permissions in Linux:– Read: r– Write: w– Execute: x

• There are also permissions for 3 different types of users:– the file owner (u)– the file owner’s group (g)– other users (o)

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

File Ownership• The long listing argument to the ls command will show the file

ownership details, for example:

ls –l filename -rwxr-xr-x 1 ict 0 Jan 1 12:00 filename

• The first letter indicates a directory (d) or a file (-)

• The following 3 letters (rwx) are the permissions of the owner

• The next 3 letters (r-x) are the group permissions

• The final 3 letters (r-x) are the permissions for other users

• Note: If a flag is not set, the dash sign is used -

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Changing the Owner

• In the following example, the ict user owns the file:

ls –l filename

-rwxr-xr-x 1 ict 0 Jan 1 12:00 filename

• The change ownership (chown) command can be used to change the owner of a file

• To change the ownership over to the csis user, type:

chown csis filename

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Changing the Permissions• In the following example, other users can read the file

ls –l filename-rwxr-xr-x 1 ict 0 Jan 1 12:00 filename

• The change modification (chmod) command can be used to alter permissions using a plus sign to add or a minus sign to remove a permission. For example, to take away the read permission for other users, type:

chmod o-r filename

• Likewise, to add write permissions to the filename, type the following:

chmod g+w filename

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Hidden Files

• Hidden files in UNIX and Linux begin with a dot. For example, we have already seen the .profile configuration file

• Hidden files are generally configuration files and places to save program settings

• To view hidden files, we can use an argument with ls, as follows (where a means list all files):

ls –a

• We can also use the wildcard special character to list all hidden files, as follows:

ls –a .*

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Comparing Files

• To compare the contents of two or more files, you can use the diff command

• The diff command outputs the differences between the compared files

• For example, to compare two files named file1 and file2, type the following:

diff file1 file2

• Note: Diff will print the lines which differ between the two files

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Creating Symbolic Links• Symbolic links are equivalent to shortcuts on Windows

• You can create a sym link in 2 different locations between:

– programs

– directories

– files, i.e. configuration files

• Example: you have installed a new program in your home directory called coffeemaker and you want to make it available to all other users on your system

• For this, we will need to use the ln command to create a symbolic link to our new location:

ln –s /usr/bin/coffeemaker ~/coffeemaker

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Creating Archives

• There are many ways of archiving in Linux

• You can use zip, and these will also open on Windows and vice-versa

• You can also use tar or bunzip or even jar

• These can all be applied in the same way

• That is, you have a large file or a folder full of files/folders which need to be compressed into a smaller space on disk

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Creating Zip Files• If you have a folder full of files called myProject, you can

create a zip file as follows:

zip –r myProject.zip myProject/*

• The –r means recursive. In other words, zip up everything inside our folder

• The first option given to zip must be the name of our newly compressed file

• We can then tell zip which directory to compress

• You can also list a set of files instead of directory

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Unpacking Zip Files

• To unpack or unzip our file we do the following:

unzip myProject.zip

• The zip program will then unpack all the files into your current working directory

• Note: you should create regular backups for your files at work or in college. You can burn these on CD or copy them to a USB Key

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Creating Tar Files• To create a tar file of our myProject directory, we can do following:

tar –cvf myProject.tar myProject

• The c means create

• The v means verbose (print out info.)

• The f means our filename is myProject.tar

• As with zip, you can also list a set of files instead of the directory as the last argument

• Note: The same arguments (cvf) are also used when creating jar files

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Unpacking Tar Files

• To unpack our file, do the following:

tar –xvf myProject.tar

• The x means extract

• Again, v means verbose output

• And f means file again

• Excellent talking point at your next cocktail party: did you know that 'tar' is a contraction of 'tape archive' (as in magnetic tape), which is how the archives were stored fadó, fadó.

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Creating Log Files

• Previously, we learned about the redirection pipe operator. If you need to redirect the output of a program to an existing file you can use the pipe operator. For log files, however, it is more useful to use the greater than operator, as it will create the file for you if it does not exist

• For example, if you want to record the output from the ls -a command, type:

ls -a > myLogFile

• The output of the ls command will be saved in a file call myLogFile

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Creating Log Files

• If myLogFile does not exist, it will be created

• If myLogFile does exist, it will be overwritten

• To append to the end of a file instead, use the double arrow operator instead of the single arrow operator, as follows:

ls -l >> myLogFile

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

System Log Files

• The log files for the Linux system are stored in the /var/log directory

• For example, any problems or error messages from the system are stored in the messages log file

• We can use the less command to view this file as normal

less /var/log/messages

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

System Log Files• The less command is difficult to use with very large files

such as these, especially when we just want to see the most recent entry appended to the end of the file

• Another command called tail is more useful as it prints the last 10 lines of a file

• For example, to print the last 10 lines of the messages log file:

tail /var/log/messages

• You can also specify how many lines to print, for example, to print the last 50 lines, use the number argument n:

tail –n 50 /var/log/messages

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

System Log Files

• There is another argument for tail which will keep updating the output as the busy log files changes

• This is tail with the –f argument

• To keep an eye on the latest system messages appended to the /var/log/messages file, use the following:

tail –f /var/log/messages

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Installing Files

• Installing files on Linux requires you to be aware of how the program is packaged

• For example, is the program a zipped up C++ project which you must compile?

• Or has it already been compiled for a system like yours and saved as an rpm file?

• Or can you use apt to update it?

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Installing from Source

• If you have to compile a C or a C++ program from source code, you will need the following 3 steps

• Note: You need to be root for the last step, as it installs the program in a system directory for everyone to use

./configure

make

make-install

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Installing an RPM

• The Red Hat Package Manager is a common format for distributing files on Linux, and makes installation much easier

• For example, if you have a program called mygame.rpm, install as follows:

rpm –ivh mygame.rpm

• The i means install

• The v means verbose

• The h means display the installation progress as a series of hashes

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Using Apt

• Apt – Debian Automatic Package Management

• Configure your sources in /etc/sources.list

• To sync your local list with the online repository, type the following:

apt-get update

• Now you are ready to install or update your programs

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Using Apt

• For example, to install the Mozilla Firefox internet browser:

apt-get install mozilla-firefox

• To update:

apt-get upgrade mozilla-firefox

• To un-install:

apt-get remove mozilla-firefox

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Installation Notes

• When installing from source or manually installing rpm files, the program you wish to install will very often have dependencies

• This means that the program depends on other programs or libraries to be installed for its use

• These dependencies need to be installed on your system before installing your program

• However, life is made easier when we use apt, as apt will automatically download dependencies for us

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Open Source Repositories

• Now that you know how to install Linux programs, the following two sites will provide you with an excellent resource for programs

• www.sourceforge.net

• www.freshmeat.net

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Exercises 1) Find out what time you created the report.txt file

2) Change the permissions on your report.txt file so that nobody else but you can read the report

3) Make a copy of report.txt called manager_report.txt and add a new line. In the terminal, find out which line is different between the two

4) Create a symbolic link from manager_report.txt to a file in your home directory called latest_report.txt

5) Create a zipped folder called meeting_files.zip from the project1 directory

6) Open up you system log file so that it updates in real time

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Lab03 – Part I

Files and the File SystemPart (b) – File System

Professional Training Academy Linux Series

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

File Systems

• Linux– ReiserFs, ext2, ext3, ext4 (amongst others)– Since early 2010 Google use ext4 as their internal file system– Since late 2010 Android has used ext4, too

• Windows

– ntfs, fat32, vfat, …

• Linux can read all

• Windows cannot (need to install open source programs to read Linux files)

• Linux is very flexible this way!

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Layout of Linux File System

• Change directory to root

cd /

• List the contents• You will see directories which are similar on all Linux

systems– bin – binary executables– boot – contains the bootable Linux image– etc – configuration files– dev – contains a location for devices– home – users’ home directories– media – location where media is mounted– usr – system users’ files– var – contains log files, mail queues, print queues, etc.

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Layout of Linux File System

Source: The Linux Documentation Project. http://www.tldp.org/LDP/intro-linux/html/sect_03_01.html

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Layout of Linux File System

Source: The Linux Documentation Project. http://www.tldp.org/LDP/intro-linux/html/sect_03_01.html

Directory Content

/bin Common programs, shared by the system, the system administrator and the users.

/boot The startup files and the kernel, vmlinuz. In some recent distributions also grub data. Grub is the GRand Unified Boot loader and is an attempt to get rid of the many different boot-loaders we know today.

/dev Contains references to all the CPU peripheral hardware, which are represented as files with special properties.

/etc Most important system configuration files are in /etc, this directory contains data similar to those in the Control Panel in Windows

/home Home directories of the common users.

/initrd (on some distributions) Information for booting. Do not remove!

/lib Library files, includes files for all kinds of programs needed by the system and the users.

/lost+found Every partition has a lost+found in its upper directory. Files that were saved during failures are here.

/misc For miscellaneous purposes.

/mnt Standard mount point for external file systems, e.g. a CD-ROM or a digital camera.

/net Standard mount point for entire remote file systems.

/opt Typically contains extra and third party software.

/proc A virtual file system containing information about system resources. More information about the meaning of the files in proc is obtained by entering the command man proc in a terminal window. The file proc.txt discusses the virtual file system in detail.

/root The administrative user's home directory. Mind the difference between /, the root directory and /root, the home directory of the root user.

/sbin Programs for use by the system and the system administrator.

/tmp Temporary space for use by the system, cleaned upon reboot, so don't use this for saving any work!

/usr Programs, libraries, documentation etc. for all user-related programs.

/var Storage for all variable files and temporary files created by users, such as log files, the mail queue, the print spooler area, space for temporary storage of files downloaded from the Internet, or to keep an image of a CD before burning it.

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Everything is a File

• Everything that is not a process is treated as a file on Linux

• For example, a directory is just a file which lists the contents of the directory

• Just like system processes, files have id numbers

• You can also view programs, for example, try using less to see the ls command

less /bin/ls

Since it is an executable very little of it is readable; to see all of the “readable stuff” try

strings /bin/ls

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Recovering Disks

• If you have a hard disk which has failed and Windows will not boot from it or allow you to copy your needed files

• Try using a live Linux CD to inspect the hard drive

• If your disk is recoverable, you can mount the drive from your live CD and copy them to a backup location

• It is always worth a try!

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Partitions

• The previous example of a disk failure is the main reason why we use disk partitions. The damage is minimised to the partition instead of the entire disk

• To view the partitions on your computer use the disk space usage command called df.

• Note: The -h option may be used to print the sizes in a more human readable format, as follows:

df -h

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Repairing Disks

• If your Linux disk is faulty or causing problems, you can use a repair program

• For example, the fsck (file system check) program will check and repair a file system

fsck /dev/hda1

• This will try to repair any broken links on hard disk number 1

• Or just type fsck on its own to choose from available partitions

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Creating Bootable CDs

• A good local mirror of Linux distribtions and open source software is available at: ftp.esat.net

• The ISOs can be burnt on to CD or DVD and will allow you to boot and install Linux

• K3b is a popular CD burner program for Linux

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Exercises 1) Examine your root directory on your system and draw a tree

diagram of the most important directories

2) Find a new program in the system-wide users' binary directory

3) Examine the disk partitions on your system, how big is your home directory in GB?

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Lab03 – Part II

Scheduling & Process Management

Professional Training Academy Linux Series

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Processes

• Linux: assumes a multi-user environment with users

running many programs (processes)

• Three types of processes exist:

– interactive: require input

– automatic: await execution in queue

– daemons: server processes always running

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Interactive Processes

• Started through the shell by a user

• May be run in:

– foreground: taking over terminal

– background: using the ampersand & sign, to facilitate

further terminal usage

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Controlling Interactive Processes

• Ctrl-Z: Suspend (pause) a foreground process

• Ctrl-C: Interrupt (cancel/quit) a foreground process

• bg: Send a process to the background

• fg: Bring a process to the foreground

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Controlling Interactive Processes

• The shell allows us to control the jobs. The jobs command lists

all background jobs in the current terminal

• Each process has an id number, When referring to the process use

its id number preceded by a percent sign %

• To terminate a process, use the kill command with the id number

preceded by a percent sign

kill %1

• Similarly, to return a background process to the foreground

fg %2

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Automatic Processes

• Await execution on First-In; First-Out queue (FIFO)

• Processes can also be scheduled for specific times with the at

command

• For example, to run a command at 6pm today, type:

at 6pm today

• When you press enter, you will be asked which command to run

• Press Ctrl-D on a blank line to exit

• You can view queued at jobs with the atq command

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Daemon Processes

• Processes that provide a service

• E.g. ftp server, web server, email server, etc.

• Therefore, they run continuously

• These processes have socket numbers which are used as

connection points by client programs using their service

• To display a list of open sockets on your computer, type:

lsof // used to be 'socklist'

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Viewing Running Processes

• So far, we have learned about jobs which list the processes associated with

the terminal. But what if we want to see all the processes on the system?

• For example, if we want something similar to pressing ctl-alt-del

and viewing the Task Manager in Windows

• The top command will display processes in order of their CPU usage

– It can also be customised to order by other metrics, e.g. memory

usage

• The pstree command will display a text-based tree listing of the

processes on the system.

– Note: that the first process initialised by the Linux system is always the init process

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Viewing Running Processes

• The ps command will report a snapshot of the current processes

• It is usually invoked with the following arguments:

ps -aux

• a: list all processes with a terminal

• u: list processes belonging to user

• x: list all processes belonging to user (not just processes

with a terminal)

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Terminating Running Processes

• It is also common to output ps to grep. For example, to search

for the process id number for firefox:

ps -aux | grep firefox

• If firefox is using too much CPU and you wish to terminate it, use

the kill command:

kill n

• Where n = firefox PID (process id)

• Actually, kill works by sending a signal to the process

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Terminating Running Processes

• The kill command controls how the process will be terminated

•For example, to kill Firefox with a PID of 4123:

kill -15 4123

Signal Name Number Meaning

SIGTERM 15 Terminate process in orderly way

SIGINT 2 Interrupt the process, which can be ignored

SIGKILL 9 Interrupt the process, cannot be ignored

SIGHUP 1 For daemons: reread the configuration file

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Terminating Running Processes in X-Windows

• For killing windows directly on your desktop, use

the xkill command

• The cursor will turn into a skull and cross bones, and kill

whatever process belongs to the window you click on

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Dealing with Common Process Problems

• One of the real benefits of using Linux is that you rarely need to

reboot, as there are many ways to recover from disaster

• If your X-Windows system crashes, you do not need to reboot

– Instead, use Ctrl-Alt-F1 for a new terminal

– Kill the process causing the problem (e.g. use top and

kill). If X-Windows itself is causing the problem, kill the

X-Windows processes (X, KDE, Gnome), and restart the

X-Window system (startx, startkde, ...)

– Use Ctrl-Alt-F7 to return to X-Windows

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Shutting Down Correctly

• Linux machines need to be shutdown correctly

• This gives the filesystem and processes a chance to terminate correctly

and save state if needed

• On the command line, you can use the shutdown command:

shutdown -h now

– h means halt, or use r to reboot

– now means now, other times can also be specified

• The reboot command will reboot the system

• Note: you will need to be root to do this from the command line

PT

A L

inu

x S

erie

s

Copyright Professional Training Academy, CSIS, University of Limerick, 2006 ©

Exercises

1) Start firefox. Find out what process id (pid) number it was assigned when run and terminate it

2) Create a job to output your shopping_list at 17:30 when you are finished work to remind you to do the shopping

3) Find out which program on your system is taking up the most amount of cpu time and which program is taking up the most amount of memory

4) Open emacs and use xkill to terminate it