introduction to linux ( ii )

27
Introduction to Introduction to Linux ( Linux ( II II ) ) Sidney Fong Sidney Fong 11 11 th th Feb 2006 Feb 2006

Upload: rio

Post on 12-Jan-2016

44 views

Category:

Documents


0 download

DESCRIPTION

Introduction to Linux ( II ). Sidney Fong 11 th Feb 2006. Rough Outline. Prerequisites: Knowledge of basic Computer / Operating System concepts Preferably some previous experience of Linux Training is divided into Two Parts: Part I - Introduction of the background of Linux - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Introduction to Linux ( II )

Introduction to Linux (Introduction to Linux (IIII))Sidney FongSidney Fong

1111thth Feb 2006 Feb 2006

Page 2: Introduction to Linux ( II )

Rough OutlineRough Outline

Prerequisites:Prerequisites: Knowledge of basic Computer / Operating System Knowledge of basic Computer / Operating System

conceptsconcepts Preferably some previous experience of LinuxPreferably some previous experience of Linux

Training is divided into Two Parts:Training is divided into Two Parts: Part I - Introduction of the background of LinuxPart I - Introduction of the background of Linux Part II – Quick review of basic Linux commandsPart II – Quick review of basic Linux commands Part III – VimPart III – Vim Part IV – Some miscellaneous topics (if time allows)Part IV – Some miscellaneous topics (if time allows)

Page 3: Introduction to Linux ( II )

Introduction to Linux (Introduction to Linux (IIII))Part I – Some Background of Part I – Some Background of

LinuxLinux

Page 4: Introduction to Linux ( II )

What is “Linux”?What is “Linux”?

Operating system?Operating system? … … What is an operating system?What is an operating system?

Wikipedia: Wikipedia: “the system software responsible for the di“the system software responsible for the direct control and management of hardware and basic srect control and management of hardware and basic system operations”ystem operations”..

The kernel?The kernel? The basic interface (eg. command line, graphical interThe basic interface (eg. command line, graphical inter

face)?face)? The commands? The tools?The commands? The tools? Text editors? Compilers?Text editors? Compilers? Web Browser?Web Browser? … … Everything?Everything?

Page 5: Introduction to Linux ( II )

Operating System (cont’d)Operating System (cont’d)

In general, there is one narrow usage and In general, there is one narrow usage and a broader usagea broader usage

Narrow usage: Only the parts that deal dirNarrow usage: Only the parts that deal directly with the hardware, and very low level ectly with the hardware, and very low level features (i.e. the kernel)features (i.e. the kernel)

Broader usage: Also includes system softBroader usage: Also includes system software. What is “system software” depends ware. What is “system software” depends on the nature of the system.bon the nature of the system.b

Page 6: Introduction to Linux ( II )

Graphical IllustrationGraphical Illustration

User space programs

Shell

Kernel

Hardware

X Client

Daemons

X Server

Page 7: Introduction to Linux ( II )

What is Linux?What is Linux?

There are also two usages of the term There are also two usages of the term “Linux”, corresponding roughly to the “Linux”, corresponding roughly to the narrow/broad usage of the term “Operating narrow/broad usage of the term “Operating System”.System”.

The narrow definition: simply the kernelThe narrow definition: simply the kernel The broader definition: includes basically The broader definition: includes basically

everything available in the systemeverything available in the system

Page 8: Introduction to Linux ( II )

A Typical Linux SystemA Typical Linux System

Linux KernelLinux Kernel User space programs (many are GNU software)User space programs (many are GNU software)

Shell (command line): bashShell (command line): bash Utilities: ls, cp, mv, rm, cat, grep, ...Utilities: ls, cp, mv, rm, cat, grep, ... Compiler: gccCompiler: gcc … … and more …and more …

X Window System (for desktops/workstations)X Window System (for desktops/workstations) Various daemons (a.k.a. services)Various daemons (a.k.a. services)

Eg.: syslogd, httpd, cron, inetdEg.: syslogd, httpd, cron, inetd

Page 9: Introduction to Linux ( II )

A bit of HistoryA bit of History

Richard Stallman founded Richard Stallman founded the GNU project in the 19the GNU project in the 1980’s80’s

Linus Torvalds began writLinus Torvalds began writing a kernel in the early 1ing a kernel in the early 1990’s990’s

=> GNU/Linux, commonly => GNU/Linux, commonly referred to as “Linux”.referred to as “Linux”.

(For more details, see for (For more details, see for yourself on the web)yourself on the web)

Page 10: Introduction to Linux ( II )

Open Source / FreeOpen Source / Free

Open Source – Source code is available to Open Source – Source code is available to everybodyeverybody

““Free” – Source code is available to everyFree” – Source code is available to everybody, and everybody can re-distribute the body, and everybody can re-distribute the source codesource code

