Unix Programs
Unix Programs
1.) date
Aim:- to display the system date and time.
Systax:- date
Example:- date
Output:- Thu Sep 17 14:02:42 IST 2009
2.) who
Aim:- to display the name of all the user who have currently logged in.
Syntax:- who
Example:- who
Output:-
root :0 Sep 17 13:42
root pts/1 Sep 17 13:42 (:0.0)
root pts/2 Sep 17 13:43 (:0.0)
3.) who am i
Aim:- to display the name of the current user.
Syntax:- who am i
Example:- who am i
Output:-
root pts/2 Sep 17 13:43 (:0.0)
4.) cat
Aim:- Display the content of the file.
Syntax:- cat
Example:- cat unix
Output:-
[root@localhost ~]# cat unix
unix is a operating system.
5.) ls
Aim:- Displayl list of all files and directories.
Syntax:- ls[option][file name]
Example:- ls
Output:-
[root@localhost ~]# ls
anaconda-ks.cfg forex forex2 forex4 install.log mohan vivek
Desktop forex1 forex3 forex7 install.log.syslog unix
a.) ls -l
Aim:- display all files in long formate.
Syntax:- ls -l
Example:- ls -l
Output:-
[root@localhost ~]# ls -l
total 152
-rw-r--r-- 1 root root 1159 Jul 14 14:08 anaconda-ks.cfg
drwxr-xr-x 2 root root 4096 Jul 14 14:11 Desktop
-rw-r--r-- 1 root root 31 Sep 14 09:32 forex
b.) ls -a
6.) copy
Aim:- copy contents of file1 to file2.
Syntax:- cp file1 file2
Example:- cp unix linux
Output:-
[root@localhost ~]# cat linux
unix is a operating system.
7.) rename
Aim:- use to move or rename a file.
Syntax:- mv oldname newname
Example:- mv linux lin
Output:-
[root@localhost ~]# mv linux lin
[root@localhost ~]# cat lin
unix is a operating system.
8.) remove
Aim:- to remove a file from system.
Syntax:- rm filename
Example:- rm lin
Output:-
[root@localhost ~]# rm lin
rm: remove regular file `lin'? y
[root@localhost ~]# cat lin
cat: lin: No such file or directory
9.) wc
Aim:- To count the number of words,line,characters in a file.
Syntax:- wc[options][filename]
Example:- wc
Output:-
[root@localhost ~]# wc unix
1 5 28 unix
a.) wc -l
Aim:- it represents numbers of lines.
Syntax:- wc -l filename
Example:- wc -l unix
Output:-
[root@localhost ~]# wc -l unix
1 unix
b.) wc -c
Aim:- It displays the no. of characters.
Syntax:- wc -c filename
Example:- wc -c unix
Output:-
[root@localhost ~]# wc -c unix
28 unix
c.) wc -w
Aim:- It display the no. of words in a file.
Syntax:- wc -w filename
Example:- wc -w unix
Output:-
[root@localhost ~]# wc -w unix
5 unix
1.) mkdir
Aim:- To create directory.
Syntax:- mkdir directoryname
Example:- mkdir computer
Output:-
[root@localhost ~]# mkdir computer
[root@localhost ~]# ls
anaconda-ks.cfg forex forex3 install.log unix
computer forex1 forex4 install.log.syslog vivek
2.) pwd
Aim:- To display your current working directory.
Syntax:- pwd
Example:- pwd
Output:-
[root@localhost ~]# pwd
/root
3.) cd
Aim:- to change the current working directory.
Syntax:- cd directory name
Example:- cd computer
Output:-
[root@localhost ~]# cd computer
[root@localhost computer]#
Example:- cd ..
Output:-
[root@localhost computer]# cd ..
[root@localhost ~]#
4.) rmdir
Aim:- to remove the directory.
Syntax:- rmdir[option]directoryname
Example:- rmdir computer
Output:-
[root@localhost ~]# cat computer
cat: computer: No such file or directory
5.) chmod
Aim:- To change the mode or permission.
Syntax:- chmod<category><operation><permission><filename>
or
chmod<octalno>filename
Example 1:- chmod ugo+x unix
Output:- ls-l
Before:- -rw-r--r-- 1 root root 28 Sep 17 14:24 unix
After :- -rwxr-xr-x 1 root root 28 Sep 17 14:24 unix
Example 2:- chmod 744 forex
Output:- ls -l
Before:- -rw-r--r-- 1 root root 31 Sep 14 09:32 forex
After :- -rwxr--r-- 1 root root 31 Sep 14 09:32 forex
6.) ps
Aim:- To give the status of your processer that are running.
Syntax:- ps
Example:- ps
Output:-
[root@localhost ~]# ps
PID TTY TIME CMD
3964 pts/1 00:00:00 bash
3992 pts/1 00:00:00 ps
DIRECTED TO A FILE
Syntax :: /
Explanation :: Repeat the previous command
Output :: Unix system recognizes only three basic types of files : or
dinary file,direc
Syntax :: 1,$s/old_word/new_word/[ g ]
Explanation :: It will replace the old_word by new_word globally and will disp
lay all the lines that
contains the word
Output :: 1,$s/files/xxx/g
Unix system recognize only three basic types of xxx :
ordinary xxx, directory xxx, and special xxx. An ordinay file is just that : an
y file on the system that contains data, text, program instructions or just abou
t anything else.
Command:: ^
Explanation :: Matching the given character in the beginning of a line and pr
int the lines.
Output :: 1,$s/^/>>/g
Will add ( >> ) symbol to the beginning of all the l
ines.
>>Unix system recognize only three basic types of xxx
: ordinary xxx, directory xxx, and >>special xxx. An ordinay file is just that
: any file on the system that contains data, text, program >>instructions or jus
t about anything else.
Command:: $
Explanation :: Matching the given character in the eng of a line and print th
e lines.
Output :: 1,$s/\.$/g
Will search for the line ends with ( . ) and prints
the line
About anything else.
1,$s/$/>>/g
>>Unix system recognize only three basic types of xxx
: ordinary xxx, directory xxx, and>> >>special xxx. An ordinay file is just tha
t : any file on the system that contains data, text, program>> >>instructions or
just about anything else.>>
Syntax :: 1,$s/. .$//
Explanation :: Use to delete the given character. Here it will delete last tw
o character from each line
Output ::
>>Unix system recognize only three basic types of xxx
: ordinary xxx, directory xxx, and >>special xxx. An ordinay file is just that
: any file on the system that contains data, text, program >>instructions or jus
t about anything else.
1,$s/^$//
Will delete each line wich contains no character
>>Unix system recognize only three basic types of xxx
: ordinary xxx, directory xxx, and >>special xxx. An ordinay file is just that
: any file on the system that contains data, text, program >>instructions or jus
t about anything else.
Syntax :: 1,$s/[Matching character]//g
Explanation :: Use to delete matching characters and displays the lines
Output :: 1,$s/[A E I O U a e I o u ]//g
>>nxsystmrcgnznlythrbsc typsfxxx:rdnryxxx,drctryxxx,
nd >>spcl xxx.nrdnyflsjsttht:nyflnthsystmthtcntnsdt,txt,prgrm >>nstrctnsrjstbtny
thng ls.
Syntax :: 1,$s/^[Matching character]//g
Explanation :: It will invert the section .Use to delete characters except ma
tching characters and
displays the lines
Output :: 1,$s/^[A E I O U a e I o u ]//g
>>Uieeoieoeeaieooiaieoa eiaAoiaieiuaaieoeeaoaiaaeoa i
uioouaouaiee
Eg :: 1,$s/i.*e/+++/
Will replace all characters of a line from first I to last e by +++
Output ::
>>Un+++ ctory xxx, and
>>Spec+++xt, program
Eg :: 1,$s/[A-Z a-z][A-Z a-z]*/*/g
Will replace at least one alphabet,zero or more characters other than special ch
aracters by *
Output ::
*:*, *,*
.** :*,*,*
*.
SED :: This is a stream editor. It performs operation on a stream of bits. It i
s used to perform basic text tranformatio on an input stream. The related comman
ds are given below.
Syntax :: $ sed ‘command’ filename >tmp
mv tmp to filename
Explanation :: It will firstly save the output of the command to tmp and th
en the contains of the tmp
file will moved to the given file(filename)
Output :: $ cat abc
We are students of srm University
$ sed ‘s/srm/SRM/’ abc >tmp
$ mv tmp to abc
cat abc
We are students of SRM University
The -n option :: Supress automatic printing of pattern phase
Syntax :: $ sed -n ‘char p’ filename
Output :: $ sed -n ‘1-10p’ files
Prints first and second line
Unix system recognize only three basic types of xxx
: ordinary xxx, directory xxx, and special xxx. An ordinay file is just
that : any file on the system that contains data, text, program
Syntax :: $ sed ‘chard’ filename
Explaination :: Delete the specified line from a file
Output :: $ sed ‘1,2d’ files
Will delete the first and second line from the file “files”. And will p
rints the remaining lines
instructions or just about anything else.
Eg :: $ sed ‘Unix/d’ files
Will display the lines which not contain word Unix
Output ::
special xxx. An ordinay file is just that : any file on the syst
em that contains data, text, program instructi
ons or just about anything else.
# cat abc
Welcome to Linux
# ed abc
Syntax :: character\{min,max\}
Explanation :: It will match the given consucative characters
Output :: 1,$s/W\{1,10\}/g
Welcome to Linux
Eg :: 1,$s/^.\{10\}//g
Will delete first 10 characters from the file abc
Output :: Linux
The tr command :: Translate, squeeze, and/or delete characters from standard i
nput and writing to standard output.
Syntax :: # tr from_char to_char <filename
Output :: # tr e x <files ( filename )
It will changes all appearences of e to x.
>>Unix systxm rxcognizx only thrxx basic typxs of fil
xs : ordinary filxs, dirxctory filxs, and >>spxcial filxs. An ordinay filx is j
ust that : any file on the system that contains data, txxt, program instructions
or just about anything elsx.
# tr a-z A-Z <files
Will changes the lowercase letters to upper case letter
Output ::
UNIX SYSTEM RECOGNIZES ONLY THREE BASIC TYPES OF FILES : ORDINARY FILES, DIRECTO
RY FILES, AND SPECIAL FILES. AN ORDINARY FILE IS JUST THAT : ANY FILE ON THE SYS
TEM THAT CONTAINS DATA, TEXT, PROGRAM INSTRUCTIONS OR JUST ABOUT ANYTHING ELSE.
Syntax :: $ command | tr char_from ASCII_value
Explanation :: Take output of the command and perform operation as by given AS
CII char
Output :: $ date | tr ‘ ‘ ‘\12’
Will give new line instead of blank space
Thu
Jul
23
14:54:48
IST
2009
QUOTES
SINGLE QUOTE
SYNTAX: $ grep text filename
EXPLANATNION: Single quote is to keep characters otherwise separated by white
spaces characters together.
EXAMPLE 1: $ grep susan goldberg phonebook
OUTPUT: susan goldberg 9987650800
EXAMPLE 2: $ file=/users/steve/bin/prog1
$ echo $file
OUTPUT: $file
DOUBLE QUOTE
SYNTAX: $ echo “textâ€
EXPLANATION: Double quotes are not restrictive.double quote say to ignore mo
st. But the three characters are not ignored inside double quotes.
Dollar signs
back quotes
backslashes
EXAMPLE 1: $ x=*
$ echo “$xâ€
OUTPUT: *
EXAMPLE 2: $ address=†39 east 12th street
> new york, N. Y. 10003
$ echo “$addressâ€
BACKSLASH
SYNTAX: \c
EXPLANATION: The backslash quotes the single character that immediately foll
ows it.The general format is \c. where c is the character we want to quote. Any
special meaning normally attached to that character is removed.
EXAMPLE 1: $ x=5
$ echo “The value of x is \†$x\†â€
BACKQUOTE
SYNTAX: command
EXPLANATION: Backquote tells the shell to execute the enclosed command and
to insert the standard output from the command at that point on the command line
.
EXAMPLE 1: $ echo The date and time is: date
OUTPUT: The date and time is: wed sep 16 14:28:43 EDT 2002
EXAMPLE 2: $ echo current working directory is pwd
OUTPUT: current working directory is /root
PASSING ARGUMENTS
Aim:
To learn the how to pass arguments to program
Commands:
1. Argument passing
Syntax:
$# variable
Example:
$cat args
echo S# argument passed
echo arg1=:$1:
echo arg2=:$2:
echo arg3=:$3:
$sh args 10 20 30
Explanation:
This will take three arguments as input and display them
OutPut:
Argument passed
arg1=10
arg2=20
arg3=30
2. Another method for argument passing
Syntax:
$*variable
Example
$cat args2
echo $#
echo they are:$*
$sh args2 12 3 4 5 60
Explanation:
This will take many arguments and display them
Output:
they are:12 3 4 5 60
3. This is used to search the name from another file
Syntax:
grep $1 filename
Example:
$cat lu
grep $1 phonebook
$ sh lu chebba
Explanation:
This will search the given name is available in exiting file are
not.
Output:
Chebba 977-764-786
4. This will add new text to exiting file
Syntax:
echo “$1 $2” filename
Example:
$cat add
Echo “$1 $2”>>phonebook
$sh add ‘pizza’ 987-342-675
$cat phonebook
Explanation:
This will add a new text to another file by pasing argument from
a add file.
Output:
Pizza 987-342-675
5. This will take arguments from terminal and display using shift
Syntax:
Echo $#$*
Shift
Example:
#cat shift
echo$#$*
Shift
echo$#$*
Shift
echo$#$*
Shift
echo$#$*
Shift
echo$#$*
Shift
echo$#$*
$ sh shift a b c d e f
Explanation:
This will take arguments from terminal by using each shift it wi
ll remove one argument and display remaining until arguments is left.
Output:
6 a b c d e f
5 b c d e f
4 c d e f
3 d e f
2 e f
1 f
Check Prime
Program ::
cat chkprime
#Chech a given number is prime or not
echo "Enter a number"
read n
i=1
count=0
while [ $i -le $n ]
do
if [ $((n%i)) -eq 0 ]
then
count=$((count+1))
fi
i=$((i+1))
done
if [ $count -eq 2 ]
then
echo "$n is a Prime number"
else
echo "$n is not a prime number"
fi
OUTPUT ::
sh chkprime
Enter a number
45
45 is not a prime number
Factorial
Program ::
cat factorial
#Print Factorial of a number
echo "Enter a number to find factorial value ::"
read n
a=$n
if [ $n -gt 1 ]
then
fact=1
while [ $n -gt 0 ]
do
fact=$((fact*n))
n=$((n-1))
done
echo "Factorial of $a = $fact"
fi
OUTPUT ::
sh factorial
Enter a number to find factorial value ::
6
Factorial of 6 = 720
FEBONICI SERIES
AIM :: Program to generate febonici series
Program ::
cat febonici
#Generate febonici series
echo "Enter the higher bound of the series ::"
read n
if [ $n -gt 1 ]
then
temp=0
temp1=1
i=0
echo "The series is :: "
while [ $i -lt $n ]
do
f=$((f+temp))
echo $f
temp=$temp1
temp1=$f
i=$((i+1))
done
fi
OUTPUT::
sh febonici
Enter the higher bound of the series ::
10
The series is ::
0
1
1
2
3
5
8
13
21
34
Greatest Among Three Numbers
Program ::
cat >greatest
#Program to find greatest among three numbers
echo "Enter Three numbers :: "
read a
read b
read c
if [ $a -gt $b -a $a -gt $c ]
then
echo "$a is the greatest among three"
elif [ $b -gt $c ]
then
echo "$b is greatest among three"
else
ech "$c is the greatest among three"
fi
OUTPUT ::
sh greatest
Enter Three numbers ::
45
67
89
89 is the greatest among three
Implement MOVE
Program ::
if [ "$#" -ne 2 ]
then
echo "usaes : copy file1 file2"
exit 1
fi
from="$1"
to="$2"
if [ -d "$to" ]
then
to="$to/$(basename $from)"
fi
if [ -e "$to" ]
then
echo "$to already exists;overwrite(y/n)? \c"
read answer
if [ "$answer"!=y ]
then
echo "copy not performed"
exit 0
fi
fi
cp $from $to
rm "$from"
echo “Moving Successfull”
OUTPUT ::
sh copy file1 file2
Move Successfull
Phonebook
AIM :: Program to Add,Remove and Search datas From Phonebook
Program ::
#Program to manipulate Phonebook
echo "
1. Add to Phonebook
2. Remove from Phonebook
3. Search from Phonebook
Enter Your choice :: "
read ch
case "$ch" in
1)echo "Enter the name ::"
read name
echo "Enter the Ph No :: "
read num
echo "$name $num" >>Phonebook;;
2)echo "Enter the name to Delete :: "
echo name
grep -v "$name" Phonebook >/tmp/Ph
mv /tmp/Ph Phonebook
echo "Deleted";;
3)echo "Enter the name to search"
read name
grep "$name" Phonebook;;
*)echo "Invalid Choice"
exit 1;;
esac
OUTPUT ::
sh phmp
Add to Phonebook
2. Remove from Phonebook
3. Search from Phonebook
Enter Your choice ::
1
Enter the name ::
Raj
Enter the Ph No ::
9042508265
INSTALLING THE LINUX OPERATING SYSTEM
AIM :: Installing The Linux Operating System
To start the installation process,boot off CD/DVD-ROM .This present us with a sp
lash screen introducing us to Linux RedHat.
Press any Key before timeout to proceed ………
After getting the Welcome Screen In PC, Follow the following Steps….
Steps ::
1. Select the language from the list in which language you want to perform
installation ……
• English(US)
Click On Next
2. Select the Keyboard Language (Layout) …..
• U.S. English Layout
Click On Next
3. Mouse Configuration….
2 button mouse
3 button mouse PS/2
………………………………
• 3 button mouse PS/2
Click On Next
Lines 1-8 All lines that begin with the pound sign(#) are comments and are ign
ored.
Line 9 ,Default This directive tells GRUB which entry to automatically boot. T
he numbering starts from Zero.
Line 10, timeout This means that GRUB will automatically boot the default entr
y after five seconds. This can be interrupted by pressing any key in keyboard.
Line 11, splashimage This line specifies the name and location of an image fi
le to be displayed at the boot menu.
Line 12, hiddenmenu This entry hides the usual GRUB menu. It is an optional en
try.
Line 13, title This is used to display a short title or description for the fo
llowing entry it defines. The title field marks the beginning of a new boot entr
y in GRUB.
Line 14, root It is used to specify where the root partition mounted
Line 15, Kernel Used to specify path to a kernel image.
Line 16, Initrd This option allows us to load kernel modules from an image, no
t the modules from /lib/modules.
5. Finally install the GRUB boot loader directly on the MBR of the hard dis
k. Type
grub> setup (hd0)
checking if “/boot/grub/stage1” exists. . . no
checking if “/grub/stage1” exists. . . yes
checking if “/grub/stage1” exists. . . yes
checking if “/grub/e2fs_stage1_5” exists. . . yes
Running “embed /grub/e2fs_stage1_5 (hd0,0)”. . . 16 sectors are embedded.
succeeded
Running “install /grub/stage1 (hd0) (hd0) 1+16 p (hd0,0) /grub/stage2/grub/g
rub.conf . . . succeeded
Done
6. Quit the GRUB shell. Type
grub> quit
PARTITION WITH FDISK
AIM :: Make partition with fdisk command
STEPS ::::::
1. Begin by running fdisk with the -l parameter to list the current par
tition table. Type
# fdisk -l
Disk /dev/hda : 10.7 GB, 10737418240 bytes
. . . (OUTPUT TRUNCATED) . . .
/dev/hda2 26 1200 9438187+ 8e Linux LVM
2. Next, we begin the actual repartitioning process using fdisk again. Typ
e
# fdisk /dev/hda
The number of cylinders for this disk is set to 1305
. . . (OUTPUT TRUNCATED) . . .
Booting and partitioning s/w from other Oss
( e.g. DOS FDISK, OS/2 FDISK )
Command ( m for help ) :
You will be presented with a sample fdisk prompt “Command(m for help):”
3. Print the partition table again while inside the fdisk program. Type p
at the fdisk prompt to print the partition table.
Command(m for help): p
Disk /dev/had : 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = Cylinders of 16065 * 512 = 8225280 bytes
Device boot start End Blocks Id
System
/dev/hda1 * 1 25 200781 83
Linux
/dev/hda2 26 1200 9438187+ 8e
Linux LVM
11. Enter the partition type for “Linux LVM“. Type 8e at the prompt
Hex code ( type L to list codes ) : 8e
12. View the changes made by you…….
Command (m for help) : p
Disk /dev/had : 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = Cylinders of 16065 * 512 = 8225280 bytes
Device boot start End Blocks Id
System
/dev/hda1 * 1 25 200781 8
3 Linux
/dev/hda2 26 1200 9438187+ 8e
Linux LVM
/dev/hda3 1201 1305 843412+ 8e
Linux LVM
13. Commit or write the changes to the partition table type
Command (m for help) : w
14. Quit the fdisk utility type
Command (m for help) : q
15. Now reboot the system by typing
# reboot