You are on page 1of 133

UNIX Structure

Applications
Shell

kernel
H/W

ravi_aix@yahoo.com

Kernel
The Kernel interfaces directly with the hardware devices and controls their access. It also controls the processes that are started by users.

Shell is a command interpreter and acts as an interface between user and kernel.
Also the Shell is a programming language. We can write Shell scripts to automate tasks.

ravi_aix@yahoo.com

Logging In $ login: team01 team01s Password: $ Logging In $ <ctrl+d> (or)

$exit
$logout

(or)

ravi_aix@yahoo.com

Passwd
Passwd command is used to change the user password.
$

passwd

Changing password for team01 team01s Old password : team01s New password: Enter the new password again:
$ _

ravi_aix@yahoo.com

Command Format
The general format of an AIX command is command option (s) argument (s)

$ lsvg l rootvg

command

option

argument

ravi_aix@yahoo.com

Date,Cal commands $ date Sat Jan 12 09:11:15 $ cal 5 2008 May 2008 Tue Wed Thu 1 6 7 8 13 14 15 20 21 22 27 28 29

GMT

2008

Sun

Mon

4 11 18 25
7

5 12 19 26

Fri 2 9 16 23 30

Sat 3 10 17 24 31
ravi_aix@yahoo.com

Clear,echo and banner


The Clear command clears the terminal window.

$ $ $ $ $ $ $ $

clear echo dinner is at 20:00 dinner is at 20:00 banner hello

ravi_aix@yahoo.com

Who, Finger commands


The Who and Finger commands are used to find information about the users.

$ who root jim

lft0 pts/0

Aug 5 05:45 Aug 5 07:40

$ who am i jim pts/0

Aug 5 07:40

$ finger jim Login name: jim Directory: /home/jim Shell: /user/bin/ksh On since Mar 04 13:23:32 on tty3 No Plan.
9

ravi_aix@yahoo.com

Sending Mail $ mail team01 Mail to a user on the same system Subject: Meeting There will be a meeting in room 432 at noon. <ctrl-d> Cc: <Enter> $ mail team20@sys32 Mail to a user on a different system Subject: Meeting Do not forget about the meeting. <ctrl-d> Cc: <Enter>

10

ravi_aix@yahoo.com

write
write provides a conversation like communication with each user alternatively sending and recieving messages.

$ write jim <enter> reboot sys2 at noon <enter> o <enter> o means over

sams terminal

$ write sam Ill do that <enter> jims terminal oo <enter> oo means over and out <ctrl-d> to end session

$ write sarah@system23
11

use write across a N/W


ravi_aix@yahoo.com

wall
The wall command writes to all terminals and useful to notify all users of a system event.

$ wall The server will be down from 23:00 today. talk


The talk command allows two users to hold a conversation.

$ talk fred
(fred recieves this message) Message from TalkDaemon@sys1 at sys1 at 5:23 ... talk: connection requested by john@sys1 talk: respond with: talk john $ talk john
12

(fred accepts invitation)


ravi_aix@yahoo.com

Files and Directories


A file is a collection of data.

contains text or code data


13

contains a list of files.

Usually represent devices used by the system.eg., /dev/cd0


ravi_aix@yahoo.com

File Structure
AIX has a Hierarchial File Structure.

14

ravi_aix@yahoo.com

File Structure
AIX has a Hierarchial File Structure.

15

ravi_aix@yahoo.com

File Structure
AIX has a Hierarchial File Structure.

16

ravi_aix@yahoo.com

File Structure
AIX has a Hierarchial File Structure.

17

ravi_aix@yahoo.com

pwd command
The print working directory prints your current directory

$ pwd /home/team01

18

ravi_aix@yahoo.com

list command

ls [ directory ]

The list command is used to list the contents of a directory.

$ ls bb manuals dec.out $ ls -a . .. .profile bb manuals $ ls l total 3 drwxrwxr-x -rwxrwxr-x

dec.out

2 team01 staff 1024 Jan 15 2 team01 staff 512 Aug 18

10:45 abc 15:40 ssa

19

ravi_aix@yahoo.com

cd command $ cd home $ cd /home/team01 $ cd $ cd ..

cd [ directory ] relative path absolute path moves to your home directory moves to the parent directory

The cd command changes our current directory.

20

ravi_aix@yahoo.com

mkdir command

mkdir [ directory ] full pathname relative pathname

The mkdir command creates one or more new directories.

$ mkdir /home/team01/test $ cd /home/team01 $ mkdir test $ mkdir p dir1/dir2/dir3

create multiple dir

21

ravi_aix@yahoo.com

rmdir command

rmdir [ directory ]

The rmdir command removes a directory.

$ rmdir /home/team01/aaa $ cd home $ rmdir team01 rmdir: team01 not empty $ rmdir p dir1/dir2/dir3

22

ravi_aix@yahoo.com

rmdir command

rmdir [ directory ]

The rmdir command removes a directory.

$ rmdir /home/team01/aaa $ cd home $ rmdir team01 rmdir: team01 not empty


Directory must be empty in order to delete it

