Sei sulla pagina 1di 75

UNIX Basics

Topics

Introduction to UNIX and Shell


VI Editor
UNIX Commands
Filters & Utilities
Process Scheduling
Shell Scripting Basics
Introduction
The UNIX kernel is the component of the UNIX operating system that
interacts directly with computer hardware.
The shell layer provides the interface between the user and the kernel.
It contains sets of commands that developers can use when creating
UNIX applications.
The application layer contains programs used by the end user.
The file system layer contains hierarchical directories and subdirectories
in which files are stored.
The UNIX shell is a program that allows users to interact with UNIX
systems. It forms an interface between the user and the UNIX kernel.
The shell consists of a set of commands that a user can enter at the
command line and then execute. Users can combine these commands
to perform complex tasks.
Introduction
The main purpose of the shell is to function as a command interpreter.
It prompts the user for a command and then parses the command by
comparing it to its library of known commands. Once the shell has
interpreted a users command, it requests the necessary system
resources from the UNIX kernel and executes the command.
The shell receives information about the results of the executed
command from the kernel and displays these results to the user. It also
prompts the user for the next command.
When the shell executes a command, it interacts with the kernel using
system calls. These are low-level instructions that work together to
execute a command. For eg, a command that copies text into a new
file needs to issue system calls to copy the text, to create the new file,
to open the file, to write the text into the file, and to close the file.
Vi Editor
The vi editor works in the following modes:

Input mode
Command mode

To enter into the VI editor, type vi


To enter into a file through VI editor, type vi <filename>
It can be creating a file for the first time or opening an already existing
file

Shifting between modes:

The VI editor opens the file in the command mode


To change from command mode to the input mode, any of the
commands (a, A, I, I, o, O) can be given
To change from input mode to the command mode, the command ESC
can be given
VI Commands
When the user is in the insert mode, it allows to type the text or script
To save the changes and quit from the insert mode, type :wq
If the user had logged in without specifying the filename initially, then type :wq <filename>
If there are no changes made to the file, then type :q to close the file
If there are some changes made to the file but if it you would to cancel all the changes,
then type :q!
To undo the recent change, type u.
If you change a word, u will change it back.
If you delete a section, u will restore it.
u only works on the last change or deletion
To delete a line, type dd
To restore the line, type p
To edit again the changes made, type :e!
VI Commands
Commands Description

G To go to the beginning of the last line in the file

:w <filename> To write the contents of the buffer to a new file

:r <filename> To read the contents of the file

6G To go to the sixth line

h, j, k, l To traverse in left, down, up and right directions

3x Deletes the three characters from the cursor position

RETURN To move to the beginning of the next line

- To move to the beginning of the previous line

w, b To advance one word and back up one word


VI Commands
Commands Description

dw To delete a word

CTRL-D , CTRL-U To advance one half screen and to backup one half screen

cw <newword> ESC To change a word

dd To delete the line

/<string> To search for a string in a file

// or ?? or n Searches for next occurrence

?<string> To search the string backward in a file

N To reverse the search direction

y,p To yank or delete the text; To get back the deleted text
UNIX Commands

Directory Operation File Comp. Security


Management

cd cmp passwd
pwd comm mkdir
chown
rmdir chgrp
umask
chmod

File File Mountable Copy, Move


contents compression file Remove & Time

cat pack mount cp


ls unpack umount ln
wc mv
file rm
touch
Working With Directories
MKDIR - To create a directory
mkdir <dirname>

mkdir -m 777 SHA

The first digit represents the owner, the second represents the group and the third
represents other users. The number 7 represents all three types of permission (i.e., read,
write and execute), 6 stands for read and write only, 5 stands for read and execute, 4 is
read only, 3 is write and execute, 2 is write only, 1 is execute only and 0 is no permissions.

cd Change working Directory


cd.. To go down one level of the directory tree
cd\ To move to the parent directory

Pwd Gives the information about the current working directory


Rmdir To remove or delete the directory if the directory is empty
rm r - Use recursive delete operation for deleting non-empty directories
Working With Files
mv To move or rename a file (Same is applicable for directory)
mv <oldfilename> <newfilename>
mv <filename> <directory>
cp To copy a file
cp <file1> <file2>
cp r To copy a directory along with its contents
cp r <directory1> <directory2>
rm To remove the file
rm <file1>
The options supported by mv command are f, -i and r
The options supported by cp command are f, -i, -p and r
The options supported by rm command are f, -i and r

