185
Shell Programming and Scripting Languages III Year IT –B

Shell Programming and Scripting Languages III Year IT –B

Embed Size (px)

Citation preview

Page 1: Shell Programming and Scripting Languages III Year IT –B

Shell Programmingand

Scripting Languages

III Year IT –B

Page 2: Shell Programming and Scripting Languages III Year IT –B

UNIT-IIntroduction to Unix: Architecture of Unix, Features of Unix , Unix utilities : process utilities, disk utilities, networking commands, text processing utilities and backup utilities.Introduction to Unix file system vi editor, file handling utilities, security by file permissions.

Page 3: Shell Programming and Scripting Languages III Year IT –B

Operating Systems -Basics

Page 4: Shell Programming and Scripting Languages III Year IT –B

4

Operating System

A program that controls the execution of application programs

An interface between applications and hardware

Different flavors of Operating Systems:

Single user , single tasking : DOS etc. Multi user and multi tasking : Windows , UNIX etc

Page 5: Shell Programming and Scripting Languages III Year IT –B

What is UNIX?

UNIX is an open operating system, developed by Bell labs in

1969

UNIX is a command line OS , also supports GUI

Simple and easy to use.

Page 6: Shell Programming and Scripting Languages III Year IT –B

Features of UNIX

Page 7: Shell Programming and Scripting Languages III Year IT –B

Why UNIX ?

PORTABLE.

MULTIUSER.

MULTITASKING.

NETWORKING.

ORGANIZED FILE SYSTEM

DEVICE INDEPENDENCE.

UTILITIES.

SERVICES.

Page 8: Shell Programming and Scripting Languages III Year IT –B

PORTABLE

UNIX is developed on C-LANGUAGE.

C-Programs can be easily moved from one hardware

environment to another.

It is relatively simple to port it to different environments

Page 9: Shell Programming and Scripting Languages III Year IT –B

MULTIUSERUNIX allows multiple users to concurrently share hardware and software.

The Unix resource sharing algorithm allows to share the

resources while at the same time preventing any one user from

locking out others.

Everyone gets an equal chance at the resources

Page 10: Shell Programming and Scripting Languages III Year IT –B

MULTITASKING

Multitasking is an Operating system feature that allows the user to run more than one job at a time.

In UNIX, a user can start a task, such as a C compilation, and

then move to another task, such as editing a file, without quitting the

first.

In fact more than one program can be running in the background

while a user is working in the foreground.

Page 11: Shell Programming and Scripting Languages III Year IT –B

NETWORKING NETWORKING allows users at one location to log into systems at other sites.

Users can gain access to remote systems, they operate just as

though they were on a system in their area.

They can execute any command of UNIX if they have

permissions.

Access to another system users a standard communication

protocol knows as transmission control protocol / internet

protocol (TCP/IP)

Page 12: Shell Programming and Scripting Languages III Year IT –B

ORGANIZED FILE SYSTEM &

DEVICE INDEPENDENCE

UNIX has a very organized file and directory system that allows users to organize and maintain files

UNIX treats input and output devices like ordinary files.

The input for a program can come from any device or file and

output for a program can go to any device or file.

Page 13: Shell Programming and Scripting Languages III Year IT –B

UTILITIES

Software developers have developed over 100 utilities that a user can use in a UNIX system and more are being developed continuously.

This library of utility programs provides the user with easily

used productivity tools readily available with short keyboard

commands.

EX: ls, sort,lp,mail ... etc

Page 14: Shell Programming and Scripting Languages III Year IT –B

SERVICES

●System Administrators monitor the system and help users when necessary.

●Support utilities are provided by Unix for system administrators to

access system resources such as disk and security access.

Page 15: Shell Programming and Scripting Languages III Year IT –B

Architecture of UNIX

Page 16: Shell Programming and Scripting Languages III Year IT –B

Architecture of Unix

UNIX is case sensitive

Shell

Utilities Application Programs

Kernel

User User User

Page 17: Shell Programming and Scripting Languages III Year IT –B

shell

Shell is the part of the unix that is most visible to the user.

To do anything we must give the shell a command.

If the command requires a utility, the shell requests that the kernel execute the utility.

If the command requires an application program, the shell requests that it be run.

Page 18: Shell Programming and Scripting Languages III Year IT –B

Two Major parts of shell

1) Command line interpreter: Reads your commands and

works with the kernel to execute them.

2) Programming capability: Allows you to write shell script.

A shell script is a file that contains SHELL COMMANDS that

perform a useful function

Page 19: Shell Programming and Scripting Languages III Year IT –B

Standard Shells in UNIX

Bourne shell C shell Korn Shell

Other shells: tcsh, bash

Page 20: Shell Programming and Scripting Languages III Year IT –B

Bourne shell

1) The default prompt for the Bourne shell is $ (or #, for the root

user).

2) Bourne shell devloped by steve bourne at the AT&T labs is

the oldest.

3) It has very good features for controlling input and output, but

is not well suited for the interactive user

Page 21: Shell Programming and Scripting Languages III Year IT –B

C shell

1) The C-shell developed in Berkley by BILL JOY.

2) A compatible version of C-shell is tcsh.

3) The command looks similar to C like statements.

4)The default prompt for the C shell is %.

5)It has job control, so that you can reattach a job running in the

background to the foreground.

Page 22: Shell Programming and Scripting Languages III Year IT –B

Korn shell

1) Korn shell, devloped by David korn,at AT & T labs.

2) korn shell is powerful because it is compatible with the

bourne shell.

3) Default prompt for korn shell is $

Page 23: Shell Programming and Scripting Languages III Year IT –B

KERNELApplications do not have direct access to the computer hardware.Applications have to request hardware access from a third-party that mediates all access to computer resources, the Kernel.

Page 24: Shell Programming and Scripting Languages III Year IT –B

UNIX Commands

Page 25: Shell Programming and Scripting Languages III Year IT –B

UNIX Command

A UNIX command is an action request given to the UNIX shell for execution.

Command syntax:

Verb [options] [arguments]

Verb is a name of the commandOptions is a command modifier preceded by + /-Arguments gives more information like file name

Page 26: Shell Programming and Scripting Languages III Year IT –B

UNIX commands

command Meaning

date Displays date and time

date -u Displays GMT date and time

cal Current month calendar

cal month year Displays specified year calendar

who Displays all users currently logged into system

who –u Gives details of each user

who am i Returns current user id

echo Displays message

Page 27: Shell Programming and Scripting Languages III Year IT –B

UNIX commands

command Meaning

passwd to change the password

man command name Displays online manual

clear Clears the screen

bc Binary calculator

vi filename Opens screen editor

mkdir dirname Creating a new directory

cd dirname Changes the current director to the specified director

Page 28: Shell Programming and Scripting Languages III Year IT –B

UNIX commands

command Meaning

cp source des Copies files/dir from source destination

mv s d Moves a file/dir from source to destination

rmdir dirname Removes empty directory

ls Lists the contents of directory

ls -l Long lists

ls -d Working directory

cat filename Creates a file

Page 29: Shell Programming and Scripting Languages III Year IT –B

PATH Path is an environment variable

echo $PATH

PATH will contain the paths of different shells such as korn, c, tcsh

etc.

PATH variable can be set in .profile or .bash_profile depending

upon the shell you are using.

If you want to set your compilers like c or java we set the path

variable in PATH variable.

Page 30: Shell Programming and Scripting Languages III Year IT –B

The path variable is used to search for a command directory.

Unix command uses the entries in the path variable to search for

the command under each directory in the PATH variable.

Example: The shell would look for the date command first in /bin

next /usr/bin finally current directory.

Page 31: Shell Programming and Scripting Languages III Year IT –B

Example

$ echo $PATH

/usr/local/bin:/usr/bin:/bin:/usr/games

$

Every path variable is seperated by a colon ( : ) .

Page 32: Shell Programming and Scripting Languages III Year IT –B

man

man - an interface to the on-line reference manuals

The man command displays online documentation.

You can quickly check the online manual and look up the

answer.

There is even a manual explanation for the man command

itself.