23

ravi_aix@yahoo.com

display directory info


The istat command displays the i-node information for a particular file or dir. Every file has an associated i-node number. When a file is accessed the filename is matched with the corresponding i-node number and the data is located. name i-node
# Type mode

links 2 1

User

Group

Date
jan 10 9:15 jan 10 9:15

Size 512 96

loc

Subdir1
myfile

4
10

4 10

dir file

755 644

jim jim

staff staff

$ vi myfile

24

ravi_aix@yahoo.com

$ istat mydir
Inode 51 on device 10/8 Directory Protection: rwxr-xr-x owner: 208(team01) Link count: 2 Group: 1 (staff) Length 512 bytes

Last updated: Last modified: Last accessed:

Mon Mon Tue

Jan Jan Jan

17 17 18

21:05:43 2000 21:05:43 2000 13:25:43 2000

25

ravi_aix@yahoo.com

touch command

touch filename

The touch command creates a zero-length file. If a file with the name already exists the last modification time is updated with the current time.

$ touch aa

26

ravi_aix@yahoo.com

copy command cp source target cp file1 file2 ... target_dir


The cp command can be used to copy files and directories.

$ pwd /home/team01 $ cp /home/team02/aa bb $ cp R /home/team01

Recursive copy

27

ravi_aix@yahoo.com

move command

mv source target

The mv command can be used to move files and directories and also rename them.

$ pwd /home/team01 $ mv test test.out

28

ravi_aix@yahoo.com

cat, pg and more commands


The cat command lists a file but if the file is longer than one screen space it scrolls down to the end of the file. The pg command displays the file one page at a time. The more command also works the same way but you can scroll one line at a time by pressing Enter and one screen at a time by pressing the space bar.

$ cat aa this is a file to test the output of cat command. this file is created using vi editor. $ pg aa $ more aa

29

ravi_aix@yahoo.com

wc command wc [-c] [-l] [-w] filename


The wc command counts the number of lines,words and bytes in a named file. This command is very useful when comparing files.

$ wc -lcw abc_file 15 154 996 abc_file

words Characters

Lines

If no options are used the order will be lines,words and characters


ravi_aix@yahoo.com

30

link command

ln source_file target_file

The ln command allows one file to have more than one name. Both copies have the same i-node.

$ pwd /home/team01 $ ln abc /home/team02/xyz

31

ravi_aix@yahoo.com

print command

qprt filename1 filename2

To queue files to the printer we use the qprt command. The qchk command displays the current status of a print queue. To cancel a print job use the qcan command.

$ qprt ls.out aa.txt $ qprt P lp2 mail to specify a non-default printer $ qchk Queue Dev lp0 lp0

Status Running

Job 99

Files User PP% Blks Cp Rnk aa jim 1 1 1 1

$ qcan 23

job number
32

ravi_aix@yahoo.com

File protection/permissions $ ls -l -rwxr-xr-x 2 team01 staff 1024 jan 12 10:35 test

Permission bits

user name

user group

file/dir

link count

character count

last modified time

filename

33

ravi_aix@yahoo.com

File protection/permissions $ ls -l -rwxr-xr-x 2 team01 staff 1024 jan 12 10:35 test

rwx r-x r-x


Permission bits

r w x

----------

read write execute

Owner (u)

Owners Group (g)

Others (o)

all (a)
34

ravi_aix@yahoo.com

Changing permissions

35

ravi_aix@yahoo.com

symbolic notation; chmod mod filename

$ ls -l -rwxr-xr-x 2 team01 staff 1024 jan 12


r w x ---------read permission write permission execute permission u g o ------+ = -------

10:35 test

add permission remove permission clear permission

owner owners group others

a --- all

$ chmod go-x test -rwxr--r-- 2 team01 staff 1024 jan 12


36

10:35 test
ravi_aix@yahoo.com

octal notation; chmod mod filename r w x ---------4 2 1

rwx r-x rw-

4+2+1=7

4+2=6 4+1=5 10:35 test

$ ls -l -rwxr-xrw- 2 team01 staff 1024 jan 12

To change permissions so that group also has write perm., and others do not have write perm:

$ chmod 774 test $ ls l -rwxrwxr-- 2 team01 staff 1024 jan 12


37

10:35 test
ravi_aix@yahoo.com

umask
umask is an octal number which specifies what permission bits will be set on a new file or directory when created. It has a default value of 022 which is set in /etc/security/user.

$ umask 022

38

ravi_aix@yahoo.com

The Vi editor

$ vi tstfile ~ ~ ~ ~ ~ ~ tstfile [New file] If the file doesnt exist a new file tstfile is created.
39

ravi_aix@yahoo.com

The Vi editor $ vi tstfile ~ ~ ~ ~ ~ ~ tstfile [New file]

~ represents empty lines


The editor starts in command mode

40

ravi_aix@yahoo.com

The Vi editor $ vi tstfile ~ ~ ~ ~ ~ ~ tstfile [New file]

commands
a A i I ----add text after cursor add text to the end of line insert text at the cursor insert text at the start of line