-p To preserve the characteristics of the source file.


-f To do the operation forcefully
-r To do the operation recursively (This is needed in case of a directory)
-i To do the operation in interactive mode
Working With Files
ls To list the contents of a directory

Options:
-a => lists all entries including those starting with a period (.).
-i => displays inode numbers along with file names (only on systems
that support inode numbers, such as POSIX-compliant and UNIX
systems).
-l => displays permissions, links, owner, group, size, time, name;
-rw-rw-rw- 1 root dir 104 Dec 25 19:32 file

The first character identifies the file type:


- Regular file b Block special file c Character
special file
d Directory l Symbolic link n Network file
p FIFO s Socket
Working With Files
ls To list the contents of a directory

Options:
The next nine characters are in three groups of three; they describe the permissions on the file.
The first group of three describes owner permissions; the second describes group
permissions; the third describes other (or world) permissions.
r Permission to read file w Permission to write to file x Permission to execute
file
a Archive bit is on (file has not been backed up) c Compressed file
s System file h Hidden file t Temporary file

-p => puts / after directory names.


-R => lists subdirectories recursively.
-r => sorts in reverse of usual order; you can combine this with other options that sort the list.
-s => displays size in blocks (after the inode number, but before other information). If the -k
option is also specified, the size is displayed in kilobytes instead.
Working With Files
chmod To change the file permissions
+ To add permission To remove permission
= turns on the specified permissions and turns off all others.

Types of permissions Read, Write and Execute


To whom we can grant permissions User, Group, Other

Eg: To grant read, write and execute permission to user, group and other on the file
test.ksh
chmod ugo+rwx test.ksh

To remove the same, chmod ugo-rwx test.ksh

Chmod +w test.ksh Sets w for all the user, group, other


chmod a=rwx file - turns on read, write, and execute permissions, and turns off the
hidden, archive, and system attributes. This is equivalent to chmod 0777 file
Working With Files
chmod To change the file permissions

The octal (0-7) value is calculated by adding up the values for each digit
User (rwx) = 4+2+1 = 7, Group(rx) = 4+1 = 5, World (rx) = 4+1 = 5
chmode mode = 0755

Examples
chmod 400 file - Read by owner
chmod 040 file - Read by group
chmod 004 file - Read by world
chmod 200 file - Write by owner
chmod 020 file - Write by group
chmod 002 file - Write by world
chmod 100 file - execute by owner
chmod 010 file - execute by group
chmod 001 file - execute by world

To combine these, just add the numbers together:


chmod 444 file - Allow read permission to owner and group and world
chmod 777 file - Allow everyone to read, write, and execute file
Working With Files
Chown To change ownership of a file

We can copy the file but it has the following disadvantages:


Extra copy of file will be created

The new owner needs to have read permission on the file

Chown <username> <filename>

Chgrp To change group of a file

When new groups are setup on a system or when files are copied to a new
system, we may need to change the group to which a particular file belongs.

chgrp <groupname> <filename>


Working With Files
Find For finding files

-name pattern => compares the current file name to pattern

find "C:/Omnimark" -name "*.java => To find all the files which has extension .java

-mtime number => matches if someone modified the file during the 24-hour period beginning
number days ago.

find "C:/Omnimark" -name "*.ksh" -mtime -2 => To find all the files with extension *.ksh and
which are modified within 2 days

-level number => does not descend below number levels.

Eg: find "C:/Omnimark/File" -name "*.txt" -mtime -1 -level 1


C:/Omnimark/File/BENETH.txt
C:/Omnimark/File/a/b.txt

find "C:/Omnimark/File" -name "*.txt" -mtime -1 -level 0


C:/Omnimark/File/BENETH.txt
Working With Files
Find For finding files

-exec command ; => takes all arguments between -exec and the semicolon as a command line,
replacing any argument which is exactly {} (that is, the two brace characters) with the current
path name. It then executes the resulting command line, treating a return status of zero from
this command as a successful match, non-zero as failure. The terminal semicolon must be
quoted or escaped and delimited with white space.

find ${SgmDir} -name "*.sgm" -mtime +90 -level 0 -exec rm "{}" ";
The above example deletes all the sgm files present in the input directory which are 90 days older