Page 33: Shell Programming and Scripting Languages III Year IT –B

1 Executable programs or shell commands

2 System calls (functions provided by the kernel)

3 Library calls (functions within program libraries)

4 Special files (usually found in /dev)

5 File formats and conventions eg /etc/passwd

6 Games

7 Miscellaneous (including macro packages and conventions),

e.g. man(7), groff(7)

8 System administration commands (usually only for root)

9 Kernel routines [Non standard]

Page 34: Shell Programming and Scripting Languages III Year IT –B

Example of manNAME date - print or set the system date and time

SYNOPSIS date [OPTION]... [+FORMAT] date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]

DESCRIPTION Display the current time in the given FORMAT, or set the system

date.

-d, --date=STRING display time described by STRING, not ‘now’

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

Page 35: Shell Programming and Scripting Languages III Year IT –B

AUTHOR Written by David MacKenzie.

REPORTING BUGS Report bugs to <[email protected]>.

COPYRIGHT Copyright © 2008 Free Software Foundation, Inc. License GPLv3+:

GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.

SEE ALSO The full documentation for date is maintained as a Texinfo manual. If the info and date programs are properly installed at your site, the command

info date

Page 36: Shell Programming and Scripting Languages III Year IT –B

Example man 2—system calls$man 2 open

NAME open, creat - open and possibly create a file or device

SYNOPSIS #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h>

int open(const char *pathname, int flags); int open(const char *pathname, int flags, mode_t mode);

int creat(const char *pathname, mode_t mode);

Page 37: Shell Programming and Scripting Languages III Year IT –B

man -k optionsearches the short descriptions of manual pages

-k – will display information including commands about the topic

Example : man -k command_topic

• sandeep@sandeep:~$ man -k unlink

• unlink (1) - call the unlink function to remove the specified file

• unlink (1posix) - call the unlink function

• unlink (3posix) - remove a directory entry

• unlinkat (2) - remove a directory entry relative to a directory file

descriptor

• sandeep@sandeep:~$

Page 38: Shell Programming and Scripting Languages III Year IT –B

man -f option

searches the manual pages referenced by option and print out the short descriptions of any found.

• sandeep@sandeep:~$ man -f man• man (1) - an interface to the on-line reference manuals• man (7) - macros to format man pages• man (1posix) - display system documentation• sandeep@sandeep:~$

Page 39: Shell Programming and Scripting Languages III Year IT –B

echo print message command

The echo command copies its arguments back to the terminal.

Syntax: echo -options arguments

Example:

1) echo Hello world

2) echo “Error 105: invalid number”

Page 40: Shell Programming and Scripting Languages III Year IT –B

echo -e option

-e enable interpretation of backslash escapes

\\ backslash

\a alert (BEL)

\b backspace

Page 41: Shell Programming and Scripting Languages III Year IT –B

\c suppress trailing newline

\f form feed

\n new line

\r carriage return

\t horizontal tab

\v vertical tab

Page 42: Shell Programming and Scripting Languages III Year IT –B

Examples

1) $ echo -e "printing backslash \\"

printing backslash \

2) $echo -e "ring the bell \a"

ring the bell

3) $echo -e "backspace\b e is removed in backspace"

backspac e is removed from backspace

4) $ echo -e " hello \n\n \c"

hello

Page 43: Shell Programming and Scripting Languages III Year IT –B

Examples sandeep@sandeep:~$ echo -e "hello\vx\vy\vz"hello x y zsandeep@sandeep:~$ echo -e "hello\tx\ty\tz"hello x y zsandeep@sandeep:~$

Page 44: Shell Programming and Scripting Languages III Year IT –B

printf

printf - format and print data

Syntax:

printf FORMAT [ARGUMENT]...

printf OPTION

Page 45: Shell Programming and Scripting Languages III Year IT –B

Printf FORMAT controls the output as in C

printf. Interpreted sequences are same

as echo -e.

%c --> character

%d --> integer

%f --> float

%s --> string

Page 46: Shell Programming and Scripting Languages III Year IT –B

Examples on printf

$printf "integer=%d \n float=%f \n string=%s \n char=%c\n"

$integer $float "$string" $charinteger=1 float=2.333333 string=welcome to printf char=csandeep@sandeep:~$

Page 47: Shell Programming and Scripting Languages III Year IT –B

Scriptrecord session command

Used to record an interactive session.

Script makes a typescript of everything printed

on your terminal.

To record a whole session, including logout

make it the first command of the session.

Syntax:

script -options arguments

Page 48: Shell Programming and Scripting Languages III Year IT –B

How to stop recording ?

exit

Page 49: Shell Programming and Scripting Languages III Year IT –B

arguments

-a --> to append the data to the exisisting file. -q --> be quiet -t --> Output timing data to standard error.

-t contains two fields1) How much time elapsed since the previous

output ?2) How many characters were output this time ?

Page 50: Shell Programming and Scripting Languages III Year IT –B

Example $ script scriptfile Script started, file is scriptfile

$ cat xhello welcome to unix programminfhow are you feeling

$ exitexitScript done, file is scriptfile-----------------------------------------------------------------------try other arguments

Page 51: Shell Programming and Scripting Languages III Year IT –B

Passwdused to chang your password

passwd - change user password

syntax

passwd [options] [LOGIN]

Page 52: Shell Programming and Scripting Languages III Year IT –B

Changes passwords for user accounts.

A normal user may only change the

password for his/her own account.

The superuser may change the password

for any account.

Page 53: Shell Programming and Scripting Languages III Year IT –B

options

-l --> Lock the password of the named account.

-m --Set the minimum number of days between

password changes to MIN_DAYS.

A value of zero for this field indicates that the user

may change his/her password at any time.

Page 54: Shell Programming and Scripting Languages III Year IT –B

-u --> Unlock the password of the named

account.

This option re-enables a password by changing

the password back to its previous value

-x --> Set the maximum number of days a

password remains valid.

After MAX_DAYS, the password is required to

be changed.

Page 55: Shell Programming and Scripting Languages III Year IT –B

-d --> Delete a user´s password (make it empty).

This is a quick way to disable a password for an

account. It will set the named account passwordless.

-w --> Set the number of days of warning before a

password change is required.

Number of days prior to the password expiring, that a

user will be warned that his/her password is about to

expire.

Page 56: Shell Programming and Scripting Languages III Year IT –B

As a general guideline, passwords should

consist of 6 to 8 characters

Including one or more characters from

each of the following sets:

1) lower case alphabetics

2) digits 0 thru 9

3) punctuation marks

Page 57: Shell Programming and Scripting Languages III Year IT –B

Examples on passwdsandeep@sandeep:~$ passwd sandeep

Changing password for sandeep.

(current) UNIX password:

Enter new UNIX password:

Retype new UNIX password:

Password unchanged

Enter new UNIX password:

Retype new UNIX password:

passwd: password updated successfully

sandeep@sandeep:~$

Page 58: Shell Programming and Scripting Languages III Year IT –B

Examples on passwd

$ passwd -d dummy

passwd: Permission denied.

$ su

Password:

# passwd -d dummy

Password changed.

#

Page 59: Shell Programming and Scripting Languages III Year IT –B

Examples on passwd

# passwd -l dummy

Password changed.

#

Page 60: Shell Programming and Scripting Languages III Year IT –B

# passwd -n 20 dummy

Password changed.

# passwd -S dummy

dummy P 06/23/2009 20 99999 7 -1

by default it warns before 7 days

# passwd -w 15 dummy

Password changed.

# passwd -S dummy

dummy P 06/23/2009 20 99999 15 -1

Page 61: Shell Programming and Scripting Languages III Year IT –B

Sttyset terminal command

The set terminal (stty) command sets / unsets

selected terminal input/output options.

To configure the terminal.

Syntax:

Stty options arguments

Page 62: Shell Programming and Scripting Languages III Year IT –B

options to stty

-a, --all

print all current settings in human-readable form

-g, --save

print all current settings in a stty-readable form.

Page 63: Shell Programming and Scripting Languages III Year IT –B

