linux programming unit1-ppt

68
LINUX Programming Introduction to LINUX

Upload: ramadevi1092

Post on 26-Nov-2015

29 views

Category:

Documents


4 download

DESCRIPTION

this ppt deals with all concepts in linux unit1

TRANSCRIPT

Page 1: linux programming unit1-ppt

LINUX ProgrammingIntroduction to LINUX

Page 2: linux programming unit1-ppt

What is LINUX?

An Operating System (OS) Mostly coded in C Machine independence It provides a number of facilities:

management of hardware resources directory and file system loading / execution / suspension of

programs

Page 3: linux programming unit1-ppt

History (Brief)

1969 First UNIX at Bell Labs The MULTICS Kernighan, Ritchie,

Thompson 1970’s

Bell Labs makes UNIX freeware

Berkeley UNIX (BSD) Bill Joy vi editor, C

Shell

1980’s System V release 4 TCP/IP Sun Microsystems

Solaris Microsoft Xenix, SCO MIT X-Windows

1990’s GNU, LINUX Stallman, Torvalds

Page 4: linux programming unit1-ppt

Why Use LINUX?

multi-tasking / multi-user lots of software networking capability graphical (with command line) easy to program portable (PCs, mainframes,

super-computers)

continued

Page 5: linux programming unit1-ppt

free! (LINUX, FreeBSD, GNU) Popular not tied to one company Wider choice active community

Next

Page 6: linux programming unit1-ppt

Your Account

Each user has their own space called their account.

Type your login ID and password to enter your account.

Only if the login ID and password match will you be let in.

Page 7: linux programming unit1-ppt

Login to your Account

login: ad You type your ID and RETURN.

Password: You type your password and RETURN. It does not appear.

$ The UNIX prompt (or similar). You can now enter commands.

Page 8: linux programming unit1-ppt

Logout from your Account

logout

or

^D Press CONTROL and D together

orexit

Page 9: linux programming unit1-ppt

On-line Help

man Manual pages Spacebar to go on; ^C to

stopman gnuchess

man man

apropos topic Lists commands related to topic

apropos game

apropos help

Page 10: linux programming unit1-ppt

Hardware

Kernel

Compilers

Inte

rne

t To

ols

Unix

Comm

ands

Databas

e Package

s

Other A

pplicatio

n/

System Softw

are

Shell

Shell

Shell

Shel

lUser

Use

r Use r

User

Kernel-Shell Relationship

Next

Page 11: linux programming unit1-ppt

The Shell

The UNIX user interface is called the shell. The shell does 4 jobs repeatedly:

displayprompt

executecommand

processcommand

readcommand the shell

Page 12: linux programming unit1-ppt

Typing Commands

Try these:date

cal 3 2005

who

ls -a

man cal

clear

Page 13: linux programming unit1-ppt

Changing your Password

The command is:passwd

It will ask you for the new password twice.

Page 14: linux programming unit1-ppt

Date Commands

date Gives time and date

cal Calendarcal 1997

cal 3

cal 7 1962

cal 9 1752

Page 15: linux programming unit1-ppt

You and the System

uptime Machine’s ‘up’ time hostname Name of the machine

whoami Your name who

Page 16: linux programming unit1-ppt

Calculators

expr e Simple arithmeticexpr 3 + 5 + 7

bc Programmable Calculator

Page 17: linux programming unit1-ppt

Some General Purpose CommandsSome General Purpose Commands

date date locatelocatecal cal moremorewhowho passwdpasswdls ls echoechomanman bannerbannerclearclear tty tty uptimeuptime uname uname

hostnamehostname tput tput

quotaquota spell spell

whoamiwhoami ispell ispell aproposapropos catcatwhatiswhatis sortsortwhichwhich pwdpwd

Page 18: linux programming unit1-ppt

Redirection, pipes , processes Output can be redirected to a file with‘>‘:

ls > dir.txtcal 2004 > year2004

Output can be appended to a file with ‘>>‘cal 2004 > yearscal 2005 >> years