rm -f -r "{}" "; The rm command can be replaced by this command to delete recursively and
forcibly
-atime number => matches if someone accessed the file during the 24-hour period beginning
number days ago.
-ctime number => matches if the file was creation during the 24-hour period beginning number
days ago.

Find / -name new_data print > found &

The above example will find the file named new_data anywhere in the entire file system and print
it in the file found. Since it may take a long time to complete this command, it is run in the
background
Working With Files
Find For finding files

Find . name garden u sha type d print

The above example finds for a directory belonging to user sha with a name beginning with garden

find . -name "*.c" -a "(" -mtime +3 -a -mtime -9 ")"

To find all files which have the extension .c and which were modified more than three days ago and less than nine
days ago:

find . "(" -name "tmp.*" -o -name "*.tmp" ")"


-perm =rx -exec rm "{}" ";"

This finds all read-only files (on Windows systems) which have tmp in either part of their names and deletes all
such files. Various parts of this expression are quoted to protect them from interpretation by the shell.

find / -atime 7 -print - will print files accessed in exactly 7 days


find / -atime +7 -print - will print files accessed earlier than 7 days
find / -atime -7 -print - will print files accessed within 7 days
Working With Files
Head To display the first ten lines of the file

head <filename>

Tail To display the last ten lines of the file

tail <filename>

By default, 10 lines are displayed.


If there are any specific number of lines to be fetched, then it can be specified as follows:

Head -<number of lines to be fetched> <filename>


Tail - <number of lines to be fetched> <filename>
UNIX Commands
cat - Concatenate and display text files

cat try.c Display the contents of try.c on the screen


cat f1 > f2 Takes input from file f1 & puts it on file f2
cat f4 >> f2 Appends the contents of f4 to file f2
cat test* > report The file report contains all files beginning with test

wc - Word Count

wc [Options] filename
Options - Display no. of lines, words, characters
-l Display no. of lines
-w Display no. of words
-c Display no. of characters
e.g., $ wc test.c
20 200 5678
20 lines, 200 words, 5678- characters

nl - no. of lines in the file and temply lists out the file. Similar to wc -l < filename >
UNIX Commands
touch Updates access, modification or change times of a file
-a update access time
-m update modification time
-c prevents creating the file
e.g., $ touch f1
* The current system date & time stamp is put on the file f1
* If f1 does not exist then it is created with 0 bytes

cmp Compare two files

If files are same no output is sent to the terminal, or else The line number and
the byte at which the first difference occurs is reported

-s Outputs nothing Registers return code


Returns 0 if files are identical, 1 if files are different and 2 on error
e.g.,
cmp test1 test2
test1 and test2 differ in char 36 line 3
cmp -s test1 test2
echo $? => Output will be 1 indicating that the files are different
UNIX Commands
diff - Reports more than one differences
$diff [Options] file1 file2

e.g., $ diff test1 test2


Outputs: n1 a n3,n4
n1,n2 d n3
n1,n1 c n3,n4
where * n1 ,n2, n3 ,n4 are line numbers
* a ,d, c means append, delete ,change respectively
comm Display common lines
$comm -[123] f1 f2
Prints a three column output:
- lines that occur only in f1
- lines that occur only in f2
- lines that occur in both
comm -12 - prints lines common to the two files
comm -23 - prints only lines in the first file but not in the second
comm -123 - prints nothing
UNIX Commands
split Splits the file into different files as specified by the number of lines
e.g., $ split -20 test.c
Splits the file test.c in blocks of 20 lines and creates files xaa, xab, xac
and so on, such that
xaa has first 20 lines of test.c
xab has the next 20 lines of test.c
...
The file test.c is unaffected
$ split-20 test.c try Generates files as tryaa , tryab , tryac

paste Joins the two or more files horizontally


e.g., $ paste xaa xab
File xaa and xab are joined horizontally and output to the terminal
UNIX Commands
umask Set file creation mode mask

$ umask nnn (nnn set file creation mode)


umask can also be set as a shell variable

e.g., umask 022 - Files normally created with 777 mode is assigned 755
permission. The value of each digit is subtracted from the corresponding "digit"
specified by the system for the creation of a file.

pack Compress the file