Examples$ stty -aspeed 38400 baud; rows 24; columns 80; line = 0;intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = M-^?; eol2 = M-^?;swtch = M-^?; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W;

$ stty -g6d02:5:4bf:8a3b:3:1c:7f:15:4:0:1:ff:11:13:1a:ff:12:f:17:16:ff:0:0:0:0:0:0:0:0:0:0:0:0:0:

0:0

Page 64: Shell Programming and Scripting Languages III Year IT –B

Set terminal without option or argument

Stty without options are arguments will

display the current common settings of

your terminal.

Network settings ( baud rate)

Control settings ( delete key)

Page 65: Shell Programming and Scripting Languages III Year IT –B

Example

$ sttyspeed 38400 baud; line = 0;eol = M-^?; eol2 = M-^?; swtch = M-^?;ixany iutf8

Page 66: Shell Programming and Scripting Languages III Year IT –B

Set terminal with arguments

Set erase and kill (ek)

Set terminal to general configuration(sane)

1) Set erase key(erase)

2) Set kill (kill)

3) Set interrupt key (intr)

Page 67: Shell Programming and Scripting Languages III Year IT –B

Set erase and kill(ek) ek argument sets the default erase

Ctl + h -> delete key

Ctl + c -> kill

Example:

$ character is deleted when you press ctl +h

$ line is killed when you press ctl + c

Page 68: Shell Programming and Scripting Languages III Year IT –B

Set terminal to general configuration(sane)

The sane argument sets the terminal

configuration to a reasonable setting that

can be used with a majority of terminals

Example:

$stty sane

Page 69: Shell Programming and Scripting Languages III Year IT –B

Set erase key (erase) Default erase key is ctl + h

It deletes the previous character typed.

We can reconfigure the keyboard to use another

key as the delete

Example:$ stty erasestty: missing argument to `erase'Try `stty --help' for more information.$ stty erase x$

Page 70: Shell Programming and Scripting Languages III Year IT –B

Set kill (kill)

The kill key deletes the whole line. Default is ctl + u

Example

$ stty kill ^b$ TYPE AND PRESS CTL + b

Page 71: Shell Programming and Scripting Languages III Year IT –B

Examples on stty

$stty -echoe switching off backspace$stty -echo switching off keyboard input display$stty intr ^c setting interrupt key to ctl +c

Page 72: Shell Programming and Scripting Languages III Year IT –B
Page 73: Shell Programming and Scripting Languages III Year IT –B

tty:tty: (Knowing your Terminal) (Knowing your Terminal)

Since Unix treats terminals as files, tty(teletype) command tells Since Unix treats terminals as files, tty(teletype) command tells you the file name of the terminal you are using. It is an obvious you the file name of the terminal you are using. It is an obvious reference to the device.reference to the device.

Example:Example: $tty$tty/dev/pts/10/dev/pts/10

• you can use tty in a shell script to control the behavior of the you can use tty in a shell script to control the behavior of the script depending on the terminal it is invoked from. If the script depending on the terminal it is invoked from. If the program must from only one specified terminal, the script logic program must from only one specified terminal, the script logic must use tty to make decision.must use tty to make decision.

Page 74: Shell Programming and Scripting Languages III Year IT –B

Uname uname - print system information -a, --all print all information, in the following order,

except omit -p and -i if unknown:

-s, --kernel-name print the kernel name

-n, --nodename print the network node hostname

-r, --kernel-release print the kernel release

Page 75: Shell Programming and Scripting Languages III Year IT –B

-v, --kernel-version print the kernel version

-m, --machine print the machine hardware name

-p, --processor print the processor type or "unknown"

-i, --hardware-platformprint the hardware platform or "unknown"

-o, -- operating-systeprint the operating system

Page 76: Shell Programming and Scripting Languages III Year IT –B

Examples for uname$ unameLinux

$ uname -sLinux

$ uname -aLinux sandeep 2.6.26-2-686 #1 SMP Thu Mar 26 01:08:11 UTC 2009 i686 GNU/Linux

$ uname -nsandeep

$ uname -r2.6.26-2-686

Page 77: Shell Programming and Scripting Languages III Year IT –B

$ uname -mi686

$ uname -punknown

$ unmae -ibash: unmae: command not found

$ uname -oGNU/Linux $ uname -v#1 SMP Thu Mar 26 01:08:11 UTC 2009

Page 78: Shell Programming and Scripting Languages III Year IT –B

Whowho is online

Displays all the users currently logged in

into the system

syntax:

who options am i

Page 79: Shell Programming and Scripting Languages III Year IT –B

Options

-b, --boot time of last system boot

-d, --dead print dead processes

-H, --heading print line of column headings

Page 80: Shell Programming and Scripting Languages III Year IT –B

Options -q, --count all login names and number of

users logged on

-r, --runlevel print current runlevel

-s, --short print only name, line, and time (default)

Page 81: Shell Programming and Scripting Languages III Year IT –B

Options

-u, --users list users logged in

-H, --heading print line of column headings

Page 82: Shell Programming and Scripting Languages III Year IT –B

Examples$ who am isandeep pts/1 2009-06-24 13:07 (:0.0)tells the current user who is logged in.

$ who -H | moreNAME LINE TIME COMMENTsandeep tty7 2009-06-24 09:51 (:0)

$ who -u | moresandeep tty7 2009-06-24 09:51 . 3290 (:0)sandeep pts/0 2009-06-24 11:23 02:08 6578 (:0.0)

shows idle time, process numbers

Page 83: Shell Programming and Scripting Languages III Year IT –B

Examples on who$ who -b --> boot system boot 2009-06-24 09:50

$ who -d -->dead processes 2009-06-24 09:50 1040 id=si term=0 exit=0 2009-06-24 09:50 2378 id=l2 term=0 exit=0

$ who -q --> users countsandeep sandeep sandeep# users=3

$ who -r run-level 2 2009-06-24 09:50 last=S

Page 84: Shell Programming and Scripting Languages III Year IT –B

date: (Display the System Date)date: (Display the System Date)

The UNIX system maintains an internal clock, you can The UNIX system maintains an internal clock, you can display the current date with the ‘date’ command, which display the current date with the ‘date’ command, which shows the date and time to the nearest second.shows the date and time to the nearest second.

Example:Example: $date$dateSat June 14 16:22:40 IST 2008Sat June 14 16:22:40 IST 2008

2) The command can also be used with suitable format 2) The command can also be used with suitable format specifiers as arguments. Each argument is preceded by the specifiers as arguments. Each argument is preceded by the + symbol, followed by the % operator and a single character + symbol, followed by the % operator and a single character describing the format.describing the format.

Page 85: Shell Programming and Scripting Languages III Year IT –B

Options Format Specifier Significance

+%d The day of the month

+%m The month

+%y The last two digits of the year

+%h The month name

+%H Hour

+%M Minute

+%S Second

+%D The date in format mm/dd/yy

+%T The time in format hh:mm:ss

Page 86: Shell Programming and Scripting Languages III Year IT –B

Examples

$date +%m$date +%m0606

$date +”%h %m”$date +”%h %m”Jun 06Jun 06

$date “+this is the date %B”$date “+this is the date %B”this is the date junethis is the date june

explore more options in labexplore more options in lab

Page 87: Shell Programming and Scripting Languages III Year IT –B

lpTo print a file/spool a file to print.To print a file/spool a file to print.

Example: Example: $lp a.lst$lp a.lstrequest id is prl-320 (1 file)request id is prl-320 (1 file)$_$_

Options:Options:-d<printer name> <file name> To specify printer name. -d<printer name> <file name> To specify printer name.

-t”tile” <file name> -t”tile” <file name> To specify page title. To specify page title. -m -m To notify user after the file has been printed. To notify user after the file has been printed. -n<number> -n<number> To specify number of copies. To specify number of copies.

Example:Example: $lp –dlaser a.list$lp –dlaser a.list$lp –t”first chapter” a.txt$lp –t”first chapter” a.txt$lp –n3 –m a.lst$lp –n3 –m a.lst

Page 88: Shell Programming and Scripting Languages III Year IT –B

