introduction to linux_dr mohamad sobh

Upload: mohammed-farag

Post on 06-Apr-2018

229 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/2/2019 Introduction to Linux_Dr Mohamad Sobh

    1/44

    Introduction to LinuxDr. M. Sobh

  • 8/2/2019 Introduction to Linux_Dr Mohamad Sobh

    2/44

    Agenda

    Overview Installation Linux GUI File System Administration Shell Shell Scripting Programming Advanced Linux Software

  • 8/2/2019 Introduction to Linux_Dr Mohamad Sobh

    3/44

    Overview

    Operating System Hardware Types Operating Systems Types Linux Types Ubuntu Operating System

  • 8/2/2019 Introduction to Linux_Dr Mohamad Sobh

    4/44

    Operating System

    Hardware dependent Software. Consisting of programs and data. Runs on computers.

    Manages computer hardware resources. Provides common services for execution of various application software.

  • 8/2/2019 Introduction to Linux_Dr Mohamad Sobh

    5/44

  • 8/2/2019 Introduction to Linux_Dr Mohamad Sobh

    6/44

    Operating Systems Types

    Microsoft Family DOS, Windows 3.1 / 95 / 98 / Me / 2000 / XP / Vista / 7 /

    WinCE / NT Server / 2003 / 2008 MAC Family

    MAC OS / MAC OS Server Unix

    HP-UX, AIX, Solaris, MAC OS X Linux

    Fedora, Red-Hat, Ubuntu, Centos...

  • 8/2/2019 Introduction to Linux_Dr Mohamad Sobh

    7/44

    Linux Types

    Ubuntu, CentOS Debian Fedora Suze Android and Google Chrome OS

  • 8/2/2019 Introduction to Linux_Dr Mohamad Sobh

    8/44

    Ubuntu Operating System

    By Canonical Ltd at 2004 Latest Version: 11.04 Supported Language: 55 Supported Hardware: Intel 32 and 64 and compatibles

    License: Free Types: Client 32 and 64 / Server 32 and 64

  • 8/2/2019 Introduction to Linux_Dr Mohamad Sobh

    9/44

    OS Market Share

    Client Microsoft: 81% MAC: 11% Linux: 3%

    Server Microsoft: 30% Linux: 70%

    Mobile

    Microsoft: 3% Linux: 28% Apple: 18% Symbian: 37% Blackberry: 14%

    Mainframe andSupercomputers

    Microsoft: 1% Unix and Linux: 98%

    Industry Microsoft: 10% Linux: 50% Custom: 20% None: 20%

    References www.eetimes.com www.linuxfordevices.com www.wikipedia.org

  • 8/2/2019 Introduction to Linux_Dr Mohamad Sobh

    10/44

    Installation

    Source Type Live CD Live USB

    Installation Type

    Standalone in a separate partition Virtual partition - for windows users

    Requirements for Client Hard-disk space: 17 GB and 30 GB is recommended RAM: 1 GB and 2 GB is recommended

    CD-Image Download http://www.ubuntu.com/download

    http://www.ubuntu.com/download
  • 8/2/2019 Introduction to Linux_Dr Mohamad Sobh

    11/44

    Linux GUI

    System Menu Desktop Basic Software

    Calculators

    Text Editor Games and Graphics Internet Office Media Date and Time and Zone Chat Language System Start / Shutdown / Suspend

  • 8/2/2019 Introduction to Linux_Dr Mohamad Sobh

    12/44

    File System

    Basic File Browser Linux File System Structure Mounting Other Drives and External Drives File Permissions

    Creating Folders and Files

  • 8/2/2019 Introduction to Linux_Dr Mohamad Sobh

    13/44

  • 8/2/2019 Introduction to Linux_Dr Mohamad Sobh

    14/44

    ShellCommand Line Interface to Manage Linux

  • 8/2/2019 Introduction to Linux_Dr Mohamad Sobh

    15/44

    Listing Files and Directories

    lsCourses.pdf linux.ppt Linux.ppt Qt.run

    ls -l-rwx--x--x 1 sobh sobh 725286856 2011-06-11 15:30 Qt.run

    ls -aCourses.pdf linux.ppt Linux.ppt Qt.run .System.dat

    ls ~ --> list all files at user account root

  • 8/2/2019 Introduction to Linux_Dr Mohamad Sobh

    16/44

    Making and Browsing Directories

    mkdir ShellTestcd ShellTestmkdir folder1mkdir folder2cd folder1

    pwd --> /home/useraccountname/ShellTest/folder1mkdir folder1amkdir folder1bcd .

    pwd --> /home/useraccountname/ShellTest/folder1cd ..

    pwd --> /home/useraccountname/ShellTestcd folder1/folder1a

    pwd --> /home/useraccountname/ShellTest/folder1/folder1acd ~/ShellTest pwd --> /home/useraccountname/ShellTestls folder1 --> folder1a folder1bcd folder1ls . --> folder1a folder1bls .. --> --> folder1 folder2

  • 8/2/2019 Introduction to Linux_Dr Mohamad Sobh

    17/44

    Making and Managing Files

    cd ShellTest

    cat > file1.txtWelcome to Linux ShellWelcome to Linux World^D

    cat file1.txt -->Welcome to Linux ShellWelcome to Linux World

    ls --> file1.txt folder1 folder2cp file1.txt file2.txtls --> file1.txt file2.txt folder1 folder2mv file1.txt myfile1.txtmv myfile1.txt folder1mv file2.txt folder2/myfile2.txt

    cp folder1/myfile1.txt tempfile.txt

    rm tempfile.txt

  • 8/2/2019 Introduction to Linux_Dr Mohamad Sobh

    18/44

    More Managing Files and Folders

    mkdir tempfolder mkdir tempfolder/tempsubfolder rmdir tempfolder --> failrmdir tempfolder/tempsubfolder rmdir tempfolder --> succeedmkdir tempfolder

    mkdir tempfolder/tempsubfolder

    mkdir tempfolder mkdir tempfolder/tempsubfolder cp -r tempfolder tempfolderbackuprm -r tempfolder mv tempfolderbackup tempfolder mkdir basefolder mv tempfolder basefolder

  • 8/2/2019 Introduction to Linux_Dr Mohamad Sobh

    19/44

    Displaying the File Contentscat > file1.txt...long text...^Dcat >> file1.txt...additional text...^Dcat file1.txtclear --> clear shell screenless file1.txt --> view contents in pages, use [Space Bar] and [q]head file1.txt

    man head --> command help, use [q] to quithead -1 file1.txt --> view firs 1 linehead -n 1 file1.txt --> view firs 1 linehead -c 10 file1.txt --> view first 10 letterstail -1 --> view last 1 linegrep Egypt file1.txt --> all lines with Egyptgrep -i egypt file1.txtgrep Egypt *grep -r Egypt *grep -c Egypt file1.txtgrep -v Egypt file1.txt --> all lines without Egyptwc -w file1.txtwc -l file1.txt

  • 8/2/2019 Introduction to Linux_Dr Mohamad Sobh

    20/44

    Redirection

    cat > file.txtcat >> file.txtcat file.txtcat file.txt file.txtcat file.txt > bigfile.txt

    cat bigfile.txtcat file.txt file.txt > bigfile.txtcat bigfile.txtcat file.txt file.txt >> bigfile.txtcat bigfile.txtls > files.txt

    grep Egypt file.txt > result.txt

    whowho > temp.txt --> list of logged in users

    sort < file.txtsort < file.txt > sorted.txt

  • 8/2/2019 Introduction to Linux_Dr Mohamad Sobh

    21/44

    Pipes

    lsls | wc -wls | wc -w > count.txtsort < file.txt | grep Egypt | wc -wsort < file.txt | grep Egypt | wc -w > temp.txt

  • 8/2/2019 Introduction to Linux_Dr Mohamad Sobh

    22/44

    Wildcards

    lsls *ls *.txtls f*.txtls f*.*ls file?.txt

    ls [fc]*.txt

  • 8/2/2019 Introduction to Linux_Dr Mohamad Sobh

    23/44

    Getting Help

    man cp --> command helpaprops copy --> matched command

  • 8/2/2019 Introduction to Linux_Dr Mohamad Sobh

    24/44

    File System Securitycat > myfile

    mkdir myfolder ls -l-rw-r--r-- 1 sobh sobh 92 2011-07-19 05:38 myfiledrwxr-xr-x 4 sobh sobh 4096 2011-07-19 05:38 myfolder

    permissions (folder)(owner/group/others)

    chmod u+x myfilechmod go+w myfile-rwxrw-rw- 1 sobh sobh 92 2011-07-19 05:38 myfilechmod u-x myfilechmod go-w myfile-rw-r--r-- 1 sobh sobh 92 2011-07-19 05:38 myfile

    sudo bash --> switch to root privilegeschown root:root myfile-rw-r--r-- 1 root root 6 2011-07-19 06:03 myfileclose and reopen the shellcat myfile --> can viewcat > myfile --> cannot appendsudo bashchown sobh:sobh myfileclose and reopen the shell

    cat > myfile --> can append

  • 8/2/2019 Introduction to Linux_Dr Mohamad Sobh

    25/44

    Processes and Jobs

    ps --> list active processessleep 10lunch sleep process for 10 sec then go back after finish

    sleep 10& -->[1] 16717 ([Job Number] Process ID)lunch sleep process for 10 sec in background then goback immediately

    sleep 10& -->[2] 16718

    sleep 100^Z --> suspend the process

    bg --> list background processes

    sleep 100sleep 100

    jobs --> list all jobs with numbers[1]- Running sleep 100 &[2]+ Running sleep 100 &fg 1

  • 8/2/2019 Introduction to Linux_Dr Mohamad Sobh

    26/44

    Processes and Jobs

    sleep 100&sleep 100&

    jobs[1]- Running sleep 100 &[2]+ Running sleep 100 &kill %2

    jobs[1]- Running sleep 100 & ps16756 pts/0 00:00:00 sleepkill 16756

    sleep 100^z[1]+ Stopped sleep 100

    ps16764 pts/0 00:00:00 sleepkill 16764 --> failedkill -9 16764 --> killed

  • 8/2/2019 Introduction to Linux_Dr Mohamad Sobh

    27/44

    Other Commands

    1.cat > file.txt..very long content .. ^dcompress file.txt --> produce compressed file file.txt.Zuncompress file.txt.Z --> uncompress the filegzip file.txt --> produce compressed file file.txt.gz

    gunzip file.txt.gz --> uncompress the file

    2.tar -zcvf myfolder.tar.gz myfolder tar -zxvf myfolder.tar.gz

    3.if tar is not installedsudo bash --> to login as rootapt-get install tar --> to download and install tar

    4.sudo bash --> to login as root

    synaptic --> lunch Ubuntu advanced package installer

  • 8/2/2019 Introduction to Linux_Dr Mohamad Sobh

    28/44

    Other Commands

    1.echo Hello Worldecho Hello World > file.txtecho Hello World | wc -w

    2.

    historyhistory 10 --> last 10 command

    3.gedit --> lunch visual text editor

    4.echo $PATHPATH=$PATH:~/ShellTestecho $PATH

  • 8/2/2019 Introduction to Linux_Dr Mohamad Sobh

    29/44

    Text Editing Using VI

  • 8/2/2019 Introduction to Linux_Dr Mohamad Sobh

    30/44

    Text Editing Using VI

    vi vi: start empty file vi vi filename: start vi and open filename i: insert mode

    ESC: command mode yy: copy current line p: past to the next line w filename: save to the filename w: save to the existing file q: quit q!: quit any way wq: save and quit o filename: open a file

  • 8/2/2019 Introduction to Linux_Dr Mohamad Sobh

    31/44

    Why?

    VI is very important to edit and view files at remote terminals

  • 8/2/2019 Introduction to Linux_Dr Mohamad Sobh

    32/44

    Shell Scripting

  • 8/2/2019 Introduction to Linux_Dr Mohamad Sobh

    33/44

    Simple Shell Scripting

    Save following script in a file [testshell]----------#!/bin/shmkdir folder1mkdir folder1/folder1amkdir folder1/folder1b

    mkdir folder2

    echo hello world > file1.txtcp file1.txt folder1mv file1.txt folder2

    echo done.----------chmod u+x testshell./testshell --> the file is executedaltPATH=$PATH:.testshell

  • 8/2/2019 Introduction to Linux_Dr Mohamad Sobh

    34/44

    Advanced Shell ScriptingEdit [testshell] with following script

    ----------#!/bin/sh# This file looks through all the files in the current# directory for the string Egypt, and then prints the names of # those files to the standard output.for file in *do

    if grep -q Egypt $filethenecho $filefidoneexit 0----------

  • 8/2/2019 Introduction to Linux_Dr Mohamad Sobh

    35/44

    Advanced Shell Scripting

    Shell Scripting Supports: Variables and Constants Loops Conditions

    Functions

  • 8/2/2019 Introduction to Linux_Dr Mohamad Sobh

    36/44

    Compiling C/C++ Programs

  • 8/2/2019 Introduction to Linux_Dr Mohamad Sobh

    37/44

    Compiling Simple C Program

    Save following code in hello.c file:------------------#include "stdio.h"int main(int argc, char* argv[]){int height;

    printf("Enter your height (cm):");

    scanf("%d", &height); printf("Your ideal weight %d kg\n", height-100);return 0;}------------------gcc hello.c --> produce executable file a.outa.out

    gcc hello.c -o hello --> produce executable file hellohello-------------------gcc and cc are both c compilers (identical in Ubuntu)

  • 8/2/2019 Introduction to Linux_Dr Mohamad Sobh

    38/44

    Compiling Simple C++ Program

    Save following code in hello.c file:------------------#include

    using namespace std;

    int main(int argc, char *argv[])

    {int height;coutheight;cout

  • 8/2/2019 Introduction to Linux_Dr Mohamad Sobh

    39/44

    Compiling Multi-File ProgramsWrite files:weight.h----------

    int weight(int height);weight.c----------int weight(int height){return height - 100;}main.c----------#include "stdio.h"#include "weight.h"

    int main(int argc, char* argv[]){int height;

    printf("Enter your height (cm):");scanf("%d", &height);

    printf("Your ideal weight is %d kg\n", weight(height));return 0;}-----------------gcc main.c weight.c -o idealweight --> Compile and Link or gcc -c main.c --> Compile Only main.c and produce main.ogcc -c weight.c --> Compile Only weight.c and produce weight.o

    gcc -o idealweight main.o weight.o --> link main.o weight.o to idealweight

  • 8/2/2019 Introduction to Linux_Dr Mohamad Sobh

    40/44

    Using External LibrariesWrite files:trig.c----------

    #include #include #include

    int main(int argc, char *argv[]){char input[25];double angle;

    printf("Enter the angle in radians :");if (!fgets(input, 25, stdin))perror("error!!.\n");angle = atof(input);

    printf("sin(%0.3lf) = %0.3lf\n", angle, sin(angle)); printf("cos(%0.3lf) = %0.3lf\n", angle, cos(angle)); printf("tan(%0.3lf) = %0.3lf\n", angle, tan(angle));return 0;}-----------------gcc trig.c -o trig --> gives link errors (math library not provided)gcc trig.c -lm -o trig --> use a library with name libm

  • 8/2/2019 Introduction to Linux_Dr Mohamad Sobh

    41/44

    Compiling a Shared Libraryusing files weight.c weight.h main.c

    gcc -fPIC -c weight.c --> Compile Only weight.c and produce weight.ogcc weight.o -shared -o libweight.so -->produce a shared library libweight.so

    gcc -c main.c --> Compile Only main.c and produce main.o

    gcc -lweight -L. main.o -o idealweight --> link and produce the executable

    cp libweight.so /usr/lib --> copy the library to common place for current user or cp libweight.so /lib --> if you are a root user

    idealweight --> execute the application

    to test which library is required by an executable

    ldd idealweightldd trig

  • 8/2/2019 Introduction to Linux_Dr Mohamad Sobh

    42/44

    Compiler Optionsusing files weight.c weight.h main.c

    gcc -g -Wall main.c weight.c -o idealweight

    -Wall turn on all warning options-g allow future debugging

  • 8/2/2019 Introduction to Linux_Dr Mohamad Sobh

    43/44

    Debuggingusing files weight.c weight.h main.c and compile with

    gcc -g -Wall main.c weight.c -o idealweight

    gdb idealweight

    (gdb)list main.c:1(gdb)list weight.c:1(gdb)list main(gdb)list weight(gdb)run(gdb)break weight(gdb)run(gdb)print height(gdb)next(gdb)next(gdb)next(gdb)c(gdb)break main.c:7(gdb)run(gdb)next(gdb)next(gdb)c

  • 8/2/2019 Introduction to Linux_Dr Mohamad Sobh

    44/44

    Make File