$ pack <filename>
e.g., $ pack try - Creates a file try.z which is packed
- Normally the executables are packed
- The size is reduced by 25 - 40 %

unpack or pcat Uncompress packed file

e.g., $ unpack try.z or $ pcat try.z

unpacks the file try.z


UNIX Commands

mount Associates a directory with a device


e.g., Mounting a floppy on the root file system

umount Dissociates directory from the device


e.g., $ mount /dev/fd096 /mnt Mounts the floppy on the directory /mnt
$ umount /mnt Dissociates /mnt from the floppy
passwd To change the password
cmp Compare two files
If files are same no output is sent to the terminal, or else The line
number and the byte at which the first difference occurs is reported

-s Outputs nothing Registers return code


Return code
0 if files are identical
1 if files are different
2 on error

e.g., $ cmp test1 test2


test1 and test2 differ in char 36 line 3
$ cmp -s test1 test2
$ echo $status => outputs 1 indicating that the files are different
UNIX Commands

file Determine file types


$file [Options] [Arguments]

Options -f filelist
Normal File Types
- C program text
- assembler program text
- commands text
- ASCII text
- English text
e.g., $ file test.c
Tools : Filters & Utilities
Grep Tr
Fgrep Sed
Egrep Tac
Look Od
Cut Dc
Paste Bc
Join Tee
Sort Script
Uniq Cal
Cmp Date
Comm Zcat
Diff Zmore
Patch Zgrep
Dircmp zdiff
Tools : Filters & Utilities
Grep - Searches through one or more files for lines containing a target
and then prints all of the matching lines it finds

$ grep pattern filenames

Options :

-c number of lines matched

-i ignore case

-n print line with line-number

-v print lines which do not match

-l list filenames only


Tools : Filters & Utilities
Regular expression symbols commonly used with grep

Symbol Meaning Example Matches

. Matches any character chil. chile, chili

* Matches zero or more repetitions of


the preceding character ap*le ale or apple

[] Matches any of the characters


enclosed in the brackets [Cc]hicken Chicken, chicken

[a-z] Matches any character in the specified


range [A-Za-z] Any alphabetic string

^^ Beginning of line ^Unix Unix at the beginning


of the line

$ End of line Soup$ Soup at end of line


Tools : Filters & Utilities
Searching Files with grep

e.g.,

$ grep -ni func1 *.c Prints all the lines and line numbers in
files *.c that match pattern func1
ignoring the case

$ grep * * Search for the pattern * in all the files

$ ls -l | grep ^d Searches for all subdirectories


Tools : Filters & Utilities
Fgrep fast, several simple strings at one time

Similar to GREP but with 3 main differences

1) We can use it to search for several targets at once


2) It doesnt allow us to use regular expression to search for
patterns
3) Faster than grep

Eg: fgrep TCS SHA Files


Fgrep sue rache1 rebecca phone_list
Tools : Filters & Utilities
Egrep - extended grep, can handle more powerful expressions like | - or operators

Accepts all of the basic regular expressions recognized by grep


We can specify several targets by using vertical bar or pipe symbol
e.g.,

$ egrep int|long test.c Searches for all those lines containing


either int or long in test.c

$ egrep (^[A-Z]) testfile Searches for all the lines which


start with a capital letter

$ cat exprfile Searches for lines having at least 3 commas


,.*,.*, in file testfile using the exprfile

$ egrep -n -f exprfile testfile


Tools : Filters & Utilities

SED: Stands for Stream Editor. It is very useful for filtering text files

Sed 1d data => Deletes the first line of the file data
Sed 1,4 d data => To delete the first 4 lines
Sed /New York/ d states => Removes all the lines containing New York
from the file states
Sed s/1998/1999/g meetings => To replace the text 1998 to 1999
Sed n 10,20p file => To print lines 10 through 20 only
Redirection
Metacharacter Performs

< input
> output write

>> output append


e.g.,
$ who
trg1 tty00 Apr 8 09:33
trg2 tty02 Apr 8 11:10
$ who > who_out
$ cat who_out
trg1 tty00 Apr 8 09:33
trg2 tty02 Apr 8 11:10
$ date > date_out
$ cat date_out
Fri Apr 8 14 : 30 : 10 est 1983
$ who >> date_out
$ cat date_out
Fri Apr 8 14 : 30 : 10 est 1983
trg1 tty00 Apr 8 09:33
trg2 tty02 Apr 8 11:10
Pipes :