MORE More allows us to set the output page size and pauses at the end

of each page to allow us to read the file.

After each page we may request one or more lines, a new page, or

quit.

Syntax:

$more options input_files

b / ^b ---- move back to previous screen

space – display next screen of output.

Enter (return) – Advances one line.

d --- Displays half a screen.

Page 89: Shell Programming and Scripting Languages III Year IT –B

options

-d --> prompts the user with the message "[Press space to continue, ’q’

to quit.]" and will display "[Press ’h’ for instructions.]

-l --> Ignores form feed character, unix treats ^L as form feed, pause after any

line that contains a form feed.

-f Causes more to count logical, rather than screen lines (i.e., long

lines are not folded).

DO NOT SCROLL

-p clear the whole screen and then display

the text.

-c paint each screen from the top, clearing the remainder of each line as

it is displayed.

Page 90: Shell Programming and Scripting Languages III Year IT –B

Options of more-s Squeeze multiple blank lines into one.

-u Suppress underlining.

-w waits at the end of the output for the user to enter any key to continue.

Most of these options dont work properly with pipe

Example:

$man more | more $ more -f x$more file1 file2

Explore all the options in lab.

Page 91: Shell Programming and Scripting Languages III Year IT –B

chmod

Used to change the permissions of a file or directory.

By default for a directory the permissions while creation are 777, for

file 666

Thes values while creation are subtracted from umask value.

The default umask value is 022

Page 92: Shell Programming and Scripting Languages III Year IT –B

Changing permissions with chmod

There are two ways to change the permissions.

1) Symbolic : A user can understand and can change.

2) Octal : A computer can understand much faster than user.

Page 93: Shell Programming and Scripting Languages III Year IT –B

9 - permissions$ls -l-rw-r--r-- 1 sandeep sandeep 20 2009-06-26 13:08 a

shows the permission columnrw-r--r--rw- --> user permissionsr-- --> Group permissions.r-- --> Other permissions

User permissions : Permissions for the person who created the file.Group permissions : Permissions for the group, user/root has given

accessOther permissions : Permissions for Other groups or users who can

access the file

r --> readw --> writex --> Execute

Page 94: Shell Programming and Scripting Languages III Year IT –B

Chmod : Symbolic Assignment operator ( = )

Adding permissions ( + )

Removing permissions ( - )

User is represented as ( u )

Group is represented as ( g )

Others are represented as ( o )

For giving permissions to all we use ( a )

Read permission is represented as ( r )

Write permission is represented as ( w )

Execute permission is represented as ( x )

Page 95: Shell Programming and Scripting Languages III Year IT –B

Examples: Assignment operator$ ls -l file

---xr--r-- 1 sandeep sandeep 20 2009-06-26 13:08 file

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

$ chmod u=rw file --> change user permission to read and write

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

$ ls -l file --> analyze the output

-rw-r--r-- 1 sandeep sandeep 20 2009-06-26 13:08 a

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

$ chmod g=rw file --> change group permission to read write

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

$ ls -l file

-rw-rw-r-- 1 sandeep sandeep 20 2009-06-26 13:08 a

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

$ chmod o=x file

Page 96: Shell Programming and Scripting Languages III Year IT –B

Examples: Adding permissions$ ls -l file-rw-rw---x 1 sandeep sandeep 20 2009-06-26 13:08 file

$ chmod u+x file

$ ls -l file-rwxrw---x 1 sandeep sandeep 20 2009-06-26 13:08 file

$ chmod u+x,g+x,o+r file

$ ls -l file-rwxrwxr-x 1 sandeep sandeep 20 2009-06-26 13:08 file

Page 97: Shell Programming and Scripting Languages III Year IT –B

cd There are no options for change directory.

The pathname can be relative or absolute.

If there is no pathname argument the default is the home directory.

cd ~ --> tilde symbol can also be used to change to home directory.

cd .. --> change directory to the parent directory.

cd . --> change directory to current directory

cd / --> change directory to root.

If you dont have Execute permission on a directory you cant

change.

Page 98: Shell Programming and Scripting Languages III Year IT –B

Example sandeep@sandeep:~/sandeep$ cd BegLinuxProgrammers/

bash: cd: BegLinuxProgrammers/: Permission denied

sandeep@sandeep:~/sandeep$ chmod 277 BegLinuxProgrammers/

sandeep@sandeep:~/sandeep$ cd BegLinuxProgrammers/

bash: cd: BegLinuxProgrammers/: Permission denied

sandeep@sandeep:~/sandeep$ chmod 177 BegLinuxProgrammers/

sandeep@sandeep:~/sandeep$ cd BegLinuxProgrammers/

sandeep@sandeep:~/sandeep/BegLinuxProgrammers$

Page 99: Shell Programming and Scripting Languages III Year IT –B

pwd print name of current/working directory.

$pwd

• Displays absolute path name of your current directory.Displays absolute path name of your current directory.

Example:Example:

$pwd$pwd

/home/kumar/home/kumar

sandeep@sandeep:~$ sandeep@sandeep:~$ cd sandeep/cd sandeep/

sandeep@sandeep:~/sandeep$ sandeep@sandeep:~/sandeep$ cd BegLinuxProgrammers/cd BegLinuxProgrammers/

sandeep@sandeep:~/sandeep/BegLinuxProgrammers$ sandeep@sandeep:~/sandeep/BegLinuxProgrammers$ pwdpwd

/home/sandeep/sandeep/BegLinuxProgrammers/home/sandeep/sandeep/BegLinuxProgrammers

Page 100: Shell Programming and Scripting Languages III Year IT –B

cp Copy files and directories

Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.

Creates a DUPLICATE of a file.

Syntax:

$cp options sources destination

options

-i --> Interactive prompt

-r --> recursive (copy subdirectories )

-p --> Preserve time and permissions

Page 101: Shell Programming and Scripting Languages III Year IT –B

Rules to copy a file successfully The source file must exist. Otherwise, unix prints the following error

message.

cp: cannot stat `filesee': No such file or directory

If no destination path is specified, UNIX assumes the destination is

the current directory.

If destination file doesnt exist it is created; if it does exist it is

replaced.

If the source is a multiple files or a directory the destination must be

a directory.

To prevent automatic replacement of the destination file, use the

interaction ( -i ) option.

Page 102: Shell Programming and Scripting Languages III Year IT –B

To preserve the modification and file access times use the preserve

( -p ) option.

Simple file copy:

$ cat >file1adcfad^C$ cp file1 file2

Page 103: Shell Programming and Scripting Languages III Year IT –B

Copy and replace filesandeep@sandeep:~/sandeep$ cat >file1Contents file 1 (one )These are the contents which are going to be replaced in file 2^C

sandeep@sandeep:~/sandeep$ cat > file2Contents of file 2 ( Two )These are the contents which are going tobe erased and file 1 contents are replaced.^C

sandeep@sandeep:~/sandeep$ cp file1 file2sandeep@sandeep:~/sandeep$ cat file2Contents file 1 (one )These are the contents which are going to be replaced in file 2

Page 104: Shell Programming and Scripting Languages III Year IT –B
Page 105: Shell Programming and Scripting Languages III Year IT –B

Path locating commands There are two path locating commands.

• Which

• Type

Example:

$ which ls

/bin/ls

$ type man

man is hashed (/usr/bin/man)

$

Page 106: Shell Programming and Scripting Languages III Year IT –B

Help

For any command you can get help.

$command -- help

Page 107: Shell Programming and Scripting Languages III Year IT –B

cat command Catenate command or cat for short.

It combines one or more files by appending them in the order they are

listed in the command.

The input can come from the keyboard or file and output goes to the

monitor or file.

Syntax:

cat options input_files

Page 108: Shell Programming and Scripting Languages III Year IT –B

Options of cat command -e ----> print $ at the end of the line.

-n ----> number lines

-s ---->silent ( no error message )

-t ----->print tabs and form feeds.

-u ----->unbuffered output.

-v -----> print control characters

Page 109: Shell Programming and Scripting Languages III Year IT –B

Using cat to display a file Cat is useful to display a file.

When only one input is provided, the file is catenated with a null file.

Result is that input file is displayed on the screen ( monitor).

They are no automatic pauses at the end of the screen. ( Ex : In more

command we have pauses )

If we accidentally cat a binary file to the monitor, all types of strange

characters and sounds result.

File should be a text file for display.

Page 110: Shell Programming and Scripting Languages III Year IT –B

Example to display a file with cat

srinivas@srinivas:~/srinivas$ cat a

this is cat file

name of the file is a

this content is getting appended

press control + d key for end of file

srinivas@srinivas:~/srinivas$

Page 111: Shell Programming and Scripting Languages III Year IT –B

Creating file with cat To create or append contents to a file we need to use redirection

opertators. ( > , >>)

To tell cat end of file use ( ctl +d ) key in unix it is end of file.

stty command tells the interrupt key for end of file.

Example

$cat > a

this is my first file.

This contains the contents for file “a” .

ctl+d ( press these two keys at a time on keyboard )

$

Page 112: Shell Programming and Scripting Languages III Year IT –B

Appending contents with cat If you want to append contents at the end of file use >> double redirection.

>> with cat will append contents at the end of the file, if file is not specified

then contents will be written to the monitor.

Example:

srinivas@srinivas:~/srinivas$ cat >>a -- append contents to file a

this content is getting appended`