““Copyleft” – Source code is available to evCopyleft” – Source code is available to everybody, and if you want to distribute the perybody, and if you want to distribute the program, you must also distribute the sourcrogram, you must also distribute the source code.e code.

Page 11: Introduction to Linux ( II )

Linux DistributionsLinux Distributions Once upon a time, there were softwaresOnce upon a time, there were softwares The softwares were scattered in different sites, and they The softwares were scattered in different sites, and they

were distributed by source codewere distributed by source code If people wanted to use them, they have to download theIf people wanted to use them, they have to download the

m from different sites, compile them, and install themm from different sites, compile them, and install them See how many packages you have in your system! (dpkSee how many packages you have in your system! (dpk

g -l)g -l) Also, consider the difficulty of installing software onto a nAlso, consider the difficulty of installing software onto a n

ew computer (with no operating system pre-installed)ew computer (with no operating system pre-installed) Conclusion: Installation of software is a very complicated Conclusion: Installation of software is a very complicated

process.process. (Refer to the LFS “Distribution” if you want to get a feel o(Refer to the LFS “Distribution” if you want to get a feel o

f this)f this)

Page 12: Introduction to Linux ( II )

Linux Distributions (cont’d)Linux Distributions (cont’d) In the beginning, people did In the beginning, people did

those complicated and tedious those complicated and tedious work themselves.work themselves.

Later, some of them released Later, some of them released their work of collecting the their work of collecting the software to the public. These software to the public. These are called “distributions”.are called “distributions”.

Typically, a modern distribution Typically, a modern distribution handles the following:handles the following: Installation of the OSInstallation of the OS Software Packaging (compiled Software Packaging (compiled

software instead of source)software instead of source) Install / Upgrade / Removing Install / Upgrade / Removing

packagespackages Sensible default configurationsSensible default configurations

You may have heard of these You may have heard of these names:names: Redhat / FedoraRedhat / Fedora MandrakeMandrake DebianDebian SlackwareSlackware GentooGentoo

Page 13: Introduction to Linux ( II )

Introduction to Linux (Introduction to Linux (IIII))Part II – Basic Linux Commands Part II – Basic Linux Commands

(Quick Review)(Quick Review)

Page 14: Introduction to Linux ( II )

Login ScreenLogin Screen

“Message of the day”

Usually contains important notices

Username Hostname Current directoryHint: You may press

<Ctrl+Alt+F1> to <Ctrl+Alt+F6>for multiple terminals if you are

using Linux on the local machine(instead of over network)

Why is the username and hostname displayed in the command prompt?

Page 15: Introduction to Linux ( II )

Getting HelpGetting Help

man pagesman pages

Purpose of commandDifferent

usages

Detailed description of command and

its options

Hint:

Press ‘q’ to quit

Page 16: Introduction to Linux ( II )

More on Man PagesMore on Man Pages There are different sections oThere are different sections o

f man pagesf man pages Section 1: commandsSection 1: commands Section 2: system callsSection 2: system calls Section 3: C library callsSection 3: C library calls Section 4: special files (in /dev)Section 4: special files (in /dev) Section 5: (config) file formatsSection 5: (config) file formats Section 6: “games”Section 6: “games” Section 7: MiscSection 7: Misc Section 8: System administratioSection 8: System administratio

n commandsn commands

If there are conflicts in nIf there are conflicts in names, the section of lowames, the section of lower value is displayed. Foer value is displayed. For a specific section:r a specific section: man <section> <page>man <section> <page>

Note that you may find sNote that you may find section 3 very useful as a ection 3 very useful as a reference to standard C reference to standard C functions (eg. printf, fopfunctions (eg. printf, fopen, string functions, etc)en, string functions, etc)

Page 17: Introduction to Linux ( II )

The BasicsThe Basics

File system:File system: cd, ls, rm, cp, mv, mkdir, rmdircd, ls, rm, cp, mv, mkdir, rmdir

PipingPiping cat input.txt | ./program1 | diff - answer1.txtcat input.txt | ./program1 | diff - answer1.txt

IO RedirectionIO Redirection sort –n > sorted.txt < numbers.txtsort –n > sorted.txt < numbers.txt ls >> logs.txtls >> logs.txt

Process managementProcess management ps, killps, kill

Page 18: Introduction to Linux ( II )

Useful Commands in CompetitionsUseful Commands in Competitions