Metacharacter Performs
| piping

e.g.,

Without Pipe $ cat /etc/passwd > temp


$ sort < temp
$ rm temp

With Pipe $ cat /etc/passwd | sort

* No need for creation/deletion of a file


* Sorts the file passwd as per the first
entry in passwd
The TEE Utility :

Helps in creating the intermediate file during the pipe


operation

e.g.,

$ ls *.c | tee Cflst | sort


$ cat cflst

* Creates the intermediate file Cflst


* Cflst contains the list of c files
Sort

* Sort keys can be fields or lines


* A field is a string of characters separated by a field separator or new line.

$ sort {-options} {+post1} {-post2} {files}


The sort key begins at post1 ands ends just before post2. There can be several keys.

Options
b ignore leading blanks and tabs
c only check if input file is already sorted
d dictionary order letters, digits, blanks
f ignore case
i ignore non-printable characters
n numeric sorting
r reverse order
tp use p as separator
u output only lines with unique keys
o filenameout save output in filenameout
e.g.,

$ sort -t: +2n -3 /etc/passwd

Print the passwd file sorted by numeric user id

$ who | sort +4n

Sorts according to the login time stamp of the user


uniq Finds and eliminates duplicate lines in
a file and is often used with sort

$ sort <file> | uniq -c

Sorts and ouputs the number of count of lines


containing unique fields

$ sort <file> | uniq -d

Gives only the duplicated lines

tsort Accepts as input a partial ordering and produces a


fully ordered list of the items.

$ tsort psortfile
Processing Tabular Data

Cut

* Deletes columns from a file producing a new file


with shorter lines

* Cuts out selected fields of each line in a file.

* Cuts columns from a table or fields from a file


which can be of type

- Fixed length fields or


- Delimited by some character
Cut (contd.)
cut -c list { file1 file2 ...}
e.g.,
cut -c 5-70 file1
cut would pass 5-70 characters from file1

cut -flist {-d char } {file1 file2....}


e.g.,
who | cut -d -f1
gives a list of user login names
cut -d: -f 1,5 /etc/passwd
gives a list of user ID and names
JOIN

* Combines corresponding lines in two files by relating the


contents of one or more columns.

* Implements a relational data base join on two tabular files

-jn m join on the mth field of file n

-on.m output mth field of nth file


n - file no.
m - field no.

-tc use char c as separator


e.g.,

$ join -j1 4 -j2 3 -o 1.1 2.1 1.6 -t: etc/passwd etc/group

- joins field group id

- outputs the following parameters

login group login


name name dir
csplit context split
$ csplit [-k] [-f prefix] [-s] file name arg1 [..argn]

Reads file and separates it into n+1 section defined by arg1...argn

Options:

-s Normally csplit prints the character counts for each file ,


-s is to suppress this

-k csplit removes previously created files in case of error ,


-k is to avoid this

csplit normally creates file as xx00 , xx01 ... , xx99

-f prefix creates file with that prefix instead of default xx


csplit(contd.)

e.g.,

$ csplit -f cobol inpfile /Procedure division/


/ Para5./ /Para6./ /Para 7./

* Creates file as cobol 00 .. cobol 03


* Edit these Cobol files
* Can be recombined as
$ cat cobol 0[0-3] > file

csplit -k prog.c %main(% /^}/+1 {20}

* Breaks file prog.c containing C routine


upto a maximum of 21

* % expr % > no. file is created for this section

* No csplit for the main routine %main%


PS, kill
$ somecommand &

5511 - pid
$ps

pid tty time command

3432 2 0 : 24 -sh
5765 2 0 : 03 ps
5511 2 0 : 51 somecommand

$ kill 5511
$ ps
pid tty time command

3432 2 0 : 24 -sh
5985 2 0 : 03 ps
$ stubborn-cmd &

pid tty time command


3432 2 0 : 24 -sh
6004 2 0 : 03 ps
5995 2 0 : 44 stubborn-cmd
$ kill 5995
$ ps
pid tty time command
3432 2 0 : 24 -sh
6004 2 0 : 03 ps
5995 2 0 : 44 stubborn-cmd
$ kill -9 5995
$ ps
pid tty time command
3432 2 0 : 24 -sh
6103 2 0 : 03 ps
Pid process id
Tt displays the device name of the terminal that controls each process, if any
Stat displays a representation of the state of each process
Time- displays the amount of cpu time that each process has consumed
Command displays the command program associated with each process.

