Introduction To Unix
Introduction To Unix
1
Understanding Initialization Files
When users log in to the system, their login shells look for and execute two different types of
initialization files. The first type controls the system-wide environment. The second type controls
the user’s environment.
The two main system initialization files are called /etc/profile and /etc/.login.
The Bourne and Korn login shells look for and execute the system initialization file /etc/profile
during login.
The C login shell looks for and executes the system initialization file /etc/.login during the login
process.
The default files /etc/profile and /etc/.login check disk usage quotas, print the message of the day
from the /etc/motd file,and check for mail.
user’s initialization files are setup in the user’s home directory. They define the characteristics of a
user’s work environment, such as a user’s search path, environment variables, and windowing
environment.
2
Layered System
USER Ot USER
ds he
el
l a n r Ap
Sh
m pl
Sh
e
o ica
ll
C Kernel tio
n ix ns
U
HARDWARE
Co
m S
pi
ler BM ll
Sh s RD e
ell Sh
USER USER
3
UNIX File System
ro o t (/)
hom e
jo h n ra k e s h syed
4
bin system binary executables
dev file names for system devices, terminals, disks etc
tmp scratch pad directory for temporary files. Must have read/write
access
5
UNIX Commands
$ touch sample
Creates a file called ‘sample’ with zero size if ‘sample’ is not present in the current directory
Changes the last modified time and access time to current system date and time if ‘sample’ is
already an existing file
-a Change the access time of file.
-m Change the modification time of file.
-t time Use the specified time instead of the current time. time will be a decimal number of
the form: [[CC]YY]MMDDhhmm [.SS]
MM The month of the year [01-12]
DD The day of the month [01-31]
hh The hour of the day [00-23]
mm The minute of the hour [00-59]
CC The first two digits of the year
YY The second two digits of the year
SS The second of the minute [00-61]
$ cat test
Displays the contents of the file ‘test’ on to standard output if already exists
6
$ cat > test
creates the file ‘test’ and writes the contents of standard output to ‘test’ till ctrl+d is pressed.
creates the file sample3 and writes the contents of sample1 & sample2 to sample3.
If sample3 already exists, it overwrites its content with sample1 & sample2
if >> operator is used,contents of sample1 & sample2 will be appended to content of sample3
$ cp temp.txt test.txt
$ cp /usr/home/mydir/temp1 /usr/home/testdir/temp2
copy the file temp1 from one path to another path with filename as temp2
7
$ mv mydir tempdir
file1 & file2 are moved to the ‘mydir’ and are no longer present in the current directory.
$ mv test.txt sample.txt
$ ls -l
Gives the directory listing. -l option gives the long list of the contents
-a Lists all entries, including those that begin with a dot (.), which are normally not listed.
-t Sorts by time stamp (latest first) instead of by name.
-r Reverses the order of sort to get reverse alphabetic
-i Displays the inode number of the file specified
8
$ rm file1
This command changes the file permissions for owner,group & others
read permission wil have a value 4,write has 2, execute has 1 and 0 has no permission
The ‘myfile’ will have permission for owner as -rwx, for group ---, for others ---
+r,+w,+x will give read,write & execute permissions to all the three. -r,-w,-x is vise versa
$ umask
022
Umask stands for user file creation mask.The value of this variable decides the masking of
file permissions when a file is created
By default UNIX assumes that the permissions for file should be 666 and for directory 777. If
the umask value is 022 then the file actually created with (666 - 022 = 644).
9
$ mkdir mydir
creates the directory ‘mydir’
$ mkdir -p work/temp/mydir
- p option creates multiple generations of directories at one go.It first creats ‘work’ then within it
creates ‘temp’ and in temp creates ‘mydir’
$ rmdir work/temp/mydir
rmdir removes only the empty directories.Removes the directory ‘mydir’.
To remove the parent directories, use -p option.It removes only if the directories are empty.
$ cd work/temp/mydir
changes the directory path to work/temp/mydir.
If only ‘cd’ is is given without any arguments, the path changes to user’s home directory.
$ pwd
Displays the present working directory.
$ who
Displays the login information login name, terminal number, date & time of login about all the users
who have logged into the system currently.
10
$ who am i
Displays your login information login name, terminal number, date & time of login
$ ps -ef
The ps command prints information about active processes. Without options, ps prints
information about processes that have the same effective user ID and the same controlling
terminal as the invoker. The output contains only the process ID, terminal identifier,
cumulative execution time, and the command name.
-e option lists information about every process now running.
-f option generates a full listing.
$ tty
Displays the name of your terminal file.
For each terminal on the n/w, Unix uses a file.The output displayed on its monitor is picked
from the file associated with the terminal.All these files are present in ‘/dev’ directory.
Unix understands each terminal as a special file.
$ logname
Prints the login name of the user
$ id
Prints the userid and group id of the user
11
$ uname
Displays the name of the unix system we are using.
$ df
The df command displays the amount of disk space occupied by mounted or unmounted file
systems, the amount of used and available space, and how much of the file system's total
capacity has been used. The file system is specified by device, or by referring to a file or
directory on the specified file system.
-b option print the total number of kilobytes free.
-e option print only the number of files free.
$ du
Summarize the disk usage
The du utility writes to standard output the size of the file space allocated to, and the size of
the file space allocated to each subdirectory of, the file hierarchy rooted in each of the specified
files.
-k Write the files sizes in units of 1024 bytes, rather than the default 512-byte units.
-s Instead of the default output, report only the total sum for each of the specified files.
$ file temp
Determines the file type of the file ‘temp’ in the current directory.
If ‘*’ is specified instead of a file name,it displays the type for all files in the current directory.
12
$ wc file1 file2
prints the number if lines, number of words and number of characters in each file
-l option displays only the number of lines in the file
-w option displays only the number of words in the file
-c option displays only the number of characters in the file
13
$ cut -f 2,7 myfile
Use the cut utility to cut out columns from a table or fields from each line of a file.
-f option is for splitting based on fields (Tab delimited). If specified 2,7 it means cut only 2nd and
7th fields.If specified as 2-7 it means cut from 2nd to 7th field and display on to standard output or
redirect to a file
-d option to recognize the field separator other than tab like ‘:’ or ‘,’
14
$ cat file1 > file2
The contents of file1 are copied to file2.It overwrites the content of file2 if file2 already exits
otherwise file2 is created and written with file1 content.
The ‘>>’ operator appends the content of file1 to file2.
$ ls | wc -l
Here the piping command ‘|’ is used to to join commands.In this case, wc command takes the
input from ‘ls’ command.Piping makes the output of first command as an input to the second
command.
$ uniq myfile
report or filter out repeated lines in a file
The uniq utility will read an input file comparing adjacent lines, and write one copy of each
input line on the output.The second and succeeding copies of repeated adjacent input lines
will not be written.Repeated lines in the input will not be detected if they are not adjacent.
-d option suppress the writing of lines that are not repeated in the input.
-u option suppress the writing of lines that are repeated in the input
15
$ diff file1 file2
The diff utility will compare the contents of file1 and file2 and write to standard output a
list of changes necessary to convert file1 into file2. This list should be minimal. No output
will be produced if the files are identical.
-b option Ignores trailing blanks (spaces and tabs) and treats other strings of blanks as
equivalent.
-i option Ignores the case of letters; for example, `A' will compare equal to `a'.
16
$ tee -a myfile
The tee utility will copy standard input to standard output,making a copy in zero or more files.
tee will not buffer its output.The options determine if the specified files are overwritten or
appended to.
-a option append the output to the files rather than overwriting them.
$ finger hpsid
Displays information about local and remote users.
$ history
Process command history list .The command history list references commands by number. The
first number in the list is selected arbitrarily. The relationship of a number to its command will
not change except when the user logs in and no other process is accessing the list, at which
time the system may reset the numbering to start the oldest retained command at another
number (usually1).
Use ‘set history = n’ to set the number of history commands to be displayed to ‘n’.
$ kill 21496
kill will terminate a process by its ID
17
$ nice [ -increment or +increment ] [ command ]
Changes the priority of a process .The nice utility invokes command, requesting that it be run
with a different system scheduling priority.
increment must be in the range 1-19; if not specified, an increment of 10 is assumed. An
increment greater than 19 is equivalent to 19. The super-user may run commands with
priority higher than normal by using a negative increment such as -10.
A negative increment assigned by an unprivileged user is ignored.
$ compress myfile
The compress utility will attempt to reduce the size of the named files
Each file will be replaced by one with the extension .Z, while keeping the same
ownership modes, change times and modification times.
The amount of compression obtained depends on the size of the input, the number of bits
per code, and the distribution of common substrings. Typically, text such as source code or
English is reduced by 50-60%. Compression is generally much
$ uncompress myfile.z
The uncompress utility will restore files to their original state after they have been
compressed using the compress utility. If no files are specified, the standard input will be
uncompressed to the standard output.
$ zcat myfile.z
The utility will write to standard output the uncompressed form of files that have been
compressed using compress. It is the equivalent of uncompress -c. Input files are not affected.
18
$ pack myfile
The pack command attempts to store the specified files in a compressed form. Wherever
possible (and useful), each input file is replaced by a packed file file.z with the same access
modes, access and modified dates, and owner as those of file. If pack is successful, file will
be removed.
The amount of compression obtained depends on the size of the input file and the
character frequency distribution. Typically, text files are reduced to 60-75% of their original
size.
-f option forces packing of file. This is useful for causing an entire directory to be packed
even if some of the files will not benefit. Packed files can be restored to their original form
using ‘unpack’ or ‘pcat’.
$ at 17:00
at> echo “Its 5 PM now”
ctrl+d
job 1056140340.a at Fri Jun 20 14:49:00 2003
The at utility reads commands from standard input and groups them together as an at-job, to
be executed at a later time. The above example shows after the echo statement given,when
ctrl+d is pressed,it displays the job id and time of execution
$ batch
The batch utility reads commands to be executed at a later time decided by the system
Instead of we specifying that our commands be executed at a precise moment in time
19
$ crontab -e
30 17 * * 5 /usr/bin/banner "Time to go!" > /dev/console
The crontab utility manages a user's access with cron by copying, creating, listing, and
removing crontab files. If invoked without options, crontab copies the specified file, or the
standard input if no file is specified, into a directory that holds all users' crontabs.
A crontab file consists of lines of six fields each. The fields are separated by spaces or
tabs. The first five are integer patterns that specify the following: minute (0-59), hour (0-23),
day of the month (1-31), month of the year (1-12), day of the week (06 with 0=Sunday).
-e option edits a copy of the current user's crontab file, or creates an empty file to edit
if crontab does not exist. When editing is complete, the file is installed as the user's
crontab file. Each entry in the crontab file looks as shown in the example above.
20
vi Editor Commands
vi filename to enter editor with existing or new file named filename
Cursor control:
kG go to line k
/text, ? text forward or backward search for the given text
Arrow keys to move cursor
^F, ^B to scroll the screen forward and backward, respectively
21
To insert text: (ESC to end insert)
To delete text:
22
Cut and paste:
23
Command Mode - after execution, returns to command mode
24
Operators used in the Command Mode:
dd deletes a line
deleted text is stored in a temporary buffer whose contents can be pasted using the p
command.
c deletes indicated text starting at the cursor, then enters the text input mode.
ce deletes from the cursor to the end of the word
y copies the indicated text, staring at the cursor, and stores it in a buffer. There are
nine unnamed buffers that store the last nine delete or yank operations and 26 named
buffers (a-z) that can also be used for storage. A double quote is used to
specify the name of the buffer.
"cy$ stores the text from the cursor to the end of the line in buffer c
p the put command, places the delete or yank buffer contents after the cursor or on the
next line.
p puts the last item yanked or deleted back into the file just after the cursor "cp puts
the contents of buffer c after the cursor
P identical to the p command, but places the text before the cursor
25
Scopes for use with operators:
26
Shell Programming
In the above example, a shell variable called files is created and made equal to each file (the
wild card *) in the root directory (/) in turn. For each occurrence of a directory entry, the shell
variable files is assigned the name of the file, then its contents (denoted by $files) is echoed to
the console screen.
27
Shell Arguments and quotes:
Positional Parameters:
Shell creates positional parameters which match each argument supplied to the shell script
The name of shell program is stored in $0
The first argument is stored in $1
The second argument is stored in $2 and so on till $9.Use ‘shift’ keyword to store the subsequent
parameters. when it is used, each parameter moves one to the left
28
Total number of arguments supplied is stored in $#
$* List all the shell arguments.It cannot yield each argument separately.
$@ yield each argument separately when enclosed in double quotes.
Example:
# echoall
while test $# != 0
do
echo $1 $2 $3 $4 $5 $6 $7 $8 $9
shift
done
Special Parameters:
29
Shell Conditional Tests:
30
Example:
if test $# -lt 1
echo must supply a single argument
else
echo the argument is $1
fi
Example:
command = who
output = ‘| wc -l’
eval $command $output
31
Taking decisions:
Example:
if cp $source $target
then
echo File copied successfully
else
echo Failed to copy the file
fi
32
The case control structure:
multiple test conditions
shell attempts to match each pattern in turn
a double semi-colon terminates the command
for each pattern list only one pattern is matched by the shell
the exit status is that of the last command executed
if no command was executed, a zero status is returned the pattern *) is a default used if none of the
other patterns match
case string in
pattern) command-list ;;
pattern) command-list ;;
*) command-list;;
esac
Example:
echo “Enter a number from 1to 3: \c”
read num
case $num in
1) echo You have entered 1 ;;
2) echo You have entered 2 ;;
3) echo You have entered 3 ;;
*) echo Enter digits only from 1 to 3 ;;
esac
33
The While loop:
Repeats a group of commands a number of times, depending on a condition
The commands are repeated whilst the exit status of that for the condition is zero
example:
count=1
while [$count -le 10]
do
echo $count
count=`expr $count + 1`
done
34
The until loop:
Repeats a group of commands a number of times, depending on a condition
The commands are repeated whilst the exit status of that for the condition is non zero
example:
# print numbers from 1to 10
i=1
until [$i -gt 10]
do
echo $i
i=`expr $i + 1`
done
35
The for loop:
A shell variable is used to match a series of patterns
The commands are executed once for each match
example:
# print all the command line arguments provided to the program
for word in $*
do
echo $word
done
36
Shell functions:
A sequence of shell commands within a script file
Similar to procedures in PASCAL or functions in C
myfunc() {
command list;
}
37
The SED utility
SED stands for Stream line Editor
SED reads lines one at a time from the input files,it applies the commands from the list,in order,
to each line and writes its edited form on the standard output.
SED does not alter the contents of its input files. It writes on standard output.It outputs each line
automatically.
Example:
#search & replace a string from every line of input files and write to a file
Example:
# The substitution deletes all characters upto (.*) and including the rightmost tab (->).
Example:
# To print first few lines from an input file.
sed 3q sedtest1.txt
38
To print its input up to and including the first line matching pattern.
To delete every line that contains pattern.Does not actually delete from input file.
Automatic printing can be turned off by -n option.In this case,only lines explicitly printed with
a 'p' command appear in the out put.
sed -n '/pattern/p'
To replace each string of blanks or tabs with a newline and split its input into one word per
line.
To select a range of lines,we can use line numbers or pair of regular expressions.
39
sed -n '/^$/,/^end/p' filename # print each group of lines from an empty line to line starting with
end.
sed -n '/HPS/w file1 # Writes lines matching HPS on file1 & not matching to file2.
> /HPS/!w file2' filenames # ‘>‘ indicates the sed command should be continued by
pressing the return key to write to multiple files.
Example:
# To write the entire output to a file and also writes just the changed lines to another file
The above command write the entire output to sedresult.out and also writes just the
changed lines to u.out.
40
The AWK utility
AWK is a pattern scanning & processing language
The idea in AWK is much the same as in SED,but the details are based on the C language.
AWk reads the input from the filenames one at a time.Each line is compared with each pattern
in order.
For each pattern that matches the line,corresponding action is performed.
Here either pattern or action is optional.If action is omitted,the default action is to print matched
lines.
If the pattern is omitted,then action part is done for every input line.
41
AWK splits each input line automatically into fields.i.e;strings of non-blank characters seperated
by blanks or tabs.
AWk calls the fields $1,$2,.....$NF, where NF is a variable whose value is set to no.of fields.
$NF is the last field on the line.
In AWK unlike in shell,fields begin with $.They are not variables here.
Examples:
#Print the 2nd field from the output of ‘du -a’ command
du -a | awk '{print $2}’
#Print the 1st and 5th fields from the output of ‘du -a’ command
du -a |awk '{print $1,$5}'
Although AWk is easy to use for operations like above,it is usually slower compared to SED.
AWK normally assumes white space (any no.of blanks & tabs) separates fields.Here the
separator can be changed to a single character.
42
Examples:
# To print the user names which come from the first field
sed 3q /etc/passwd |awk -F: '{print $1}' # -F option changes the field seperator to ':'
Patterns in AWK:
43
Control Flow in AWK:
awk '
FILENAME != prevfile { # new file
NR =1 # reset line number
prevfile = FILENAME
}
NF > 0 {
if ($1 ==lastrecord)
printf "double %s,file %s,line %d\n",$1,FILENAME,NR
for (i=2;i<=NF;i++)
if ($i == $(i-1))
printf "double %s,file %s,line %d\n",$i,FILENAME,NR
if (NF > 0)
lastword = $NF
}' $*
44
Built-in variables used by awk:
Variables Description
45