press control + d key for end of file

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

srinivas@srinivas:~/srinivas$ cat a ---to see contents of a

this is cat file

name of the file is a

this content is getting appended

press control + d key for end of file

Page 113: Shell Programming and Scripting Languages III Year IT –B

Basic cat commandsrinivas@srinivas:~/srinivas$ cat a b c

this is cat file

name of the file is a

this is cat file b

It contains the contents of b

this is cat file c

It contains the contents of c

srinivas@srinivas:~/srinivas$

in the above we are catenating a b c files and displaying them on screen.

Page 114: Shell Programming and Scripting Languages III Year IT –B

Options can be grouped Options are grouped into 4 categories.

1) Visual characters.

2) Buffered output.

3) Missing files.

4) Numbered lines.

Page 115: Shell Programming and Scripting Languages III Year IT –B

Visual characters ( -v and -e) If we want to see unprintable characters, such as ASCII control characters.

Such characters are not displayed by cat.

-v option is used to display, allows us to see control characters.

If we use option -ve will display dollar at end of each file.

If we use option -vt the tabs will appear as ^I.

Page 116: Shell Programming and Scripting Languages III Year IT –B

Examples on visual characterscat -v example -- > to display unprintable characterscat -ve example ---> to display $ at the endcat -vt example ---> to display tabs a ^Icat -vet example ---> two display tabs as ^I and $ at end