To exit TEXT MODE press <Esc>

Cursor Movement

h j

41

ravi_aix@yahoo.com

The Vi editor

more commands

:q! -- quit without saving :w -- write changes and stay in editor :wq :x -- write changes and quit <shift-zz> x -dw -d$ -d0 -dd -:20,40d u
42

delete a single character delete to the end of current word delete to the end of the line delete to the start of the line delete the whole line -- delete a range of lines (20,40 are line numbers) -- undo last change
ravi_aix@yahoo.com

The Vi editor /the ?the n N

search for a pattern search forward for the search backward for the continue search in same direction continue search in opposite direction

Global Replacement :g/abc/s//xyz/g replaces abc with xyz throughout the entire file yy p copy a line paste the copied line

43

ravi_aix@yahoo.com

vi options
we can change the behaviour of vi by setting some options. options are stored in the file $HOME/.exrc

:set :set :set :set :set :set :set :set :set

all ai noai nu nonu list nolist ic noic

display all settings set autoindent on set autoindent off enable line numbers disable line numbers displays non-printable characters hide non-printable characters ignore case sensitivity case sensitive

44

ravi_aix@yahoo.com

command-line editing
To enable the functionality of vi editor in the command line. To recall previous commands press <esc-k>,use k,j keys to scroll through previously entered commands.

$ set o vi $ set +o vi turn off command recall facility

45

ravi_aix@yahoo.com

vi EDITORS
There are various forms of vi.

read-only form of vi

beginners version of vi

subset of vi working in line mode

simple form of ex

46

ravi_aix@yahoo.com

SHELL Basics
The Shell is the primary interface between the user and the OS.

47

ravi_aix@yahoo.com

Metacharacters and Wildcards


These are characters that the shell interprets as having a special meaning. These should not be used as a part of any filename. Wildcards are used to search for and match file patterns.

48

ravi_aix@yahoo.com

Examples $ ls ne? net new $ ls ?e? few new den ? is expanded by the shell to match any single character in a filename.

$ ls n* net new no node2.out $ ls ne[stw] net new

49

ravi_aix@yahoo.com

Standard files and Redirection


Three files are automatically opened for each process.

standard input (0) default is keyboard ---- stdin <


standard output (1) standard error (2) ---- stdout > default is screen ---- stderr 2>

These defaults can be changed by redirection.

$ ls > ls.out

output redirection

$mail team01 < letter


$cat abc 2> errfile
50

input redirection
error redirection
ravi_aix@yahoo.com

Pipes
A pipe is a sequence of one or more commands where the stdout of one command becomes the stdin of the next command.

$ cat abc | wc l 124 Filters

A filter is a command that reads from stdin, processes it and then writes it to stdout.

$ ls l | grep d | wc l

Filter
51

ravi_aix@yahoo.com

Tee
The tee command reads stdin and sends the data to both stdout and a file.

$ ls l | tee /tmp/ls.out | wc l

mail.txt err.out ast.pdf

ls -l

tee

wc -l

/tmp/ls.out
52

ravi_aix@yahoo.com

command grouping
Multiple commands can be entered on the same line seperated by a semi-colon.

$ ls R > ls.out ; cat aa Line continuation


\ can be used to continue a command on a seperate line. A > prompt id issued by the shell to indicate line continuation.

$ cat /home/mydir/mails/mail2 \ > /home/jim/imp/data.txt

53

ravi_aix@yahoo.com

Shell Variables
Variables represent data whose value may change. Shell variables define your environment...HOME,TERM,PATH etc., Shell variable names are case sensitive. The convention is that UPPERCASE are used for system-defined variables and lowercase are used for user-defined variables.

$ set

displays your current settings

HOME=/home/team01 PATH=/bin:/usr/bin:/etc:/home/team01/bin: PS1=$ PS2=> SHELL=/usr/bin/ksh TERM=ibm3151 $_


54

ravi_aix@yahoo.com

Setting and Referencing Shell Variables $ SHELL=/usr/bin/bash assign a value to a shell variable $ xy=hi friends $ echo $xy hi friends $ unset xy

referencing a variable
delete a variable

$ xy=day $ echo tomorrow is sun$xy tomorrow is sunday

55

ravi_aix@yahoo.com

Commmand substitution $ date Thu 13 Oct 23:43:55 2008 $ now=$(date) $ echo $now Thu 13 Oct 23:43:55 2008

56

ravi_aix@yahoo.com

Quoting Metacharacters
Single Quotes Ignores all metacharacters between the quotes.

$ echo $HOME $HOME