bc – Arbitrary precision calculatorbc – Arbitrary precision calculator sort – Sort lines of text files (note the “-n” option)sort – Sort lines of text files (note the “-n” option) ps – list running processes on the machineps – list running processes on the machine kill – Sends a signal to running programs (terminates it by default)kill – Sends a signal to running programs (terminates it by default) date – Print system date/timedate – Print system date/time time – reports execution time of a commandtime – reports execution time of a command diff – compares filesdiff – compares files head, tail – prints the first/last N lines of the inputhead, tail – prints the first/last N lines of the input wc – word count (also counts characters and lines)wc – word count (also counts characters and lines) more – pager (allows you to scroll the input for easy viewing)more – pager (allows you to scroll the input for easy viewing) less – better version of moreless – better version of more grep – print lines matching a patterngrep – print lines matching a pattern find – search for filesfind – search for files factor – factorize numbersfactor – factorize numbers tsort – perform topological sorttsort – perform topological sort seq – print a sequence of numbersseq – print a sequence of numbers file – determine file type (sometimes displays other useful information)file – determine file type (sometimes displays other useful information) tar – Archive files (“glue” multiple files into one, and vice versa)tar – Archive files (“glue” multiple files into one, and vice versa)

Page 19: Introduction to Linux ( II )

Some shell scriptingSome shell scripting

For loop:For loop: for ((i=0;i<10;i++)); do echo $i; donefor ((i=0;i<10;i++)); do echo $i; done

If statement:If statement: if [ $ME = “dizzy” ]; then if [ $ME = “dizzy” ]; then

echo “I am $RANDOM years old”echo “I am $RANDOM years old”fifi

While loop:While loop: A=1A=1while [ $A -ne 0 ]; dowhile [ $A -ne 0 ]; do

read Aread Aecho $A | factor echo $A | factor

donedone

Note that spacing may be significant!!Note that spacing may be significant!!

Page 20: Introduction to Linux ( II )

Introduction to Linux (Introduction to Linux (IIII))Part III – VimPart III – Vim

Page 21: Introduction to Linux ( II )

Starting VimStarting Vim Type “vim” in shell. (some systems equate Type “vim” in shell. (some systems equate

vi with vim, but not always)vi with vim, but not always) Screenshot of an empty vim sessionScreenshot of an empty vim session

Page 22: Introduction to Linux ( II )

QuitQuit

Even if you don’t want to learn vi(m), you hEven if you don’t want to learn vi(m), you have to know how to quit:ave to know how to quit: <ESC>:q!<ENTER><ESC>:q!<ENTER>

By the way, the way to exit Emacs:By the way, the way to exit Emacs: Ctrl+X Ctrl+CCtrl+X Ctrl+C

Page 23: Introduction to Linux ( II )

Using Vim as “notepad”Using Vim as “notepad”

Once started, press “i” to enter insert modOnce started, press “i” to enter insert mode.e.

Use like notepad.Use like notepad. To save/quit, <ESC> and :w / :qTo save/quit, <ESC> and :w / :q

Note: set nocp, set bs=2Note: set nocp, set bs=2

Page 24: Introduction to Linux ( II )

Different modesDifferent modes

Vi has two (three?) different modes:Vi has two (three?) different modes: Insert mode – in which you type the textInsert mode – in which you type the text Command mode – in which you issue commands to Command mode – in which you issue commands to

the editorthe editor (ex mode) – more complex commands(ex mode) – more complex commands

Vim starts in command mode by defaultVim starts in command mode by default Various commands to enter insert modeVarious commands to enter insert mode Esc to exit from insert mode back to command modeEsc to exit from insert mode back to command mode During command mode, the “:” character indicates an During command mode, the “:” character indicates an

ex command. For example, ‘:q!’ is an ex command.ex command. For example, ‘:q!’ is an ex command.

Page 25: Introduction to Linux ( II )

Basic movementsBasic movements

Vim is Vim is fastfast when it comes to movement (of when it comes to movement (of cursor). One of the reasons is that you do cursor). One of the reasons is that you do not have to move your hand to the arrow knot have to move your hand to the arrow keys to move. (Almost) Everything can be deys to move. (Almost) Everything can be done by [a-zA-Z0-9]one by [a-zA-Z0-9]

h,j,k,l h,j,k,l : left, down, up, right: left, down, up, right If you want to learn vi, you must learn thesIf you want to learn vi, you must learn thes

e.e.

Page 26: Introduction to Linux ( II )

Vim commandsVim commands

(Refer to command list)(Refer to command list)

Page 27: Introduction to Linux ( II )

vimrcvimrc

vimrc – start up commands (in ex) for Vimvimrc – start up commands (in ex) for Vim Utilize it to personalize your settings. But remember nUtilize it to personalize your settings. But remember n

ot to be over reliant on these because you may not haot to be over reliant on these because you may not have the time to set up a vimrc in competition environmeve the time to set up a vimrc in competition environments (except simple ones)nts (except simple ones)

Simple Example:Simple Example: set nocpset nocp #do not use old vi behavior#do not use old vi behavior

set bs=2set bs=2 # sane backspace setting (see :help bs)# sane backspace setting (see :help bs)set rulerset ruler # “status bar”# “status bar”set aiset ai # auto indent# auto indent

Combine with :map command for easy compile/rCombine with :map command for easy compile/run keysun keys