Stat parameters:
D means that the process is waiting for a turn to write to disk
I - that the process is currently idle & has been inactive for longer than 20 secs
J - process is in jail its scope of operation is limited to a certain predefined are of the system.
R process is runnable though not necessarily running currently
S process is sleeping and has been inactive for less than 20 secs
T - process has been stopped
Z process is dead. Such a process is called zombie
E trying to exit
< - process with high priority
+ - indicates a foreground process
Timely Execution :

time Time a command

$ time wc test.c > wc.test

real 2.0
user 0.4
sys 0.3
nohup Protecting a process from hanging and quit signals
or interrupts
The standard output is sent to nohup.out
e.g.,
$ nohup du / &
820
Sending output to nohup.out
$ logout
nice * Executes at specified priority
* Default priority is 24

$ nice +n Raise priority

$ nice -n Lower priority

$ nice n set priority

at Executes process at specified time

$ at 5 pm
echo ^G ^G Time to logoff >/dev/tty04
^D
$
User Backup Utilities :

tar tape archiver


Copies files on backup medium such as floppy
or tape in tar format

tar [options] devicename filelist

Options :

c create a new tape backup tape old files are overwritten


r append files to the tape
t list the names of files from backup tape
x extract files from the backup medium
u update a tape, if the named files are not present or have
been modified later on
v verbose ; provides informational messages, such as the
name of each file as and when it is encountered
f devicename use device for backup medium
tar (example)

$ tar cvf /dev/fd096 * copies all files from the current directory
onto the backup medium /dev/fd096

$ tar xvf /dev/fd096 Extracts all the files from the backup medium
/dev/fd096 onto the current directory

Creates necessary directories

tar xvf /dev/fd096 try.c Extracts the file try.c from /dev/fd096

tar tvf /dev/fd096 Generates a file list of /dev/fd096

find / -mtime -7 -exec tar uf /dev/fd096 {} \;


All the files modified last week is backed up onto /dev/fd096
cpio * Copy files archives in and out
* Bundles all the files into one package
cpio [options] file list devicename
Options

-o Copy these files onto tap


-i Extract files from tape
-p Read a list of file or path names from the standard input
-v display a verbose set of cpio action
-c character information in ASCII for portability considerations
-t For listing files from the backup medium
-O Append files to the end of tape to be used with -A
-d Creates necessary directories
-k In case of error , ignore the error and continue
cpio (Examples)

$ ls *.c | cpio -ocBv > /dev/rmt/0n

Lists the files *.c and copy onto the device /dev/rmt/0n

$ cpio -ocBv < filelst > /dev/rct/c0s0

Copies all the files mentioned in filelst onto the catridge tape

$ cpio -icBdv < /dev/rct/c0s0

Extracts all the files from device c0s0 and creates necessary required directories

$ cpio -itv < /dev/rmt/0m

List the files from the tape /dev/rmt/0m


dd convert, reblock, translate, copy a tape file.

dd {option = value }

Options Values

if input file name


of output file name
ibs input block size (Default 512)
obs output block size(Default 512)
cbs conversion block size
skip n records before copying from I/P file
seek write after n output records in O/P file
count n records
conv To ASCII, EBCDIC, lcase, ucase (separated by
a comma)
e.g.,
$ dd if=/dev/rmt/0n of=x ibs=800 cbs=80 conv=ascii,lcase
doscp Copy a UNIX file in DOS format
or
Copy a DOS file in UNIX format

doscp source file target file

e.g.,

$ doscp /usr/trg/test.c a: copies the file test.c onto the floppy

$ doscp a:try.c . copies the file try.c from the floppy onto
the current working directory
Output Related Commands :

pr prepares a file for printing


Options
-k K col. Output
+k from page k
-lk set length of page to k lines
-p pause after each page
-h take next argument as header
-wk set width to k characters
-d double space
Eg: $ pr -3 d h file list f1 f2
- Generates a header as file list, 3 Column output, Double spacing
$ pr -5 wordlist
- Generates 5 column output
pr (contd.)

