You are on page 1of 35

Ex.No:1 BASIC UNIX COMMANDS 1.

CAT COMMAND To create a new files: administrator@ubuntu:~$ cat>file1 assalam college of engineering and technology thirumanagalakudi aduthuraia dministrator@ubuntu:~$ cat>file2 departments available CSE ECE CIVIL To display the contents of the file: administrator@ubuntu:~$ cat file1 assalam college of engineering and technology thirumanagalakudi aduthurai administrator@ubuntu:~$ cat file2 departments available CSE ECE CIVIL To concatenate the contents of the file: administrator@ubuntu:~$ cat file1 file2>file3 administrator@ubuntu:~$ cat file3 assalam college of engineering and technology thirumanagalakudi aduthuraidepartments available CSE ECE CIVIL

2.DATE COMMAND To Display Date: administrator@ubuntu:~$ date Tue Apr 24 11:46:05 IST 2012 administrator@ubuntu:~$ date +%h Apr administrator@ubuntu:~$ date +%m 04 administrator@ubuntu:~$ date +%H 11 administrator@ubuntu:~$ date +%M 47 administrator@ubuntu:~$ date +%S 24 administrator@ubuntu:~$ date +%Y 2012 administrator@ubuntu:~$ date +%D 04/24/12 3.CALENDAR COMMAND To display the calendar: administrator@ubuntu:~$ cal April 2012 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 administrator@ubuntu:~$ cal may 2010 May 2010 Su Mo Tu We Th Fr Sa 12 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

administrator@ubuntu:~$ cal 01 2000 January 2000 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 4.WHO AND WHO AM I COMMAND To display all users: administrator@ubuntu:~$ who administrator pts/0 2012-04-24 10:25 (:0) administrator tty7 2012-04-24 10:25 (:0) To display the current user: administrator@ubuntu:~$ who am i administrator pts/0 2012-04-24 10:25 (:0) 5.MAN COMMAND To display the manual: administrator@ubuntu:~$ man echo ECHO(1) User Commands ECHO(1) NAME echo - display a line of text SYNOPSIS echo [SHORT-OPTION]... [STRING]... DESCRIPTION -n do not output the trailing newline 6.WORD COUNT COMMAND To display the word count in a file: administrator@ubuntu:~$ wc file1 2 8 73 file1

7.COPY COMMAND To copy a file aduthuraiadministrator@ubuntu:~$ cp file1 copyfile administrator@ubuntu:~$ cat file1 assalam college if engineering and technology thirumanagalakudi aduthurai administrator@ubuntu:~$ cat copyfile assalam college if engineering and technology thirumanagalakudi aduthurai 8. MOVE COMMAND To move a file into another: aduthuraiadministrator@ubuntu:~$ mv file1 newfile administrator@ubuntu:~$ cat file1 cat: file1: No such file or directory administrator@ubuntu:~$ cat newfile assalam college if engineering and technology thirumanagalakudi aduthuraia 9.COMPARE COMMAND To compare two files: administrator@ubuntu:~$ cmp file2 newfile file2 newfile differ: byte 1, line 1 10. ECHO COMMAND To print the given statement: administrator@ubuntu:~$ echo UNIXLAB UNIXLAB

11.READ COMMAND To get the input statement: administrator@ubuntu:~$ read unix lab 12.LIST COMMAND To list all the files: administrator@ubuntu:~$ ls 1.c bhaj.sh g.c 2.c bs.sh gowtham.sh 3.c bst.sh kk.c 4.c bz.sh krishna.sh 13. REMOVE COMMAND To remove a file: administrator@ubuntu:~$ rm system administrator@ubuntu:~$ cat system cat: system: No such file or directory 14.PWD COMMAND To display current working directory: administrator@ubuntu:~$ pwd /home/administrator 15.SORT COMMAND To sort the contents in a file administrator@ubuntu:~$ sort system cpu monitor motherboard mouse

nesar one.sh padd.c pag.c

shahul sivam.sh sivaprakash.sl siva.sl

16.TTY COMMAND To display the current terminal: administrator@ubuntu:~$ tty /dev/pts/0 17. BC command To open the calculator administrator@ubuntu:~$ bc 5+3 8 2*3 6 18.BANNER COMMAND To highlight the message.