Pipes : sending the output of one program to the input of the otherls | sort

who | sort

Processes : Running two commands sequentially

locate mj > xxx; date

locate usr > xxx &

Page 19: linux programming unit1-ppt

The UNIX File System

Page 20: linux programming unit1-ppt

The File

Ordinary Files

Directory Files

Device Files

Page 21: linux programming unit1-ppt

Ordinary files Ordinary files can contain text, data, or program

information. Files cannot contain other files or directories. Unlike other operating systems, UNIX filenames are not

broken into a name part and an extension part (although extensions are still frequently used as a means to classify files).

Instead they can contain any keyboard character except for '/' and be up to 256 characters long (note however that characters such as *,?,# and & have special meaning in most shells and should not therefore be used in filenames). Putting spaces in filenames also makes them difficult to manipulate - rather use the underscore '_'.

Page 22: linux programming unit1-ppt

Directory Files Directories are containers or folders that hold files,

and other directories.

Device Files To provide applications with easy access to

hardware devices, UNIX allows them to be used in much the same way as ordinary files.

There are two types of devices in UNIX - block-oriented devices which transfer data in blocks (e.g. hard disks) and character-oriented devices that transfer data on a byte-by-byte basis (e.g. modems and dumb terminals).

Page 23: linux programming unit1-ppt

The Parent Child Relationship

A simplified UNIX directory/file system:

/

etc bin usr1

faculty

dev tmp

date cal. . .. . . . . .. . .

mj

Page 24: linux programming unit1-ppt

System Directories / root directory /home Home directory for all users /bin and /usr/bin all commands /sbin and /usr/sbin admin commands /etc configuration & system data files (e.g. /etc/passwd) /dev files representing I/O devices (they don’t occupy

space on disk) /lib and /usr/lib Library files in binary form. /usr/include Standard Header files used by C

Program /tmp allows users to create temporary files. /var Variable part of file system

Page 25: linux programming unit1-ppt

Pathnames

A pathname is a sequence of directory names (separated by /’s) which identifies the location of a directory.

There are two sorts of pathnames absolute pathnames relative pathname

Page 26: linux programming unit1-ppt

Absolute Pathnames

The sequence of directory names between the top of the tree (the root) and the directory of interest.

For example:/bin/etc/terminfo/export/user/home/ad/export/user/home/s3910120/proj1

Page 27: linux programming unit1-ppt

Relative Pathnames

The sequence of directory names below the directory where you are now to the directory of interest.

If you are interested in the directory proj1:proj1 if you are in s3910120s3910120/proj1 if you are in homehome/s3910120/proj1 if you are in user

Page 28: linux programming unit1-ppt

Commands and Pathnames

Commands often use pathnames.

For example:/usr/games/fortune

cat /etc/passwd List the password file

Page 29: linux programming unit1-ppt

pwd Print name of current working directory

Move back to directory (the parent directory):cd ..

When in proj1, move to proj2 with one command:cd ../proj2

../proj2 is a relative pathname

Page 30: linux programming unit1-ppt

Special Directory Names

/ The root directory . The current working directory .. The parent directory

(of your current directory)

Page 31: linux programming unit1-ppt

Examples

cd / Change to root directory cd ~ Change to home directory cd (Special case; means cd ~)

cd ../.. Go up two levels.

Page 32: linux programming unit1-ppt

Investigate the System

Use cd cat file List file

cd /etc

cat passwd

ls Directory listingls List current dir.

ls /etc List /etc

Page 33: linux programming unit1-ppt

Making / Deleting / Renaming Directories

Usually, you can only create directories (or delete or rename them) in your home directory or directories below it.

mkdir Make a directory

rmdir Delete a directory

mv Rename a directory

Page 34: linux programming unit1-ppt

Permissions

ls –l /etc/passwd-rw-r--r-- 1 root root 2365 Jul 28 16:19 /etc/passwd

read, write, execute (r w x) - rw- r-- r-- directory owner group everyone

chmod -w, +w ….

Page 35: linux programming unit1-ppt

Commands to work with files

cat > filename less head tail cp mv rm wc grep spell ispell

Page 36: linux programming unit1-ppt

ls: listing directory contents• ls -- listing order numerals, UPPERCASE, lowercase

• ls –x -- displays multicolumnar output

• ls –Fx -- in o/p * indicates executables and /refers to directories

• Ls –a -- lists (.) (dot) files

• ls –l -- output in long format

• -R -- recursive list

• -r -- sorts filenames in reverse order

• -1 -- one filename in each line

• -d dirname -- lists only dirname if dirname is a directory

• -i -- displays inode number

Page 37: linux programming unit1-ppt

Handling Ordinary files

Page 38: linux programming unit1-ppt

cat : displaying and creating files

To display the contents of the small files$ cat dept.lst -- dept.lst should be present in the current directory

• Cat also accepts more than one argument. $ cat file1 file2 The contents of file2 is shown immediately after file1.

cat concatenates the 2 files, hence its name. Options with cat (-v and –n) (not POSIX rec.)-v --- displays non printable characters-n --- displays the numbering lines. (else use pr)

Page 39: linux programming unit1-ppt

Creating files using cat

$cat > file1

> First file created using cat command.

<ctrl+d>

$_ - returns to shell prompt

$ cat >> file1 -- appends to file1

Page 40: linux programming unit1-ppt

Copying a file - cp$ cp file1 file2 $ cp file1 /data/official - file1 is copies into /data/official

$ cp file1 /data/official/letter1 - file1 is copied to /data/official as letter1

$ cp /home/mca01/p1.c p1.c -- p1 is copied as p1.c in current directory (destination is a file)

$ cp /home/mca01/p1.c . -- destination is a directory

$ cp file1 file2 file3 off_data -- copying multiple files, here the last filename must be a directory and must be an existing directory.

Page 41: linux programming unit1-ppt

cp options

$ cp –i file1 file2 -- interactive copying

$ cp –R off_data official - copies directory structure.

• Here off_data and official are directories.

• If official directory does not exists then cp creates it.

• If official directory exists then off_data becomes a subdirectory under official.

Page 42: linux programming unit1-ppt

Deleting files : rm$ rm file1 file2 -- deletes file1 and file2 in the current directory.

$ rm /data/file1 /data/chap2 -- using absolute pathnames

$ rm /data/file[12] -- similar to the previous one.

$ rm –I file1 -- interactive deletion, asks for confirmation before deletion.

$ rm –r * or rm –R * -- recursively deletes the directories and subdirectories.

$ rm –rf * --- the –f option forceably removes the files/directories

Page 43: linux programming unit1-ppt

Renaming Files : mv

mv filename1 filename2 --- moves a file (i.e. gives it a different name, or moves it into a different directory)

mv chap01 unit01 mv chap01 chap02 chap03 notes Here notes is a directory

Page 44: linux programming unit1-ppt

Security by File Permissions ls –l complete description

File Type & Permissions, Links, Ownership, Group Ownership, File Size, Last Modification Time

id for to know UID & GID chmod

Relative Permissions Absolute Permissions Option -R

chown – Change Ownership chgrp – Change Group Owner umask

Page 45: linux programming unit1-ppt

Process utilities

Page 46: linux programming unit1-ppt

Disk utilities df :- Report how much free disk space is

available for each file system separately. It shows the information like

Directory where file system is attached. Device name of the file system. Number of 512-byte blocks available. Number of files we can create. Options –t (Total), -k (Reports size in KB), -h

(Reports size in MB, GB)

Page 47: linux programming unit1-ppt

du :- Disk usage :- To find the consumption of a specific directory tree rather than entire file system. Options –s for summary of usage.

Page 48: linux programming unit1-ppt

Networking Commands

Page 49: linux programming unit1-ppt

Communicating with People

Page 50: linux programming unit1-ppt

Information on Others

users Who else is logged on?

who Information on current users

ps What are people doing?ps -au

Page 51: linux programming unit1-ppt

w What are people doing?w -sh A shorter report

Examine password info:more /etc/passwd

grep s38 /etc/passwd

Page 52: linux programming unit1-ppt

Fingering People

finger Info. on current usersfinger -l Longer information

finger user Information on user (need not be logged

in)finger ad

Page 53: linux programming unit1-ppt

finger @machine-name User info. for that machine

finger @catsix

finger @ratree.psu.ac.th

ping machine-name Is machine alive (on)?

ping catsix (^C to stop)

Page 54: linux programming unit1-ppt

Your Finger Information

chfn Change your finger entry

finger also prints the contents of the .plan and .project files in your home directory. List ‘.’ files with:ls -a

Page 55: linux programming unit1-ppt

Talking

talk user Talk to user (on any machine)

talk ad

talk [email protected]

Get out by typing ^C

Page 56: linux programming unit1-ppt

write user Send a message to user

on this machinewrite ad

mesg n Switch off talk / write acceptance.

mesg y Switch on

Page 57: linux programming unit1-ppt

Sending E-mail

Send mail :

mail Add Subject: Shoe ProblemWhat colour are my shoes? I cannot see them at the moment because of my desk.- Jim^D

Page 58: linux programming unit1-ppt

The vi Editor

Two modes Insert i Command <ESC>

Append a Replace character r, Replace word R ….. Deleting character x, Deleting line dd Exit

Goto command mode press :wq……

Page 59: linux programming unit1-ppt

Filters

The UNIX programs that read some input, perform a simple transformation on it and write some output.

grep, egrep, fgrep tr, dd, sort Sed, awk – programmable filters

Page 60: linux programming unit1-ppt

grep

grep options pattern format filename(s)

Some option -c Counting number of occurrences -n Line numbers along with lines grep Mamata –e mamata database grep [Mm]amata database

Page 61: linux programming unit1-ppt

grep : Regular Expressions

Character sets [mM] , [aeiou] , [a-zA-Z0-9]

Immediately preceeding character G*, [gG]*

Matching a single character 2… A four character pattern starting with 2 .* A number of characters or none

Page 62: linux programming unit1-ppt

grep : RE

Specifying pattern boundaries ^r pattern beginning

with expression r

^[^r] pattern not beginning with expression r

r$ pattern ending with expression r

c Any non-special character c matches

\c Turn off any special meaning of character c

^ Beginning of line

$ End of line

. Any single character

[…] Any one of character in …; ranges like a-z are legal

[^…] Any single character not in …; ranges are legal

r* Zero or more occurrences of r

r1r2 RE r1 followed by RE r2

Page 63: linux programming unit1-ppt

egrep : Regular Expressions

r+ : one or more occurrences of r r? : zero or more occurrences of r r1|r2 : r1 or r2 (r) : nested r

Page 64: linux programming unit1-ppt

fgrep

Searches for multiple patterns Does not accept regular expression Multiple patterns are separated by new

line character.

The disadvantage of grep family is that none of them has a separate facility to identify fields.

Page 65: linux programming unit1-ppt

sort

-f : eliminates distinction between uppercase and lowercase letters.

-n : numeric comparison - r : largest to smallest +m : comparison skips first m fields +0 : beginning of the line -u : discard duplicates

Page 66: linux programming unit1-ppt

comm

File comparison command Gives three columns of the output

Lines that occur only in file 1 Lines that occur only in file 2 Lines that occur in both

One or more columns can be suppressed Comm –12 f1 f2

Page 67: linux programming unit1-ppt

tr

Transliteration of character in the input tr a-z A-N

Mostly used for character conversion

Page 68: linux programming unit1-ppt

Assignment

1. Try all the UNIX commands. Store the output in a file appropriately using redirection operators.

2. Read a word from the terminal and check if the spelling is correct. Suggest few alternatives.

3. Create a file using Vi. Store few names in the file. Search all the names containing the letter M or m.

4. Create another file using cat command5. Compare both the files to find the differences6. Use calculator commands to compute 5

arithmetic expressions.