$ pr myfile
prepare myfile
nov 24 : 11 : 31 1987 myfile page 1

$ pr -t myfile
suppresses the header

This file can be printed using


$ pr myfile | lpr
lpr Print a file on the line printer

$ lpr myfile prints myfile on the printer

$ lpr -c myfile make a copy to the spool directory

$ lpr -r myfile myfile is removed from the spool

lp Combines the actions of pr and lpr

$ lp myfile prints myfile with header, date, time and


page numbering.
Other Utilities

sleep Suspend execution


$ sleep 5 prompt appears after 5 seconds

$ sync * updates super block


* writes all disc buffers
* calls sync before stopping system to ensure file
system integrity
* saves all modifications

$ wait waits till all background jobs are over


$du determines disk usage
-s total blocks
-a size of each file
df displays no. of free blocks
df [option] file system

clear clears the screen and the prompt goes to


top of screen
tr replaces specified characters with other
characters
e.g.,
$ tr [a-z] [A-Z] < lfile > ufile
Replaces all small case letters to upper case letters

$ tr -d < tstfl > tstfl2


Deletes all the blank characters

$ tr -s \012 < try > try2


Removes adjacent blank lines in file try
make - An Introduction

Making & Maintaining large programs

What is make? Program for maintaining large number of programs

Need for make * Difficulties in remembering the dependencies

* Simple mechanism for maintaining an up-to-date


version of Programs

Characteristics * Helps maintain large systems

* Specifies dependencies of files in the system and t


the actions to make them

* Uses creation date to determine the actions to


be taken
make : AN INTRODUCTION Contd...

make program takes the file named makefile or Makefile as its input.

makefile details:
- the names of the files that make up the program system
- their interdependencies
- how to regenerate the program system

Example :
final

mod1 mod2 mod3


make : AN INTRODUCTION Contd...
Example (contd):
Makefile or makefile

final : mod1.o mod2.o mod3.o


cc -o final mod1.o mod2.o mod3.o
mod1.o : mod1.c
cc -c mod1.c
mod2.o : mod1.c
cc -c mod2.c
mod3.o : mod3.c
cc -c mod3.c

Run the command as ....


$ make final
use -f urflname option If the name of the file is not
Makefile or makefile
UNIX Process
Multitasking is achieved using processes. Whenever you execute a program, that
program is assigned to a process.
Unix shares available CPU time between processes, which makes it appear that
they run concurrently.
On execution, the binary file doesnt execute but a copy of its code known as
image is loaded into memory, assigned to a process and executed.
The UNIX kernel handles process management, and one of the first processes it
starts when you log in is the shell. Once running, shell programs have their own
system calls for creating new processes.
The action of creating a new process is called forking, and is achieved using the
fork system call.
Job Scheduling
Job scheduling in UNIX is the ability to set up a process or job to
execute at a specific time. It can mean the queuing of tasks so that
they execute one after the other.

Standard tools for scheduling jobs:

At The at tool is a program that can read commands from either standard
input the keyboard or a file for later execution. You normally use it to schedule
one-off jobs.

Cron The cron tool is a system daemon that UNIX uses for handling timing.
You can schedule jobs to run regularly using a specially formatted crontab file.
Job Scheduling
Using at:

You can use the at command to schedule jobs that you only want to run once, at a specific
date and time. You start an at session using the syntax

At time [date]

The at command requires root privileges to run

You can specify time in HHMM OR HH:MM format, and you can optionally add a date in
MMDDYYYY format. If you prefer, you can specify time using the keywords midnight, noon
and teatime. Teatime is considered to be 4:00 pm.

Unix assigns a job number to the task.


Options with at command:
-m You use the m option to have at send you mail when your job completes.
-f You use the f option with a filename to specify a file as the source for the commands
you want to execute. No at prompt will appear when you use this option.
-c You use the c option to cat the commands you enter into at back to standard output
when you are done.
Job Scheduling
At Examples:

At 6 am Friday
Echo Dont forget the meeting
CTRL-D

At f scriptfile 6 am Monday

At l => To list all the scheduled at jobs

At r => To remove the job from queue


Job Scheduling
Crontab (CRON TABle)is a program that manipulates the CRON daemon, making it easy for
users to schedule task and run programs/commands at pre determined periods of time.
Crontab can also be considered a file witch contains commands that will be run by the
system as the user that owns the crontab file.