administrator@ubuntu:~$ banner unix # # # # # # # # # ## # # # # # # ## # # ## # # # ## # ## # # # ## # # # #### # # # # #

Ex.No:2 WORKING WITH FILES 1.COPY COMMAND To create a new file administrator@ubuntu:~$ cat>file1 assalam college of engineering and technology thirumanagalakudi aduthuraia To copy a file aduthuraiadministrator@ubuntu:~$ cp file1 copyfile To display a file administrator@ubuntu:~$ cat file1 assalam college of engineering and technology thirumanagalakudi aduthurai administrator@ubuntu:~$ cat copyfile assalam college of engineering and technology thirumanagalakudi aduthurai 2. REMOVE COMMAND To remove a file: administrator@ubuntu:~$ rm system administrator@ubuntu:~$ cat system cat: system: No such file or directory 3. MOVE COMMAND To move a file into another: aduthuraiadministrator@ubuntu:~$ mv file1 newfile administrator@ubuntu:~$ cat file1 cat: file1: No such file or directory administrator@ubuntu:~$ cat newfile assalam college of engineering and technology thirumanagalakudi

3. SORT COMMAND To sort the contents in a file administrator@ubuntu:~$ sort system cpu keyboard monitor pendrive administrator@ubuntu:~$ sort -r system pendrive monitor keyboard cpu administrator@ubuntu:~$ sort -s system cpu keyboard monitor mouse pendrive 4.WC COMMAND To display the word count in a file: administrator@ubuntu:~$ wc file1 3 8 73 file1 administrator@ubuntu:~$ wc -l file1 3 file1 administrator@ubuntu:~$ wc -c file1 73 file1 administrator@ubuntu:~$ wc -w file1 8 file1

5.Directing output to the file administrator@ubuntu:~$ who>file administrator@ubuntu:~$ cat file administrator pts/0 2012-04-24 14:45 (:0)

Ex.No:3

WORKING WITH DIRECTORIES 1. MKDIR COMMAND To create a directory administrator@ubuntu:~$ mkdir UNIXLAB 2.CD COMMAND To change the working directory administrator@ubuntu:~$ cd UNIXLAB administrator@ubuntu:~/UNIXLAB$ cat>file Pipe Filters VI editor Shell program administrator@ubuntu:~/UNIXLAB$ cat file Pipe Filters VI editor Shell program 3.PWD COMMAND To print the current working directory administrator@ubuntu:~/UNIXLAB$ pwd /home/administrator/UNIXLAB

4. RMDIR COMMAND To remove a directory administrator@ubuntu:~/UNIXLAB$ cd\ administrator@ubuntu:~$ rmdir UNIXLAB administrator@ubuntu:~$ pwd /home/administrator 5.TO SPECIFY THE PATH OF OS administrator@ubuntu:~$ echo $HOME /home/administrator 6.MOVING FILES WITHIN DIRECTORIES administrator@ubuntu:~$ cat lab1 unix foc physics chemistry administrator@ubuntu:~$ mkdir dir administrator@ubuntu:~$ mv lab1 dir administrator@ubuntu:~$ cat lab1 cat: lab1: No such file or directory

Ex.No:4

UNIX EDITOR administrator@ubuntu:~$ vi editor A file has been created and opened editor This is a file created for working with unix vi editor character has been inserted using insert mode i we can move through the contents in a escape mode saving and quitting using ":wq". Open the shell program with the extension sh. Ex. vi filename.sh Run the shell program by sh filename.sh Open the C program with the extension c. Ex vi filename.c compile the c program by cc filename.c Run the C program by ./a.out ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ "editor" 6 lines, 205 characters

Ex.No:5

PIPES administrator@ubuntu:~$ who administrator pts/0 2012-04-24 15:11 (:0) administrator@ubuntu:~$ who|wc -l 1 administrator@ubuntu:~$ ls|sort>file administrator@ubuntu:~$ cat file aa.sh az.c b.c cal.y~ chokka dir1 ex.c fib gk.c lex.yy.c madha munna.sh qwerty.c segment.c unix z.c ZZZ.sh administrator@ubuntu:~$ ls|sort|wc -l 142 administrator@ubuntu:~$ who|tee sample|wc -l 1 administrator@ubuntu:~$ cat sample administrator pts/0 2012-04-24 15:11 (:0)

Ex.No :6 SIMPLE FILTERS