srinivas@srinivas:~/srinivas$ cat -v athis is a file with ASCII control characterspageup ^[[5~pagedown ^[[6~home ^[OHend^[OF

srinivas@srinivas:~/srinivas$ cat -v athis is a file with ASCII control characterspageup ^[[5~pagedown ^[[6~home ^[OHend^[OFsrinivas@srinivas:~/srinivas$

Page 117: Shell Programming and Scripting Languages III Year IT –B

Buffered output When output is buffered, it is kept in the computer until the system has time

to write it to a file.

Normally cat output is buffered.

To have unbuffered write, use -u option.

Using these option -u may slow down your system (not recommended)

Example:

$cat -u file

Page 118: Shell Programming and Scripting Languages III Year IT –B

Missing files While concatenating several files together if one of them is missing, the

systems displays the message such as

Cannot open x.dat : no such file or directory.

You can specify cat is to be silent by using -s option.

Example:

$cat -s x.dat a

Page 119: Shell Programming and Scripting Languages III Year IT –B

Numbered linessrinivas@srinivas:~/srinivas$ cat > a

hello

are you there

srinivas@srinivas:~/srinivas$ cat > b

Hi

yes I am here

srinivas@srinivas:~/srinivas$ cat -n a b

1 hello

2 are you there

3 Hi

4 yes I am here

Page 120: Shell Programming and Scripting Languages III Year IT –B

ls The ls commands lists the contents in a directory.

Depending upon the options used it can list files, directories, or sub

directories.

Syntax :

ls options pathname

Page 121: Shell Programming and Scripting Languages III Year IT –B

options-l ---------- long list-d --------- working directory.-n --------- user / group ids-r ---------- reverse order-t ---------- time sequence-u --------- time of last access-c --------- i-node creation time-p --------- identify directories-R -------- Recursive-i ---------- Print i-node-1 --------- print one column

Page 122: Shell Programming and Scripting Languages III Year IT –B

ls with no arguments Lists all directories and files in present working directory in columns.

Example:

srinivas@srinivas:~/srinivas$ ls

a BegLinuxProgrammers example y

b c x z

srinivas@srinivas:~/srinivas$

Page 123: Shell Programming and Scripting Languages III Year IT –B

-l option of ls -l → will list the file in long list formatExample:srinivas@srinivas:~/srinivas$ ls -ltotal 20-rw-r--r-- 1 srinivas srinivas 20 2009-06-26 13:08 a-rw-r--r-- 1 srinivas srinivas 17 2009-06-26 13:08 b-rw-r--r-- 1 srinivas srinivas 49 2009-06-26 11:19 cdrwxr-xr-x 2 srinivas srinivas 4096 2009-06-26 14:30 sample

Examination of output:

d rwxr-xr-x 2 srinivas srinivas 4096 2009-06-26 14:30 sample

Page 124: Shell Programming and Scripting Languages III Year IT –B

First field Is type of the file.

- → Regular file

d → directory

c → character special file

b → Block special file

l → symbolic link.

P → FIFO

s → socket.

Second field is permissions

rwx → for user (first three)

rwx → for group ( second three)

rwx → for others ( last three )

There are totally 9 permissions rwxrwxrwx

Page 125: Shell Programming and Scripting Languages III Year IT –B

Third field specifies the links

Directory the default no. of links is two.

Regular file and others the default link count is 1.

For symbolic link the link count is one.

Fourth and fifth columns specify the username and Group to which they

belong.

6th field tells File size of directory / file

7th field specifies the date and time.

8th field specifies the name of the file

Page 126: Shell Programming and Scripting Languages III Year IT –B

Hidden files listing (-a) All hidden files in unix start with . (dot)

To list the hidden files with ls use -a option.

Example:

srinivas@srinivas:~/srinivas$ ls -a

. .. a b BegLinuxProgrammers c example .file2 .hiddenfile sample x y z

srinivas@srinivas:~/srinivas$

Page 127: Shell Programming and Scripting Languages III Year IT –B

Displaying the working directory name ( -d )

-d option displays working directory name.

Used with -l option displays attributes.

Example:

srinivas@srinivas:~/srinivas$ ls -ld

drwxr-xr-x 4 srinivas srinivas 4096 2009-06-26 14:49 .

Page 128: Shell Programming and Scripting Languages III Year IT –B

Displaying user and group id ( -n ) -n is same as l with a little difference, it displays user and group id instead

of there names.

Example:

srinivas@srinivas:~/srinivas$ ls -n

total 24

-rw-r--r-- 1 1000 1000 20 2009-06-26 13:08 a

-rw-r--r-- 1 1000 1000 17 2009-06-26 13:08 b

Page 129: Shell Programming and Scripting Languages III Year IT –B

Reverse order ( -r ) -r → sorts the display in descending order.

By default ls sorts in ascending order.

Example:

srinivas@srinivas:~/srinivas$ ls -r

z y x sample example c BegLinuxProgrammers b a

srinivas@srinivas:~/srinivas$

Page 130: Shell Programming and Scripting Languages III Year IT –B

Time sorts (-t) There are three time sorts

1) Basic time sort ( -lt ) : sorts by time stamp.

2) Last access ( -lu ): sorts by last access

3) Inode change ( -lc ): sorts by i-node change

Example:

srinivas@srinivas:~/srinivas$ ls -lt

total 24

drwxr-xr-x 2 srinivas srinivas 4096 2009-06-26 14:30 sample

-rw-r--r-- 1 srinivas srinivas 17 2009-06-26 13:08 b

-rw-r--r-- 1 srinivas srinivas 20 2009-06-26 13:08 a

Page 131: Shell Programming and Scripting Languages III Year IT –B

Identify directories (-p) -p option appends each directory with a slash at the end for a directory.

Example:

srinivas@srinivas:~/srinivas$ ls -p

a b BegLinuxProgrammers/ c example sample/ x y z

srinivas@srinivas:~/srinivas$

Page 132: Shell Programming and Scripting Languages III Year IT –B

Recursive list ( -R ) To list all the files in the current directory, and subdirectories and

subdirectories until all the directories are listed.

Example

srinivas@srinivas:~/srinivas$ ls -R

.:

a b BegLinuxProgrammers c example sample x y z

./BegLinuxProgrammers:

a a.out b c copy_system_block_by_block.c copy_system.c out

./sample:

srinivas@srinivas:~/srinivas$

Page 133: Shell Programming and Scripting Languages III Year IT –B

Print one column ( -1 ) To print filenames as columns we use -1 option

Example:

srinivas@srinivas:~/srinivas$ ls -1

a

b

BegLinuxProgrammers

c

example

sample

x

y

z

srinivas@srinivas:~/srinivas$

Page 134: Shell Programming and Scripting Languages III Year IT –B

Mkdir (making directories)

Mkdir command is used to create new directories.

Syntax:

mkdir options directory_name

options

-m -> mode

-p -> Parent

Page 135: Shell Programming and Scripting Languages III Year IT –B

Examplessandeep@sandeep:~/sandeep$ mkdir Class

sandeep@sandeep:~/sandeep$ ls

a b BegLinuxProgrammers c Class example sample x y z

sandeep@sandeep:~/sandeep$ mkdir -m 777 SecondYearCSE

sandeep@sandeep:~/sandeep$ ls -ld SecondYearCSE/

drwxrwxrwx 2 sandeep sandeep 4096 2009-06-26 15:48 SecondYearCSE/

$ mkdir -p First/Second/Third (To create Recursive Directories )

$ mkdir -p Current Current2 ( To Create Multiple Dir in current Dir)

$ls -R

check the output in lab

Page 136: Shell Programming and Scripting Languages III Year IT –B

rmdir (remove Empty directory)

rmdir is used to delete directories.

The directory should be empty to delete.

You need to be in the parent directory, to delete.

Syntax:

rmdir options directory_name

options:

-p --> to delete the subdirectories.

Page 137: Shell Programming and Scripting Languages III Year IT –B

Examples

$ mkdir -p First/Second/Thirdsandeep@sandeep:~/sandeep/SecondYearCSE$ cd First/sandeep@sandeep:~/sandeep/SecondYearCSE/First$ touch a b csandeep@sandeep:~/sandeep/SecondYearCSE/First$cd ..sandeep@sandeep:~/sandeep/SecondYearCSE$ mkdir Current

-------------------------------------------------------------------------------------$ rmdir -p First/rmdir: failed to remove `First/': Directory not empty---------------------------------------------------------------------------------sandeep@sandeep:~/sandeep/SecondYearCSE$ rmdir Currentsandeep@sandeep:~/sandeep/SecondYearCSE$ ---------------------------------------------------------------------------------$ rmdir -p First/Second/Third/sandeep@sandeep:~/sandeep/SecondYearCSE$ -------------------------------------------------------------------------------

Page 138: Shell Programming and Scripting Languages III Year IT –B

rm (remove files and directories)

Page 139: Shell Programming and Scripting Languages III Year IT –B

Unix Utilities

139

Page 140: Shell Programming and Scripting Languages III Year IT –B

• Process utilities

• Disk utilities

• Networking commands

• Backup utilities

• Text processing utilities

140

Unix Utilities

Page 141: Shell Programming and Scripting Languages III Year IT –B

Process utilities

Ps (process status) command: Display some process attributes.Example:$ ps

PID TTY TIME CMD1078 Pts/2 0:00 bash

141

Page 142: Shell Programming and Scripting Languages III Year IT –B

Process utilitiesThe following options are used with ps command

142

Option Description

-f Displays full information about process

-xShows information about processes without terminals.

-u Shows additional information like -f option.

-e Display extended information (system process)

Example:

$ ps -fUID PID PPID C STIME TTY TIME CMD2684 2594 2590 0 09:54 pts/0 00:00:00 -bash2684 3847 2594 0 10:48 pts/0 00:00:00 ps -f

Page 143: Shell Programming and Scripting Languages III Year IT –B

143

Column Description

UID User ID that this process belongs to (the person running it).

PID Process ID.

PPID Parent process ID (the ID of the process that started it).

C CPU utilization of process.

STIME Process start time.

TTY Terminal type associated with the process

TIME CPU time taken by the process.

CMD The command that started this process.

The following information is displayed by ps command

Page 144: Shell Programming and Scripting Languages III Year IT –B

Kill command: kill command is used to kill (stop) the process as follows:

Syntax: Kill PID Ex: $ kill 2594 top command : displays the processes information in sorted order

144

Page 145: Shell Programming and Scripting Languages III Year IT –B

Networking commandsftp: file transfer protocolWith this we can upload and download files from one computer to anotherSyntax: $ ftp host name or ip-address

ping : The ping command sends an echo request to a host available on the network. Using this

command you can check if your remote host is responding well or not.Syntax: $ ping host name or ip-address

145

Page 146: Shell Programming and Scripting Languages III Year IT –B

telnet: Telnet is a utility that allows a computer user at one site to make a connection, login and then conduct work on a computer at another site.

$telnet host name or ip-address

finger: The finger command displays information about users on a given

host. The host can be either local or remote. $finger

Check all the logged in users on local machine $finger hostname or ip_address

Check all the logged in users on remote machine

146

Page 147: Shell Programming and Scripting Languages III Year IT –B

Backup utilitiestar: the tape archive program

Tar doesn’t normally write to the standard output but creates an archive in the media.

Tar accepts file and directory names as arguments.

1.Creating an archive using tar with cvf option: syntax: tar -cvf archive_name.tar dirname/ c – create a new archive v – verbosely list files which are processed. f – following is the archive file name

147

Page 148: Shell Programming and Scripting Languages III Year IT –B

2.Creating a tar gzipped archive using option cvzf

Syntax : tar -cvzf archive_name.tar.gz dirname/

3.Extracting (untar) an archive using tar command

Syntax: tar -xvf archive_name.tar

148

Page 149: Shell Programming and Scripting Languages III Year IT –B

cpio: copy input-output

Cpio command copies files to and from a backup device. It uses standard input to take the list of filenames.

Cpio can be used with redirection and piping.

Cpio uses two options-o (output) and –i (input) either of which must be there in the command line.

$ cpio -oacv > backupfile

-a Reset the access times of files after reading them to the initial value. -c  The c option tells cpio to use the ASCII header format. -v The v option results in verbose output

149

Page 150: Shell Programming and Scripting Languages III Year IT –B

Disk utilitiesDu: disk usage Du estimate the file space usage on the disk. It produces a list containing the usage of each subdirectory of its

argument and finally produces a summary. $du /home/usr1

df: displays the amount of free space available on the disk. The output displays for each file system separately.

$ df

150

Page 151: Shell Programming and Scripting Languages III Year IT –B

Introduction to UNIX File System and File Handling

Utilities

Page 152: Shell Programming and Scripting Languages III Year IT –B

The UNIX file system

bin lib dev etc tmp home unix

user 1 user 2 user 3All the above mentioned directories are present on almost all

UNIX installations.

These directories and their purpose are given below:

bin – Binary executable files

lib – Library functions

dev – Devices related files

etc – Binary executable files usually required for system administration

tmp – Temporary files created by UNIX or users

home – contains directories of all users

unix – UNIX kernel related files.

Page 153: Shell Programming and Scripting Languages III Year IT –B

File: The file is a collection of information that is assigned a name and

is stored on a secondary storage medium like disk or magnetic tape.

All versions of the UNIX recognize three types of files:

Ordinary files. This type of file is used to store the data. Directory files. A directory file contains a list of files. Each entry

in the list consists of two parts (file name and a pointer to the actual file on the disk.

Special files. These files are used to reference physical devices, such as terminals, printers, disks, and tape drives.

Page 154: Shell Programming and Scripting Languages III Year IT –B

File Name:

A filename can be any sequence of ASCII characters. Never start your file name with a period. (Which

indicates that is hidden) Do not use special characters like > and < .

Page 155: Shell Programming and Scripting Languages III Year IT –B

Directories

Files are organized by grouping them into directories. There are four special directories supported by UNIX Root Directory : A top of the directory structure. Home Directory: The directory which holds all user created

directories. Working Directory: Current directory. Parent directory: Is immediately above the working directory.

Page 156: Shell Programming and Scripting Languages III Year IT –B

pwd :: prints the current working directoryLogin: example

$ pwd/home/example$ _

/

homebin

example new

Page 157: Shell Programming and Scripting Languages III Year IT –B

Absolute and Relative pathnames Absolute pathname: A pathname is a sequence of

directory names that leads you through the hierarchy from a starting directory to a target file, called ‘absolute pathname’.Eg: /home/example/file1

Relative pathname: A pathname is a sequence of directory names that leads you through its current working directory to a target file, called ‘relative pathname’.It uses following fields:

Home directory: ~ current directory: .

parent directory: ..Eg’s: ../cse007/file2.c, file1.c, ../../bin/myfile, ./dir1/dfile.c

Page 158: Shell Programming and Scripting Languages III Year IT –B

cat – concatenate and print a file/files

$ cat > filename9………………… type your data here^d$ $ cat filename9………………… display the content of filename9$

Page 159: Shell Programming and Scripting Languages III Year IT –B

cp – copy an ordinary file cp command allows you to create a duplicate copy of an

ordinary file.$ cp srcfile destfile Here srcfile is the original source file and destfile is the name of the copy to be created.

$ cat file1unix is an operating system.$ cat file2invented by kenthompson.

$ cat file2$ unix is an operating system

-i interactive copying

Page 160: Shell Programming and Scripting Languages III Year IT –B

rm – remove an ordinary fileRemoves one or more ordinary files from a directory, effectively erasing the file

$ rm filename……..where each filename is separated by a white space.$ lsfile1 file2 file3$ rm file1 file2$ lsfile3$

Page 161: Shell Programming and Scripting Languages III Year IT –B

rm options rm file1file2 file3 file4

removes file1file2 file3 file4 in current directories if exists. rm new/file1 old/file2 rm * deletes all files -r or –R recursive deletion -f forcing removal -rf deletes every thing in the current directory and

below

Page 162: Shell Programming and Scripting Languages III Year IT –B

mv – move ( rename ) a file

The mv command changes the name associated with a file by associating a new file name.$ mv oldname newname This command line changes the name of the file from ‘oldname’ to ‘newname’.

Page 163: Shell Programming and Scripting Languages III Year IT –B

ls – listing the directory contents

To obtain the list of the all file names in the current directory.$ ls 560.cTOC.c XYZ.txta.out temp.bak$ ls directorynameLists the content of given directory name

Page 164: Shell Programming and Scripting Languages III Year IT –B

ls -options -x : output in multiple columns

ls –x560.c TOC.c XYZ.txt a.out

temp.bak -F : identifying Directories and Executables

ls –Fx560.c* TOC.c* XYZ.txt a.out* temp.bak new/ old/‘*’ indicates executable code and ‘/’ refers to directory.

-a : show hidden files alsols –axF. / . . / .exrc .kshrc.profile .xinitrc .sh_hostory 560.c*TOC.c* XYZ.txt a.out/ old/new/

Page 165: Shell Programming and Scripting Languages III Year IT –B

ls -options-R Recursive list-r Sorts the filenames in reverse order (based

ASCII)-d ‘dirname’ lists only ‘dirname’ if ‘dirname’ is a

directory-t sorts file names by last modification time-lt sorts listing by last modification time -u sorts filenames by last access time-lu sorts listing by last access time -I displays longlist

Page 166: Shell Programming and Scripting Languages III Year IT –B

ls with longlist option

$ ls -l : which prints following information about your files and directories

File type : directory / other Permissions : read /write/execute Links Owner id group id File size Last modification time filename

Page 167: Shell Programming and Scripting Languages III Year IT –B

ln – Creating filename Aliases The ‘ln’ command is used for establishing additional links

for the same physical file.

$ ln oldname newnameBoth ‘oldname’ and ‘newname’ refer to the same file.$ lsletter poem1$ ln poem1 versa$ lsletter poem1 versa$ cat poem1 or cat versa generates the same result

Page 168: Shell Programming and Scripting Languages III Year IT –B

Wildcards:

A wildcard is a token that specifies that one or more different characters can be used to satisfy a specific request.

Three wildcards in UNIX: ? ( single character) [ …] ( Set of characters)

* ( Zero or more characters)

Page 169: Shell Programming and Scripting Languages III Year IT –B

The lp subsystem: printing a file lp line printing command provides spooling facility. lp file1.c

request id is prl -11 ( 1 file) lp –d printername filename

uses a printer specified as printername to print filename. lp –t “title” filename

prints a title as “title” on the first page. lp –n3 file2.c

prints three copies .

Page 170: Shell Programming and Scripting Languages III Year IT –B

file : Knowing the file types

UNIX provides the file command to determine the type of file.

file filenamefilename : file type

file *lists all file names and their types.

Page 171: Shell Programming and Scripting Languages III Year IT –B

wc :Counting lines, words, and characters wc ‘filename’

no of lines no of words no of chars ‘filename’ A line is any group of characters not containing a newline. A word is a group of characters not containing a space, tab, or newline. A character is the smallest unit of information, and includes a space,

tab, and newline. -l counts number of lines -w counts number of words -c counts number of characters wc file1 file2 file2 …………… lines in file1 words in file1 char’s in file1 file1

lines in file2 words in file2 char’s in file2 file2 lines in file3 words in file3 char’s in file3 file3

Page 172: Shell Programming and Scripting Languages III Year IT –B

Directory related utilities pwd : Prints current working directory. mkdir : Creates directory/directories.

$ mkdir dir or mkdir dir1 dir2 dir3

$ mkdir /books/thompson/Ritchie

rmdir : removing directory / directories$ rmdir directoryname/names$ rmdir –p /books/thompson/RitchieRemoves all the directories. Note: If the directory name is empty then it removed.

cd: change directory, uses either absolute path or relative path.cd “pathname”

Page 173: Shell Programming and Scripting Languages III Year IT –B
Page 174: Shell Programming and Scripting Languages III Year IT –B

In In Unix file system, A file will have a number of attributes (Unix file system, A file will have a number of attributes (PropertiesProperties) ) that are stored in the Inode Table.that are stored in the Inode Table.

ls command looks up the file’s Inode Table to fetch its attributes.ls command looks up the file’s Inode Table to fetch its attributes.

ls ls –l command gives detailed listing of all seven fields of attributes of a l command gives detailed listing of all seven fields of attributes of a file.file.

Example:Example:

$$ls –lls –l Total 72 Total 72 ------ (Total Bytes occupied by the files, 72 Blocks each ------ (Total Bytes occupied by the files, 72 Blocks each

of 512B)of 512B)_rw_r_ _r_ _ 1 kumar metal 191514 May 10 13:45 chap1_rw_r_ _r_ _ 1 kumar metal 4174 May 10 15:15 chap2drw_r_ _r_ _ 2 kumar metal 572 Apr 09 10:31 prgs_rwxr_ _r_ _ 1 kumar metal 1514 Apr 10 17:45

add.sh_rw_rw_r_ _ 1 kumar metal 10214 Apr 11 12:04 sal.txt

File SecurityFile Security

Page 175: Shell Programming and Scripting Languages III Year IT –B

Fields:Fields: File Type & Permissions: File Type & Permissions:

First character indicates file type. [ _ First character indicates file type. [ _ ordinary ordinary filefile, d - , d - DirectoryDirectory]]

Next series of characters that take the values Next series of characters that take the values r, r, w, x.w, x.

r – r – readread w- w- writewrite x- x- executeexecute

Links:Links:

The second column indicates the number of The second column indicates the number of links associated with a file.links associated with a file.

Ownership:Ownership:

Shows owner of all the files and sub-directories.Shows owner of all the files and sub-directories.

Group Ownership:Group Ownership: When opening a user account, the When opening a user account, the

administrator also assigns the user to same administrator also assigns the user to same group.group.

All the group members have privileges on same file.All the group members have privileges on same file.

File size:File size:

Shows file size in bytes.Shows file size in bytes.

Page 176: Shell Programming and Scripting Languages III Year IT –B

Last modification time:Last modification time:

The 6The 6thth , 7 , 7thth and 8 and 8thth columns indicates last columns indicates last modification date of file.modification date of file.

File name:File name:

Names are arranged in ASCII collating Names are arranged in ASCII collating sequence. sequence. File ownership:File ownership:

When you create a file, your user name shows up in the third When you create a file, your user name shows up in the third column of the file’s listing.column of the file’s listing. your group is the “group owner” of the file displayed in the your group is the “group owner” of the file displayed in the fourth column of the file’s listing.fourth column of the file’s listing. when the system administrator creates a user account:when the system administrator creates a user account:

The user_id (UID) – both its name and number.The user_id (UID) – both its name and number. The group_id (GID) – both its name and number.The group_id (GID) – both its name and number.

How can I know my own UID and GID?How can I know my own UID and GID?

$$ididuiduid=655537(kumar) gid655537(kumar) gid=655535(metal)655535(metal)

Page 177: Shell Programming and Scripting Languages III Year IT –B

File PermissionsFile Permissions Unix follows a three-tired file protection system that Unix follows a three-tired file protection system that determines the file’s access rights.determines the file’s access rights.

Each group represents a “category” and contains three Each group represents a “category” and contains three slots, representing slots, representing read, write, executeread, write, execute permissions. permissions.

The three groups/categories are:The three groups/categories are: Owner:Owner: the person who created the file. the person who created the file. Group:Group: all the users belonging to a group. all the users belonging to a group. Others:Others: those how are neither owner nor belonging to a those how are neither owner nor belonging to a

group. group.

(also referred as “world”)(also referred as “world”)

Page 178: Shell Programming and Scripting Languages III Year IT –B

chmod:chmod: This command is used to set the permissions of one or more This command is used to set the permissions of one or more files for all categories of users (owner, group and others)files for all categories of users (owner, group and others) It can be run by only the owner of the file and the super It can be run by only the owner of the file and the super user.user. The command can be used in two ways:The command can be used in two ways:

Symbolic code. Symbolic code. Octal code. Octal code.

Generally a file or a directory is created with a default set of Generally a file or a directory is created with a default set of permissions. The default permissions in general: permissions. The default permissions in general: _rw_r_ _r_ __rw_r_ _r_ _

Symbolic code:Symbolic code: When changing permissions in a relative manner, chmod When changing permissions in a relative manner, chmod only changes the permissions specified in the command line only changes the permissions specified in the command line and leaves the other permissions unchanged.and leaves the other permissions unchanged.

syntax:syntax: $ $chmodchmod category category operationoperation permissions permissions filenamefilename

Page 179: Shell Programming and Scripting Languages III Year IT –B

Category specifies following: u —user g – group o —other a —allThere are three sets of operators: = --- reset the permissions + --- add a permission - --- delete a permissionPermissions are represented as r --- read w --- write x --- execute

Page 180: Shell Programming and Scripting Languages III Year IT –B

Examples:Examples: To assign execute permissions to the user.To assign execute permissions to the user. $$ chmod chmod uu+xx file1.text file1.text

The expression combining all 3 categories.The expression combining all 3 categories. $$ chmod chmod ugougo+xx file1.text file1.text

To remove a permission.To remove a permission. $$ chmod chmod go-rgo-r file1.text file1.text

Combining assign, remove operation in single line.Combining assign, remove operation in single line. $$ chmod chmod a-x,goa-x,go+rr file1.text file1.text

Combining permissions in a single line.Combining permissions in a single line. $$ chmod chmod oo+wxx file1.text file1.text

Note:Note: When you do not specify the category, then the When you do not specify the category, then the permissions are applied to all categories.permissions are applied to all categories.

$$ chmod chmod +xx file1.text file1.text (it is same as $(it is same as $chmodchmod a+x a+x file1.txtfile1.txt))

Page 181: Shell Programming and Scripting Languages III Year IT –B

Octal code: Permissions are represented by octal digits. The octal commands set every permissions. Permission octal values: read -- 4 write – 2 execute -- 1 Examples:Examples: Assigning r,w permissions to all the 3 categories.Assigning r,w permissions to all the 3 categories. $$ chmod chmod 666 666 abc.text (same as abc.text (same as $$chmod chmod aa+rwrw abc.txt) abc.txt)

To restore the original permissions to the file.To restore the original permissions to the file. $$ chmod chmod 644 644 abc.text abc.text

Page 182: Shell Programming and Scripting Languages III Year IT –B

Using chmod recursively (-R):Using chmod recursively (-R): -R (recursive) option can be used with chmod to make it -R (recursive) option can be used with chmod to make it descend a directory and apply the expression to every file and descend a directory and apply the expression to every file and subdirectory it finds.subdirectory it finds.

Examples: Examples:

$$chmod –R achmod –R a+x prgsx prgs

$$chmod –R chmod –R 755 .. ((works on hidden files alsoworks on hidden files also))

$$chmod –R achmod –R a+x * x * ((Leaves out hidden filesLeaves out hidden files))

Page 183: Shell Programming and Scripting Languages III Year IT –B

Directory permissions:Directory permissions: DDirectories also have their own permissions and the irectories also have their own permissions and the significance of these permissions differ a great deal from significance of these permissions differ a great deal from ordinary files.ordinary files.

Ordinary files also influenced by the permissions of the Ordinary files also influenced by the permissions of the directory.directory.

A directory must never be writable by group and others.A directory must never be writable by group and others.

To check permissions of a directory:To check permissions of a directory: $$mkdir mkdir prgs; prgs; ls ls -ld -ld prgsprgsSetting default File and Directory Setting default File and Directory permissions:permissions:

When you create files and directories, the permissions When you create files and directories, the permissions assigned to them depend on the system’s default settings.assigned to them depend on the system’s default settings.

The Unix system has the following default permissions:The Unix system has the following default permissions:

rw_rw_rw_ rw_rw_rw_ (Octal 666) for regular files.(Octal 666) for regular files. rwxrwxrwx rwxrwxrwx (Octal 777) for directories. (Octal 777) for directories.

Page 184: Shell Programming and Scripting Languages III Year IT –B

Actually, this default is transformed by subtracting the Actually, this default is transformed by subtracting the ““user maskuser mask” from it to remove one or more permissions.” from it to remove one or more permissions.

umaskumask is a shell built-in command that can be used to set a is a shell built-in command that can be used to set a new default permissions.new default permissions.

Example:Example:$$umaskumask (To display current umask value)(To display current umask value)002002$$umask 022umask 022 (To set new umask value)(To set new umask value)

This indicates the octal number to be subtracted from This indicates the octal number to be subtracted from default to obtain the actual default setting. (i.e., 644 for files, default to obtain the actual default setting. (i.e., 644 for files, 755 for directories)755 for directories)

Note:Note: No one – not even the Administrator can use umask to No one – not even the Administrator can use umask to change the system wide default settings.change the system wide default settings.

The system wide umask setting is placed in one of the The system wide umask setting is placed in one of the machine’s startup scripts, and is automatically available to all machine’s startup scripts, and is automatically available to all users.users.

Page 185: Shell Programming and Scripting Languages III Year IT –B

Changing ownership and group There are two commands that allow the owner and group to be

changed To change ownership : chown To change group : chgrpSyntax: chown newusername filename