Linux in 90 Minutes - hcs.harvard.edu · bin/ etc/ lib/ misc/ oracle@ scratch@ tmp/ boot/ home@...

40
Linux Demystified Linux Demystified HARVARD HARVARD COMPUTER COMPUTER SOCIETY SOCIETY Greg Brockman ([email protected]) Andy Brody ([email protected])

Transcript of Linux in 90 Minutes - hcs.harvard.edu · bin/ etc/ lib/ misc/ oracle@ scratch@ tmp/ boot/ home@...

Linux DemystifiedLinux Demystified

HARVARDHARVARDCOMPUTERCOMPUTERSOCIETYSOCIETY

Greg Brockman ([email protected]) Andy Brody ([email protected])

HARVARDCOMPUTERSOCIETY

What is Linux?What is Linux?

HARVARDCOMPUTERSOCIETY

NO!NO!

■ Richard StallmanRichard Stallman

HARVARDCOMPUTERSOCIETY

What is GNU/Linux?What is GNU/Linux?

HARVARDCOMPUTERSOCIETY

GNU’s Not UnixGNU’s Not UnixIt’s recursive… get itIt’s recursive… get it

The Free Software The Free Software FoundationFoundation

HARVARDCOMPUTERSOCIETY

Linux is: Linux is: $0.00$0.00

HARVARDCOMPUTERSOCIETY

Linux is:Linux is:Free SoftwareFree Software

Free as in free speech and Free as in free speech and free beer free beer

HARVARDCOMPUTERSOCIETY

Available in many Available in many distributions on many distributions on many

platformsplatforms

HARVARDCOMPUTERSOCIETY

Ultimate flexibility and Ultimate flexibility and controlcontrol

HARVARDCOMPUTERSOCIETY

Linux General PrinciplesLinux General Principles

HARVARDCOMPUTERSOCIETY

General PrinciplesGeneral Principles■ Less is more (no news is good Less is more (no news is good

news)news)■ Small programs that do one Small programs that do one

thing really wellthing really well■ (Almost) everything is open-(Almost) everything is open-

sourcesource■ Input and output to programs is Input and output to programs is

plain-text: easy to see what plain-text: easy to see what programs doprograms do

HARVARDCOMPUTERSOCIETY

■Multi-user computing Multi-user computing environment with permissionsenvironment with permissions

■Everything is a file. Everything is a file. Everything.Everything.

■Ctrl-Z, Ctrl-D, Ctrl-C – typical Ctrl-Z, Ctrl-D, Ctrl-C – typical ways to get out of somethingways to get out of something

■ If you don’t know, RTFM: If you don’t know, RTFM: manman

Principles cont.Principles cont.

HARVARDCOMPUTERSOCIETY

Where is everything?Where is everything?brockman@cato:/$ ls

bin/    etc/         lib/         misc/  oracle@  scratch@  tmp/boot/   home@        local/       mnt/   proc/    shells/   usr/cdrom@  initrd/      lost+found/  nfs/   root/    srv/      var/

    dev/    initrd.img@  media/       opt/   sbin/    sys/      vmlinuz@

HARVARDCOMPUTERSOCIETY

HARVARDCOMPUTERSOCIETY

Your Home DirectoryYour Home Directory~/~/

HARVARDCOMPUTERSOCIETY

The Linux filetree is flexible.The Linux filetree is flexible./ ./ ..// ./ ../symlinkssymlinks

HARVARDCOMPUTERSOCIETY

User configurationsUser configurationsHidden files : Hidden files : .hiddenstuff.hiddenstuff

HARVARDCOMPUTERSOCIETY

Useful programsUseful programs

HARVARDCOMPUTERSOCIETY

Useful Linux ProgramsUseful Linux Programs

■ finger, writefinger, write■ find, which, find, which, whereiswhereis

■ grepgrep■ ps, kill, ps, kill, killall, topkillall, top

■ jobs, fg, screenjobs, fg, screen■ quota, du, dfquota, du, df