FILE CREATION administrator@ubuntu:~$ cat>file name total rank aaaa 492 1 bbbb 480 2 cccc 473 3 dddd 450 4 eeee 425 5

administrator@ubuntu:~$ cat>file1 As-salam college of engineering & technology Thirumangalakudi Aduthurai FILE DISPLAY administrator@ubuntu:~$ cat file name total rank aaaa 492 1 bbbb 480 2 cccc 473 3 dddd 450 4 eeee 425 5

administrator@ubuntu:~$ cat file1 As-salam college of engineering & technology Thirumangalakudi Aduthurai

1.HEAD FILTER administrator@ubuntu:~$ head -3 file name total rank aaaa 492 1 bbbb 480 2 2.TAIL FILTER administrator@ubuntu:~$ tail -3 file cccc 473 3 dddd 450 4 eeee 425 5

3.PAGE FILTER administrator@ubuntu:~$ pg file name total rank aaaa 492 1 bbbb 480 2 cccc 473 3 dddd 450 4 eeee 425 5 (EOF):

4.MORE FILTER administrator@ubuntu:~$ more file name total rank aaaa 492 1 bbbb 480 2 cccc 473 3 dddd 450 4 eeee 425 5 --More--(45%)

5.NL FILTER administrator@ubuntu:~$ nl file 1 name total rank 2 aaaa 492 1 3 bbbb 480 2 4 cccc 473 3 5 dddd 450 4 6 eeee 425 5 6.PASTE FILTER administrator@ubuntu:~$ paste file file1 name aaaa bbbb cccc dddd eeee total 492 480 473 450 425 rank 1 2 3 4 5 As-salam college of engineering & technology Thirumangalakudi Aduthurai

7.COMPARE FILTER administrator@ubuntu:~$ cmp file file1 file file1 differ: byte 1, line 1

8.CUT FILTER administrator@ubuntu:~$ cut -f1 file name aaaa bbbb cccc dddd eeee

Ex.No :7a(i)

SHELL VARIABLES PAYROLL CREATION PROGRAM echo PAYROLL echo Enter employee name read ename echo Enter employee id read eno echo Enter basic pay read bp echo Enter DA% read dap echo Enter HRA% read hrap pf=$(($bp*(125/10)/100)) da=$(($bp*$dap/100)) hra=$(($bp*$hrap/100)) echo Provident Fund=$pf echo Dearness Allowance=$da echo Houserent Allowance=$hra np=$(($bp-($pf+$da+$hra))) echo Net Pay=$np

OUTPUT administrator@ubuntu:~$ vi payroll.sh administrator@ubuntu:~$ sh payroll.sh PAYROLL Enter employee name surya Enter employee id 123001 Enter basic pay 20000 Enter DA% 20 Enter HRA% 15 Provident Fund=2400 Dearness Allowance=4000 Houserent Allowance=3000 Net Pay=10600

Ex.No:7a(ii)

TEMPERATURE CONVERSION

PROGRAM echo "1.Farenheit to Celcius 2.Celcius to Farenheit" echo "Enter choice" read c if [ $c -eq 1 ] then echo "Enter temperature in Farenheit" read tf tc=$(echo "scale=2;(5/9)*($tf-32)" | bc) echo "$tf F=$tc C" elif [ $c -eq 2 ] then echo "Enter temperature in celcius" read tc tf=$(echo "scale=2;(9/5)*($tc+32) " | bc) echo "$tc C= $tf f" else echo "Enter correct option " fi

OUTPUT administrator@ubuntu:~$ vi temp.sh administrator@ubuntu:~$ sh temp.sh 1.Farenheit to Celcius 2.Celcius to Farenheit Enter choice 1 Enter temperature in Farenheit 100 100 F=37.40 C administrator@ubuntu:~$ sh temp.sh 1.Farenheit to Celcius 2.Celcius to Farenheit Enter choice 2 Enter temperature in celcius 37 37 C= 124.20 F

Ex.No :7(b)

CONDITIONAL STATEMENTS BIGGEST OF THREE NUMBERS

PROGRAM echo "enter 3 numbers" read a b c if [ $a -gt $b -a $a -gt $c ] then echo "$a is greater" elif [ $b -gt $c ] then echo "$b is greater" else echo "$c is greater" fi