Double Quotes Ignore all metachar., except for $,` and \

$ echo $HOME /home/team01


\Backslash Ignore the special meaning of the following character

$ echo \$HOME $HOME


57

ravi_aix@yahoo.com

Process
A program or a command that is actually running on a system is referred to as a process. Every process has a Process ID (PID). PID 1 is always assigned to the init process which is the first process that is started during the boot process. A PPID is the parent PID. The variable $$ shows the PID of the current shell.

$ ls $date $clear $echo $$ 4353


58

PID 205 206 207

PPID 1 205 20

ravi_aix@yahoo.com

The Login Process


When a user logs into a system a new process is started with a PID that is randomly allocated by the Kernel. The program usr/bin/ksh is loaded into this process.

PID=345

-ksh

Login

kernel loads ksh

59

ravi_aix@yahoo.com

Variables and Processes


variables are part of the process environment.Processes cannot access or change variables from another process.

$ x=4 $ ksh $echo $x


Subshell

$x=1 $<ctrl-d> $echo $x 4

Each program runs in its own process environment.Variable x is not known in the subshell. To pass variables into a subshell we need to execute the export command.
60

ravi_aix@yahoo.com

Exporting variables $ x=4 $ y=6 $ export y $echo $x; echo $y 4 6 $ksh $echo $x $echo $y 6 $y=11 <ctrl-d> $echo $y 6
61

variable is exported to subshell

Subshell

variable is not exported

ravi_aix@yahoo.com

Shell Script
A shell script is a collection of commands stored in a text file. Any text editor can be used to create a shell script. Here we start ksh and pass the script name as an argument.

$ cat hello echo hi friends today is $(date) pwd $ksh hello execute script hi friends today is Mon Aug 12 2008 12:34 /home/team01

Subshell

62

ravi_aix@yahoo.com

Invoking Shell Script


The shell uses the PATH variable to find executable programs. The directory in which the script is stored must be defined in the path variable.

$ cat hello echo hi friends today is $(date) pwd $ chmod +x hello $ hello execute script hi friends today is Mon Aug 12 2008 12:34 /home/team01
Subshell

63

ravi_aix@yahoo.com

Invoking Shell Script


Each Shell Script is executed in a subshell. Variables defined in a shell script cannot be passed back to the parent shell. If the script is invoked with a .(dot) it runs in the current shell.

$ cat hello echo hi friends today is $(date) pwd $.hello execute script hi friends today is Mon Aug 12 2008 12:34 /home/team01

Subshell

64

ravi_aix@yahoo.com

Exit Codes
A command returns an exit value to the parent process. The environment variable $? contains the exit value of last command.

0 1-255

= =

Success An error has occured

$ cd /etc/security ksh: /etc/security: permission denied $ echo $? 1

65

ravi_aix@yahoo.com

ps command
The ps command displays process status information.

$ps -f UID PID jim 205 jim 206 $ps -f $ps -e

PPID ... TTY 1 ... tty0 205 ... tty0


... ... ...

COMMAND ls date

FULL listing displays info about every process running in the system Long listing shows all processes from user team05
ravi_aix@yahoo.com

$ps l $ps u team05

66

Types of Processes
Processes that are started from and require interaction with the terminal are called Foreground processes. Processes that can run independently are called Background processes.

$ps f > pro.out

background process

67

ravi_aix@yahoo.com

Terminating Processes

$kill 202 $kill -9 215

(Termination signal) (kill signal)

68

ravi_aix@yahoo.com

Termination Signals Signal 01 Meaning hangup signal is sent to a process if its parent dies Interrupt signal is generated when <ctrl-c> is pressed Quit signal is generated when the quit key is pressed <ctrl-\>

02

03

09
15
69

Kill signal (USE WITH CAUTION)


Termination signal (Default)
ravi_aix@yahoo.com

Running long processes


The nohup command will prevent a process from being killed if you log off the system before it completes. If you do not redirect output, nohup will redirect output to a file nohup.out nohup tells the process to ignore signals 01 and 03.

$nohup ls R / > ls.out & [1] 34 $_

$nohup ls R / [1] 34 sending output to nohup.out $_

70

ravi_aix@yahoo.com

Job control $jobs [1] + List jobs ls R / > ls.out Suspend a foreground job

Running

<ctrl-z>

$fg %1
$bg %1 $kill %1

execute job in foreground


execute job in background kill a job

71

ravi_aix@yahoo.com

Daemons
A daemon is a never ending process that controls a system resource (printer queue). It starts when the system is started and runs until the system is down. For example qdaemon tracks print job requests and the printers available to handle them.

72

ravi_aix@yahoo.com

User Environment Login Files


The first file that the OS uses at login time is the /etc/environment file which contains variables specifying the basic environment for all processes. The second file is the /etc/profile file which controls systemwide default variables.

The third file is the .profile file which resides in a users login login directory and enables the user to customize their working environment.

73

ravi_aix@yahoo.com

User Environment Login Files root /etc/environment

can only be changed by system admin

root /etc/profile

can only be changed by system admin

User
$HOME/.profile
74

can be changed by the user


ravi_aix@yahoo.com

sample /etc/environment
$ cat /etc/environment # # # # WARNING: This file is only for establishing environment variables.Execution of commands from this file or any lines specified above may cause failure of the initialization process.

PATH=/usr/bin:/etc:/usr/sbin:/usr/ucb:/usr/bin/X11:/sbin TZ=EST5EDT LANG=C LOCPATH=/usr/lib/nls/loc NLSPATH=/usr/lib/nls/msg/%L/%N:/usr/lib/nls/msg/%L/%N.cat

75

ravi_aix@yahoo.com

sample /etc/profile
$ cat /etc/profile #System-wide profile.All variables set here may be #overridden by a users personal .profile file in their #$HOME directory.However all commands here will be #executed at login regardless.

readonly LOGNAME umask 022 TMOUT=120 MAIL=/usr/spool/mail/$LOGNAME MAILMSG=[YOU HAVE NEW MAIL] TERM-ibm3151
export LOGNAME MAIL MAILMSG TERM TMOUT
ravi_aix@yahoo.com

76

sample .profile
$ cat .profile PATH=/bin:/usr/bin:/etc:$HOME/bin: PS1= $PWD=> if [ -s $MAIL ] then echo $MAILMSG ENV=$HOME/.kshrc execute this file every time a new ksh is started export PATH PS1 ENV

77

ravi_aix@yahoo.com

Environment Variables
LOGNAME TMOUT MAIL It is a read-only variable which contains your login name. Time after which the terminal is logged off by the system. Holds the name of the file where your mail is sent.

TERM
TERM

The terminal type that we are using.


A list of colon seperated directories that the shell searches for commands

PS1
ENV

Primary system prompt(default is $)


Pointer to a file containing Korn shell settings
ravi_aix@yahoo.com

78

sample .kshrc
$ cat .kshrc

# set up the command recall facility


set o vi # set alias alias alias up aliases l=ls -l p=ps -f up=cd ..

The difference between .profile and .kshrc is that .kshrc is read each time a subshell is spawned whereas .profile is read once at login.

79

ravi_aix@yahoo.com

ksh features-Aliases
The alias command invoked with no arguments prints the list of aliases in the form name=value. The unalias command will cancel the alias named and removes it from the alias list. $ alias p=ps -ef $ alias l=ls -l $ alias history=fc -l l=ls -l p=ps -ef r=fc e-

$unalias l
$ l ksh: l: not found
80

ravi_aix@yahoo.com

ksh features-History
The last 128 commands are stored in $HOME/.sh_history. The r command allows you to recall previously entered commands. $ 2 3 4 5 fc l cd /home/list ls l fc l mail lists the last 16 commands

$ r m No mail for team01 $ r 3 -rw-r-r john staff 345 aug 23 12:35 fileout

81

ravi_aix@yahoo.com

AIX Utilities

find command (find path expression)


Searches one or more dir structures recursively for files meeting certain specified criteria and displays those filenames or executes commands against them.

$find . name sum -print ./color/sum ./shape/sum

82

ravi_aix@yahoo.com

Executing commands with find


The exec command executes a command on each of the files found.

$find . name m* exec -rw-r-r john staff 45 -rw-r-xr john staff 465 -rwxrwr john staff 12

ls l {} \; aug 23 11:35 fileout aug 23 23:11 df.out aug 23 03:12 aa

The command following exec (ls) is executed for each file name found. \; is hard coded with the find command and is required for use with exec and ok.

83

ravi_aix@yahoo.com

Interactive command execution


The ok option causes command execution on an interactive basis.

$find . <rm ... <rm ... <rm ...

name m\* ok rm {} \; ./shape/misc>? y ./shape/medium>? y ./misc>? n

84

ravi_aix@yahoo.com

Additional Options
-type f d +n -n n ordinary file directory larger than n blocks smaller than n blocks equal to n blocks

-size

-mtime

+x -x
onum mode user

modified more than x days ago modified less than x days ago
access permissions match onum access permissions match mode values find files owned by user logical or
ravi_aix@yahoo.com

-perm

-user -o

85

find examples
$find . name s* type f size +2 exec ls l {} \; -rw-r-r john staff 3545 aug 23 11:35 sum.out -rwxr-r john staff 4543 aug 11 23:12 ss.out $find . perm 644 mtime +4 print ./impfiles/misc

$find . name imp o name misc ./files/misc ./imp


$find / name securityprint 2> err.out /var/security /usr/lib/security

86

ravi_aix@yahoo.com

find with the links option


$ find /home type f links +1 | xargs ls li 127 rw-r--r 3 team01 staff 156 oct 29 12:15 /home/file1 127 rw-r--r 3 team01 staff 156 oct 29 12:15 /home/filecopy1 127 rw-r--r 3 team01 staff 156 oct 29 12:15 /home/filecopy2

same i-node

same no. of bytes

no. of links The links +1 option lists the files that have more than one link associated with them.

87

ravi_aix@yahoo.com

grep command (grep [options] pattern [file1 file2....])


Searches for lines matching specified pattern and also displays the name of the file containing the pattern.

$grep 800 phone1 Hotline

HW

800 322-4500

$ps ef | grep team01 team01 10514 13254 0 09:54:55 pts/1 0:00 -ksh

88

ravi_aix@yahoo.com

grep with regular expressions


When * is used with the grep command ti will match zero or more occurences of the previous character.

Valid metacharacters
. * [aA] any single character zero or more occurences of the preceeding character Enumeration: a or A any one of the char between a to f Any lines that start with a Any lines that end with a z
ravi_aix@yahoo.com

[a-f] ^a z$
89

grep examples
$ps ef | grep team01 team01 10514 13254 0 09:54:55 pts/1 0:00 -ksh $grep ^B phone1 Baker jim $grep 5$ phone1 Avis Betty

^ - start
454-3414

125-4625

$ - end

$grep ^[DH] phone1 Dade travel sam Hotline HL

434-5643 322-4500

[DH] - enumeration

90

ravi_aix@yahoo.com

grep options -v -c -l print lines that do not match print only a count of matching lines print only the names of the files with matching lines number the matching lines ignore case senitivity do a whole word search

-n -i -w

91

ravi_aix@yahoo.com

Other greps fgrep

fast grep only fixed strings;no expressions


800 322-3455 slightly faster

$fgrep HW phone1 Hotline HW

egrep

Extended grep allows multiple patterns


slightly slower

$egrep 800|817 phone1 Hotline HW 800 322-3455 sam jim 817 322-3434

92

ravi_aix@yahoo.com

sort command
The sort command sorts lins and writes the result to standard output.

$ cat animals dog.2 cat.4 rabbit.7

$ sort animals default sort order cat.4 dog.2 rabbit.7 $ cat animals | sort +0.1 sort by second char., rabbit.7 cat.4 dog.2

93

ravi_aix@yahoo.com

head and tail commands


head command can be used to view the first few lines of a file or files. tail command displays a file beginning at a specified point and displaying a specified number of lines.

$head -5 ls.out

read first 5 lines

$tail -20 myfile $tail f /tmp/err.out displays lines as they are added to the file

94

ravi_aix@yahoo.com

xargs
The xargs command reads a group of commands from stdin and runs an AIX command with that group of arguments. Here cat passes xargs the list of files and allows xargs to pass them to rm. The t flag echoes the constructed command line to stderr.

$cat oldfiles file1 file2 file3 file4 $cat oldfiles | xargs t rm rm file1 file2 file3 file4
ravi_aix@yahoo.com

95

xargs more examples


Here we create a list of files to be printed and queue them up for printing through xargs.

$ls > printlist $xargs t qprt < printlist qprt file1 file2 file3 $ls | xargs t I {} mv {} {}.old mv mail mail.old
Here { } is called a placeholder and the I flag tells xargs to insert each line of the ls directory listing where { } appears.

96

ravi_aix@yahoo.com

xargs,find and grep


Using xargs is more efficient and easier to remember than the find commanad

$ find . type f mtime +30 | xargs t rm rm ./file1 ./file2 ./file3 $ find . type f | xargs t grep l Hello grep l Hello ./file4 ./file7 ./file10

97

ravi_aix@yahoo.com

which,whereis,whence
which command takes a list of program names and looks for the files that are executed when these names are given as a command. whereis also takes a list of program names but only searches in some standard locations. Whence is a ksh-specific command which also searches for ksh aliases.

$ which find /usr/bin/find $ whereis find /usr/bin/find $ whence find /usr/bin/find


98

ravi_aix@yahoo.com

file command
The file command can be used to determine the type of a file. It also tells us under what OS version it is compiled. $file /usr/bin/vi /usr/bin/vi:executable (RISC System/6000) or object module $file cl cl: ascii file $file /usr/bin /usr/bin: directory

99

ravi_aix@yahoo.com

diff (Differential File Comparator)


The diff command works only with text files and and reports the differences between the two files. $ diff names names.old 1c1 < Carol --->Caroline 4d3 < Don 5a5 > Sam
names Carol Bob Jim Don Mary

> <

names.old caroline jim Mary Sam

a indicates lines that should be appended to first file to obtain the second file d indicates lines that are missing from second file.

c indicates lines that are changed between the first file and second file.
100

ravi_aix@yahoo.com

cmp (compare)
The compare command works with all types of files and it reads two files until it finds any differences and reports them. $ cmp names names.old names names.old differ: $ cmp l names names.old 6 12 151 7 102 154 .... .... cmp:EOF on names byte 6,line1

Here the first column is the byte number and the second and third columns are the octal values of the bytes from first file and second file respectively that differ.

101

ravi_aix@yahoo.com

Telnet
The tn command allows to login to remote systems.

$tn miami Trying ... connected to miami ...

AIX Version 5 (C) Copyright by IBM and others 1982,1996 login: team01

102

ravi_aix@yahoo.com

FTP
The ftp command allows us to transfer files in a network.

$ftp miami connected to miami 220 FTP server ready Name (miami: team01): team05 Password rewquired for team05. Password: 230 User team05 logged in. ftp>

ftp prompt
ravi_aix@yahoo.com

103

FTP subcommands
The most important ftp subcommands are: ftp> pwd ftp> cd remotedir ftp> dir (or) ls -l

ftp> get remotefile [localfile]


ftp> put localfile [remotefile] ftp> help [subcommand] ftp> quit

get file1 /tmp/file1


put /im/ls.out ls.out

104

ravi_aix@yahoo.com

tar: Backup and Restore files


tar(tape archiver) saves files recursively and stores them as one archive file.

tape

tar cvf /dev/rmt0 .

tar xvf /dev/rmt0

floppy
tar cvf /dev/fd0 . tar xvf /dev/fd0

disk
tar cvf /tmp/file1.tar . tar xvf /tmp/file1.tar

105

ravi_aix@yahoo.com

tar: more options


If the .(dot) is specified then the files are saved relatively which allows you to restore the files in a new directory.

The tar options are -c -t -v -f -r -x create table of contents verbose filename extend archive extract

To show the contents of archive file tar tvf /dev/rmt0

106

ravi_aix@yahoo.com

compress,uncompress,zcat
The compress command compresses a file and replaces the original file with a .z appended to it. $ ls l file1 127 rw-r--r 3 team01 staff 13354 oct 29 12:15 /home/file1 $ compress v filel file1: compression 56.99% file1 is replaced with file1.z $ ls l file1 127 rw-r--r 3 team01 staff 5756 oct 29 12:15 /home/file1 $ zcat file1.z $ uncompress filel.z $ ls l file1 127 rw-r--r 3 team01 staff 13354 oct 29 12:15 /home/file1
107

view a compressed file

ravi_aix@yahoo.com

Additional Shell Features---Important Shell Variables


These variables are set by the shell or a shell script. $$ $0 $# PID name of currently executing shellscript no., of arguments passed to shellscript

$*
$? $!

All command-line arguments passed to script


Exit value of last command PID of last background process

108

ravi_aix@yahoo.com

Positional Parameters
Parameters can be passed to shell scripts as arguments on the command line $ cat ascript echo first parameter is $1 echo second parameter is $2 echo third parameter is $3 $ ascript good morning friends first parameter is good second parameter is morning third parameter is friends

109

ravi_aix@yahoo.com

expr Utility
The expr utility can be used to perform integer arithmetic. \* multiplication high / integer divison % remainder precedence order + addition subtraction low $ x=6 $ y=3 $ expr $x/$y 2 $ expr $x-$y 3 $ expr \($x+$y)\*4 \(\) to group expressions 36

110

ravi_aix@yahoo.com

conditional execution
The exit value from a command or a group of commands can be used to determine whether to do the next command. command1 && command2 if command1 is succesful then do command2 $ ls s* && rm s* command1 || command2 if command1 is not succesful then do command2 $ cd /dir1 || echo cannot change to dir1

111

ravi_aix@yahoo.com

test command
The test command allows you to test for a given condition.

test expression or [ expression ] or [[ expression ]] The test command evaluates the expression and returns true or false.
Operator $string1 = $string2 $string1 != $string2 $number1 -eq $number2 $number1 -ne $number2 Returns true if strings are equal strings are not equal numbres are equal numbers are not equal

-a $file
-d $file -r $file -w $file
112

file exists
file is a directory file is readable file is writeable
ravi_aix@yahoo.com

if command
if condition is true then carry out this set of actions else carry out these alternative actions fi

optional

The exit statement is used to terminate a process.

113

ravi_aix@yahoo.com

if command (example)
$ cat active usage=$0: userid if [[ $# -ne 1 ]] then echo proper usage: $USAGE exit 1 fi if who | grep $1 > /dev/null then echo $1 is active else echo $1 is not active fi exit0
ravi_aix@yahoo.com

114

read command
The read command reads one line from stdin and assigns the values of each field to a shell variable.
$ cat delfile # usage: delfile echo please enter the filename: read name if [[ -f $name ]] then rm $name else echo error: $name is not an ordinary file fi

The read command can be used to assign more than one variable. Here if delfile is invoked with more than one filename then then first variable is assigned first filename and so on...
ravi_aix@yahoo.com

115

for loop
for variable in list do commands done
$ cat count for var in ls.out err.out do wc l $var done $ count 12 ls.out 34 err.out $ cat rm_tmp for file in /tmp/* do echo removing $file rm $file done

116

ravi_aix@yahoo.com

While loop
while expression do commands done
$ cat info x=1 while [[ $x lt 9 ]] do echo it is now $(date) echo there are $(ps e | wc -l) processes running echo there are $(who | wc -l) users logged in x=$(expr $x + 1) sleep 600 done

117

ravi_aix@yahoo.com

command search order


The shell looks for a in the following order.

Qualified pathname
Reserved word
if,then,else,while...etc.,

alias built-in command


cd,pwd,umask,read...etc.,

Function
path variable
118

ravi_aix@yahoo.com

X Window
The X-window is a N/W based graphics system. It enables us to work with multiple items simultaneously. It provides the capability to manage local and remote displays. X window uses a client/server environment. So the graphic application can run on one system, yet display its output on another system. X window is platform independent. It allows a keyboard and display attached to one system to use programs running on a completely different system.

119

ravi_aix@yahoo.com

X Window
The X-window is a N/W based graphics system.
X-station

RISC workstation

RS/6000 Workstation

RS/6000 Workstation
120

RS/6000 server

Intel PC
ravi_aix@yahoo.com

X Window
X window function is split into terminal and application support. Typically the application support runs on a UNIX system and the terminal system can run on any system. The system providing application support is called client and the one providing terminal support is called server. In most cases both will be on the same system. Client is the application that is running and needs to display graphics to a user. Clients recieve keyboard and mouse input from the associated x server. X servers respond to requests from clients and to actions from users.

121

ravi_aix@yahoo.com

X Clients
X clients are the aplications which the users run under the X window system:common X clients are xterm aixterm xclock Standard terminal emulator IBM AIX terminal emulator displays a clock

xcalc
xwd mwm

displays a calculator
dumps the image of an x window motif window manager

122

ravi_aix@yahoo.com

X Clients
standard X client command line options are:

-bg color
-bd color -bw number

specifies the color for window background


specifies the color for window border specifies window border in pixels Identifies the host server name and the X server display number where the command is to run. specifies the color for the window foreground specifies the normal sized text fontset.

-display Name:Number

-fg color -fn number

123

ravi_aix@yahoo.com

X Server
Each X server controls one keyboard. one mouse and one or more screens. Allows simultaneous access by several clients Performs basic graphic operations Provides information such as fonts and colors Routes keyboard and mouse I/P to the correct clients

124

ravi_aix@yahoo.com

Starting AIXwindow
The startx command is used to start the AIXwindows environment.

If the workstation is not an X Station then startx will execute the xinit command.
By default startx starts three clients aixterm,xclock and mwm. Any errors occuring during will be logged in a file $HOME/.xerrors. We use <ctrl><alt><backspace> to close AIXwindows and return to command prompt. Some windows like aixterm accept and display information while some like xclock and xcalc simply display information.

125

ravi_aix@yahoo.com

The aixterm Window


An aixterm can be started in two ways: In SHELL aixterm Display Root menu and select New Window To create an additional aixterm window enter aixterm & at the command prompt. The aixterm window can be closed by Type exit or <ctrl>+d

126

ravi_aix@yahoo.com

Running a Client on Another System


With AIXwindows it is possible to run a client on a remote system in the network yet display the application window on your screen. we need to tell the client process where to display its window. AIXwindows uses the DISPLAY environment variable to indicate the name of the server where it should display its output. To override this value we need to specify a value using the display flag when starting the client. The value is generally :0.0 for local severs or sys1:0:0 for a remote server.

127

ravi_aix@yahoo.com

Running a Client on Another System


Here sys2 runs the client application(aixterm) while sys1 needs to display the output.
sys1 sys2

$ rexec sys2 aixterm display sys1:0

remote execute comand


128

xclient application

xserver

ravi_aix@yahoo.com

xhost command
The xhost command adds and deletes hosts on the list of machines from which the xserver accepts connections. The X server allows connections only from X clients running on the same machine or clients listed in /etc/X0.hosts. The X host command must be executed on the machine to which the display is attached.
$ xhost displays names of hosts allowed access to Xserver add a hostname remove a hostname

$ xhost + hostname $ xhost hostname

129

ravi_aix@yahoo.com

AIXwindows Startup
The xhost command adds and deletes hosts on the list of machines from which the xserver accepts connections. After a customizable script .xinitrc will execute.It starts a users clients and then starts mwm. mwm starts Motif and tailors it according to files .Xdefaults and .mwmrc. Xdefaults file contains a users personal preferences like colors,fonts etc., and can be found in the users $HOME dir. mwmrc is used to customize things like Root menu etc.,

130

ravi_aix@yahoo.com

Customizing AIXwindows

---

.xinitrc

The startx first searches for a file specified by the users XINITRC environment variable. If it is not set (not set by default) then it searches the users HOME dir for a file called .Xinit,.xinit,.Xinitrc,.xinitrc or .xsession respectively to begin X client programs.

If these files are not found then it uses the system-wide /usr/lpp/X11/defaults/xinitrc.
If a user wishes to customize their own AIXwindows startup env., they should copy the system-wide file into their HOME dir and modify it. The file /usr/lib/X11/rgb.txt contains a list of valid colors which we can use while customizing AIXwindows files.
131

ravi_aix@yahoo.com

Customizing AIXwindows

---

.Xdefaults

Fonts are stored in /usr/lib/X11/fonts and can be listed using $ custom -e font.

Most of the customization is done through the use of resources. For example the bgcolor of aixterm,preferred focus policy are all resources. Most of these resources are set in a users .Xdefaults file and they look like object*attribute: value.
$ vi $HOME/.Xdefaults Aixterm*background: grey Aixterm*foreground: navy Aixterm*font: rom10 xclock*update: 1 Mwm*keyboardFocusPolicy: explicit

132

ravi_aix@yahoo.com

Customizing AIXwindows

---

.mwmrc

Root menu,window menu and mouse options can be customized in the .mwmrc file. DO NOT MODIFY SYSTEM-WIDE FILE which can be found in /usr/lpp/X11/defaults/Motif1.2/system.mwmrc. To customize we need to copy this file to our HOME dir and modify it as it will override the system-wide version.

133

ravi_aix@yahoo.com

You might also like