■ lnln■ dig –tracedig –trace■ ping, wget, curlping, wget, curl■ emacs, vimemacs, vim■ echo, catecho, cat■ head, tail, head, tail, less, moreless, more

■ chown, chmodchown, chmod

HARVARDCOMPUTERSOCIETY

Package ManagerPackage Manager

■ Can install software from centralized Can install software from centralized repositoriesrepositories

■ apt-get install <package>apt-get install <package>■ Want a webserver?Want a webserver?

– apt-get install apache2apt-get install apache2

■ Want Open Office?Want Open Office?– apt-get install openoffice.orgapt-get install openoffice.org

HARVARDCOMPUTERSOCIETY

So how do I do anything So how do I do anything useful?useful?

PIPE> >> |

stdinstdinstdinstdinstdinstdin stdout/stderrstdout/stderr

HARVARDCOMPUTERSOCIETY

Commands & PipesCommands & Pipes

■ last | lesslast | less■ find ./ -name “Thumbs.db” -deletefind ./ -name “Thumbs.db” -delete■ fortune | cowsayfortune | cowsay

________________________________________________________________

< It's all in the mind, ya know. >< It's all in the mind, ya know. >

----------------------------------------------------------------

\ ^__^\ ^__^

\ (oo)\_______\ (oo)\_______

(__)\ )\/\(__)\ )\/\

||----w |||----w |

|| |||| ||

HARVARDCOMPUTERSOCIETY

Getting to know your shellGetting to know your shell

tcsh  bash  zsh

HARVARDCOMPUTERSOCIETY

Environment variablesEnvironment variables

■ Control the characteristics of the Control the characteristics of the shellshell– View them withView them with [set]env [set]env, or , or $VARIABLE$VARIABLE

– Set them with Set them with exportexport

– Change up your prompt! Change up your prompt! export export PS1=“myCOOLprompt: ”PS1=“myCOOLprompt: ”

■ But these have to be declared every But these have to be declared every time you use your shell.time you use your shell.– Solution: Solution: ~/.profile, ~/.bash_profile~/.profile, ~/.bash_profile, ,

etc.etc.

HARVARDCOMPUTERSOCIETY

But if we have variables…But if we have variables…

■ And we have all these nifty little And we have all these nifty little programs that can be strung programs that can be strung together…together…

■ Can we make our own programs?Can we make our own programs?■ YES.YES.■ Linux is beautiful.Linux is beautiful.

HARVARDCOMPUTERSOCIETY

Shell ScriptsShell Scripts

■ Shell scripts are “programs” that are Shell scripts are “programs” that are completely uncompiled, but read and completely uncompiled, but read and executed by the shell line by line.executed by the shell line by line.

■ Typically end in .shTypically end in .sh■ Must be chmod’ed executable.Must be chmod’ed executable.■ Start with a “shebang” – tells the Start with a “shebang” – tells the

shell what to use to interpret it. e.g.,shell what to use to interpret it. e.g.,– #! /bin/bash#! /bin/bash for a bash script. for a bash script.

HARVARDCOMPUTERSOCIETY

Quick overview of BASH Quick overview of BASH scriptingscripting

■ Easy hello world program:Easy hello world program:

■ #! /bin/bash#! /bin/bashecho echo “Hello World”“Hello World”

HARVARDCOMPUTERSOCIETY

BASH vs. CBASH vs. C

#! /bin/bash#! /bin/bash

number=3number=3name=name=“bob”“bob”echo echo “$name is your “$name is your 

chosen name, $number chosen name, $number your chosen number.”your chosen number.”

let let “inc=number+1”“inc=number+1”ifif  [[  “$inc”“$inc” –eq  –eq “4”“4”  ]]

then echo then echo “Addition “Addition works like a charm.”works like a charm.”

fifi

#include <stdio.h>#include <stdio.h>#include <cs50.h> #include <cs50.h> 

int number = 3;int number = 3;string name = string name = “bob”“bob”;;printf(printf(“%s is your chosen “%s is your chosen 

name, %d your chosen name, %d your chosen number.\n”number.\n”, number, , number, name);name);