OUTPUT administrator@ubuntu:~$ vi big.sh administrator@ubuntu:~$ sh big.sh enter 3 numbers 234 324 1090 1090 is greater

Ex.No :7c(i)

TESTING AND LOOPING TESTING WHETHER THE NUMBER IS ODD OR EVEN NUMBER

PROGRAM echo "enter a number" read n r=$(($n%2)) if [ $r -eq 0 ] then echo "given number $n is even" else echo "given number $n is odd" fi

OUTPUT administrator@ubuntu:~$ vi odd.sh administrator@ubuntu:~$ sh odd.sh enter a number 242 given number 242 is even administrator@ubuntu:~$ sh odd.sh enter a number 973 given number 973 is odd

Ex.No :7c(ii)

PRINTING NATURAL NUMBERS UPTO 'N' USING LOOP

PROGRAM echo "enter a value for n" read n i=1 while [ $i -le $n ] do echo "The number is $i" i=$(($i+1)) done

OUTPUT administrator@ubuntu:~$ vi natural.sh administrator@ubuntu:~$ sh natural.sh enter a value for n 5 the number is 1 the number is 2 the number is 3 the number is 4 the number is 5

Ex.No :7d(i)

REVERSING A GIVEN NUMBER.

PROGRAM echo "enter a number" read num rev=0 while [ $num -ne 0 ] do d=$(($num %10)) num=$((num/10)) rev=$(($rev*10+$d)) done echo "the reverse digit is $rev" echo "enter the same number " read num if [ $num -eq $rev ] then echo "the given number is a palindrome" else echo "the given number is not a palidrome"

OUTPUT administrator@ubuntu:~$ vi reverse.sh administrator@ubuntu:~$ sh reverse.sh enter a number 123 the reverse digit is 321 enter the same number 123 the given number is not a palidrome administrator@ubuntu:~$ sh reverse.sh enter a number 454 the reverse digit is 454 enter the same number 454 the given number is a palindrome

Ex.No :7d(ii) FIBONACCI SERIES

PROGRAM echo Enter the range read n a=0 b=1 echo Fibonacci series echo $a echo $b i=2 while [ $i -le $n ] do c=$(($a+$b)) echo $c i=$(($i+1)) a=$b b=$c done

OUTPUT administrator@ubuntu:~$ vi fibonacci.sh administrator@ubuntu:~$ sh fibonacci.sh Enter the range 7 Fibonacci series 0 1 1 2 3 5 8 13

Ex.No :7e

GREP AND SED COMMANDS

GREP-Global Regular Expression and Print TO CREATE A FILE administrator@ubuntu:~$ cat>file2 UNIX is a multitasking, multi-user computer operating system. The UNIX system was originally developed in 1969. It was developed by a AT&T employees at Bell Labs including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas Mcllroy, and Joe Ossanna. UNIX means Uniplexed Information Computing System(UNICS). UNIX operating system is written in C language. A directory is a named group of related files.

1.TO PRINT THE LINES THAT MATCH WITH GIVEN PATTERN administrator@ubuntu:~$ grep The file2 The UNIX system was originally developed in 1969. administrator@ubuntu:~$ grep was file2 The UNIX system was originally developed in 1969. It was developed by a AT&T employees at Bell Labs including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas Mcllroy, and Joe Ossanna.

2. TO PRINT MATCHING LINES WITH LINE NUMBER administrator@ubuntu:~$ grep -n is file2 1:UNIX is a multitasking, multi-user computer operating system. 3:It was developed by a AT&T employees at Bell Labs including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas Mcllroy, and Joe Ossanna. 5:UNIX operating system is written in C language. 6:A directory is a named group of related files.

3. TO PRINT NON-MATCHING LINES administrator@ubuntu:~$ grep -v UNIX file2 It was developed by a AT&T employees at Bell Labs including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas Mcllroy, and Joe Ossanna. A directory is a named group of related files. SED-STREAM EDITOR 1.TO REPLACE A WORD administrator@ubuntu:~$ sed 's/UNIX/UNICS/' file2 UNICS is a multitasking, multi-user computer operating system. The UNICS system was originally developed in 1969. It was developed by a AT&T employees at Bell Labs including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas Mcllroy, and Joe Ossanna. UNICS means Uniplexed Information Computing System(UNICS). UNICS operating system is written in C language. A directory is a named group of related files.