Purpose of Crontab
Cron is designed to maintain a list of commands that the system needs to run at a given
time interval.

For example if you have a script that generates statistics and needs to be run every couple
of hours or everyday cron can do it for you. Or for example if you have a script that sends a
newsletter every month you can use cron to run the script that sends the newsletter every
month or week.

Crontab commands

When your logged in to your server you can use program cron using the following commands:

crontab -l => Lists the current cron jobs


crontab e => Edit your current crontab file and ad/remove/edit crontab tasks.
crontab r => Remove the crontab file.
crontab v => Displays the last time you edited your crontab file.
Job Scheduling
Components of crontab

MIN HOUR DOM MOY DOW COMMAND


(0-59) (0-23) (1-31) (1-12) (0-6)

When you enter the edit mode (crontab -e) and start adding tasks to your cron file you
need to consider the following syntax:

The asterisk (*) symbolizes that every instance of that field (i.e. every minute, hour, day,
month, weekday) will be used in the command.

Example on how to setup your first crontab


Lets say you have a script named run-me.sh located in /home/your_username you want to
run every day at 13:30. You will need to login your server console and input the following
commands:

crontab -e

This will open the crontab file and let you edit it. By default this file will be opened with the
VI editor and you will need to press the Insert key on your keyboard to be able to write in
that file.

30 13 * * * /home/your_username/run-me.sh >/dev/null 2>&1


Job Scheduling
The first character you see is 30 this means that crontab will run the script every time the
clock hits the 30 minutes mark. Next 13 this means that crontab will run the script when
the clock hits 13. The next three * tell crontab to run the script every day, of every month
of every weekday. Combining these fields crontab will run the script every day at exactly
13:30. You may notice that we added the >/dev/null 2>&1 string at the end of the
command. The default cron job will always send and e-mail to the root account when ever
a command is executed. Now you don't want to be notified every day that your crontab job
has been executed. If you don't want to receive e-mails every day notifying you about your
job's execution place this >/dev/null 2>&1 at the end of every instance of every crontab
command.

When you are finished adding your commands to the crontab file you need to save and exit.
If you are using VI as your editor you need to issue the following commands:

Press the Esc (Escape key) on your keyboard to enter the command mode of VI

After you pressed Escape then type the following characters :wq! and press Enter.
Remember you have to type this characters (remove the quotes): :wq!.
Job Scheduling
Now to list your crontab job just issue the following command: crontab -l
If you need to add another crontab job or even more all you need to do is follow the same
steps as above and just ad another line to the file.
REMEMBER: Every crontab job needs to be placed on its own line in the file and after the
last line you need to insert a non-braking character (press Enter).

The crontab syntax goes a little beyond its boundaries and has more advance meaning for
some users.
For example if you wish to use more then one instance of one column you will separate
those instances by a comma , or if you wish to use a time period lets say for example
from Sunday till Tuesday you will use dash -.

10,20,30 13 * * 0-2 /home/your_username/run-me.sh >/dev/null 2>&1


This crontab job will run your scrip run-me.sh on from Sunday until Tuesday (Sunday,
Monday, Tuesday) at 13:10, 13:20 and 13:30. Remember there are no spaces between
values separated by commas , and neither in dashes -. There is also an operator that
some versions of cron support called the slash operator / that can be used to skip a given
number of values in your jobs.
Job Scheduling
Crontab examples:

Run the script every day at 12:00.


0 12 * * * /home/your_username/run-me.sh >/dev/null 2>&1

Run the script every day at 12:00, 14:00 and 16:00.


0 12,14,16 * * * /home/your_username/run-me.sh >/dev/null 2>&1

Run the script every Sunday at 13:30.


30 13 * * 0 /home/your_username/run-me.sh >/dev/null 2>&1

Run the script every Saturday at 12:00, 14:00 and 16:00.


0 12,14,16 * * 6 /home/your_username/run-me.sh >/dev/null 2>&1

Run the script on the first, fifteenth and twentieth of every month.
0 0 1,15,20 * * /home/your_username/run-me.sh >/dev/null 2>&1
Run the script every day from 3 to 3 hours: 00:00, 03:00, 06:00 etc.
0 */3 * * * /home/your_username/run-me.sh >/dev/null 2>&1

Potrebbero piacerti anche