int inc = number++;int inc = number++;if (if ( inc == 4  inc == 4 ) {) {

printf(printf(“Addition works “Addition works like a charm.\n”like a charm.\n”););

}}

HARVARDCOMPUTERSOCIETY

BASH vs. CBASH vs. C■ All variables are All variables are

stringsstrings■ Variables are Variables are

accessed with accessed with $VAR$VAR

■ Runs other Linux Runs other Linux programs to do its programs to do its workwork

■ Spacing usually Spacing usually matters.matters.

■ No line endingsNo line endings

■ Multiple types, Multiple types, must be declaredmust be declared

■ Variables do not Variables do not have prefixeshave prefixes

■ Runs subroutines Runs subroutines or functions from or functions from libraries to do worklibraries to do work

■ Spacing matters a Spacing matters a lot less.lot less.

■ Lines end in ;Lines end in ;

HARVARDCOMPUTERSOCIETY

As you can see there are many As you can see there are many similarities…similarities…

BASH is a programming language in and of itself.

You put all the little pieces of Linux together in the ways that suit you best. It’s your computer to

control.

HARVARDCOMPUTERSOCIETY

smartsyncsmartsync#!/bin/bash#!/bin/bash

eventevent=='-e close_write''-e close_write'

inotifywaitinotifywait -mrq --format-mrq --format '%w %f''%w %f' $event$event $exclude $1$exclude $1 | | \\

while read path file; dowhile read path file; do

echoecho ""$($(datedate '+%F %R' '+%F %R')) sent sent ${path}${file} $2${path}${file} $2""

rsyncrsync -CR -CR ${path}${file} $2${path}${file} $2 || echo || echo "ERROR""ERROR"

donedone

■ Bash makes for convenient glue codeBash makes for convenient glue code

HARVARDCOMPUTERSOCIETY

Focus on command line,Focus on command line,a GUI is often secondarya GUI is often secondary

HARVARDCOMPUTERSOCIETY

Trinity uses Linux.Trinity uses Linux.

HARVARDCOMPUTERSOCIETY

HARVARDCOMPUTERSOCIETY

Try doing that by point-and-Try doing that by point-and-click... oh wait.click... oh wait.

HARVARDCOMPUTERSOCIETY

got graphix?got graphix?

■ So far we’ve been staring a lot at So far we’ve been staring a lot at text consoles.text consoles.

■ Linux does allow for the display of Linux does allow for the display of graphics:graphics:– X11 on nice – demo it!X11 on nice – demo it!– Window managers: Gnome, KDEWindow managers: Gnome, KDE– You can see these on the SC Lab You can see these on the SC Lab

computers, or in most desktop Linux computers, or in most desktop Linux distros, e.g., Ubuntu, Suse, Red Hat, etc.distros, e.g., Ubuntu, Suse, Red Hat, etc.

HARVARDCOMPUTERSOCIETY

Cool programs with graphicsCool programs with graphics

■ Firefox (duh)Firefox (duh)■ WiresharkWireshark■ VNCVNC■ XaoSXaoS

HARVARDCOMPUTERSOCIETY

Anatomy of a DistributionAnatomy of a Distribution

■ Building an OS with Linux is complex!Building an OS with Linux is complex!

HARVARDCOMPUTERSOCIETY

Try it out!Try it out!

■ You can try Linux in a VM, with a You can try Linux in a VM, with a LiveCD, or by installing alongside your LiveCD, or by installing alongside your current OS (dual booting).current OS (dual booting).

■ The internets are your friend: there The internets are your friend: there are lots of forums and email listsare lots of forums and email lists

■ See Wikipedia for history, etc.See Wikipedia for history, etc.■ Read the man pages for details on Read the man pages for details on

any particular command (`man ls`)any particular command (`man ls`)

HARVARDCOMPUTERSOCIETY

Till next time...Till next time...

Come to HCS weekly meetingsCome to HCS weekly meetings

www.hcs.harvard.edu/events www.hcs.harvard.edu/events for schedulefor schedule