2.TO REPLACE A CHARACTER administrator@ubuntu:~$ sed 's/a/A/' file2 UNIX is A multitasking, multi-user computer operating system. The UNIX system wAs originally developed in 1969. It wAs developed by a AT&T employees at Bell Labs including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas Mcllroy, and Joe Ossanna. UNIX meAns Uniplexed Information Computing System(UNICS). UNIX operAting system is written in C language. A directory is A named group of related files.

Ex.No :7f

AWK FILTER

TO CREATE A FILE administrator@ubuntu:~$ cat>file banana yellow 50 apple red 45 papaya yellow 25 graphes black 90

1.TO DISPLAY THE COLUMN administrator@ubuntu:~$ awk '/yellow/{print$1}' file banana papaya administrator@ubuntu:~$ awk '/yellow/{print$3}' file 50 25 2.TO DISPLAY UPTO THE PARTICULAR CHARACTER TO DISPLAY UPTO THE CHARACTER 'A' administrator@ubuntu:~$ awk -F a '{print$1}' file b p gr administrator@ubuntu:~$ awk -F a '{print$2}' file n pple red 45 p phes bl

administrator@ubuntu:~$ awk -F a '{print$3}' file n y ck

90

TO DISPLAY UPTO THE CHARACTER 'P' administrator@ubuntu:~$ awk -F p '{print$1}' file banana yellow 50 a gra

3.TO INSERT A WORD administrator@ubuntu:~$ awk -F a '{print"unix:"$1}' file unix:b unix: unix:p unix:gr administrator@ubuntu:~$ awk -F p '{print"unix:"$2}' file unix: unix: unix:a unix:hes black 90 administrator@ubuntu:~$ awk -F e '{print"unix:"$3}' file unix: unix:d 45 unix: unix:

Ex.No :8

SIMPLE C PROGRAM USING FORK() PROGRAM #include<stdio.h> #include<unistd.h> int main() { int pid=fork(); int pi; if(pid==-1) { perror("during fork()\n"); exit(0); } if(pid==0) { printf("Child Process\n"); printf("Process ID in child=%d\n",getpid()); printf("Process ID of parent in child=%d\n",getpid()); exit(0); } else { printf("Parent Process\n"); printf("Process ID in parent=%d\n",getpid()); printf("Process ID of child in parent=%d\n",getpid()); exit(0); } }

OUTPUT administrator@ubuntu:~$ vi fork.c administrator@ubuntu:~$ cc fork.c administrator@ubuntu:~$ ./a.out Parent Process Process ID in parent=1760 Process ID of child in parent=1760 Child Process Process ID in child=1761 Process ID of parent in child=1761

Ex.No :9

SIMPLE C PROGRAM USING SIGNAL

PROGRAM #include<stdio.h> #include<signal.h> void sigproc(void); void quitproc(void); main() { signal(SIGINT,sigproc); signal(SIGQUIT,quitproc); printf("ctrl-c disabled use ctrl-\ to quit\n"); for(;;); } void sigproc() { signal(SIGINT,sigproc); printf("you have pressed ctrl-c \n"); } void quitproc() { printf("ctrl-\ pressed to quit \n"); exit(0); }

OUTPUT administrator@ubuntu:~$ vi signal.c administrator@ubuntu:~$ cc signal.c administrator@ubuntu:~$ ./a.out ctrl-c disabled use ctrl- to quit ^Cyou have pressed ctrl-c ^\ctrl- \ pressed to quit

Ex.No:10 SIMPLE C PROGRAM USING FILE HANDLING PROGRAM #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> main() { int fd; char fname[20],buf1[100],buf2[]="welcome to ascet"; printf("\n enter input file name:"); scanf("%s",fname); if((fd=creat(fname,S_IRWXU))<0) { printf("\n error in file creation\n"); exit(0); } else { write(fd,buf2,20); printf("\n content is written\n"); close(fd); fd=open(fname,O_RDONLY); read(fd,buf1,20); printf("\n the file content is :\n %s",buf1); printf("\n"); close(fd); } }

OUTPUT administrator@ubuntu:~$ vi file.c administrator@ubuntu:~$ cc file.c administrator@ubuntu:~$ ./a.out enter input file name:file.out content is written the file content is : welcome to ascet

You might also like