Friday, November 23, 2012


UNIX COMMANDS
1)      To Display the Date:

[mca0610015@linux ~]$ date
Tue Jan  8 15:20:42 IST 2008

2)      To Display the Time:

[mca0610015@linux ~]$ time

real    0m0.000s
user    0m0.001s
sys     0m0.000s
[mca0610015@linux ~]$

3)To Display the calendar:

[mca0610015@linux ~]$ cal
    January 2008
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) To clear the screen:
[mca0610015@linux ~]$clear

5) To Create a new directory:

[mca0610015@linux sample]$ mkdir first
[mca0610015@linux sample]$

6)      To check how many users activated currently:

[mca0610015@linux nana]$ who
mca06100 pts/1        Jan  8 15:21 (192.169.3.20)
mca06100 pts/2        Jan  8 15:17 (192.169.3.10)
mca06100 pts/3        Jan  8 15:27 (192.169.3.33)
mca06100 pts/4        Jan  8 15:18 (192.169.3.36)
mca06100 pts/5        Jan  8 15:18 (192.169.3.18)
mca06100 pts/6        Jan  8 15:19 (192.169.3.43)



7)      To Display the current Login:

[mca0610015@linux nana]$ who am i
mca06100 pts/24       Jan  8 15:24 (192.169.3.44)
[mca0610015@linux nana]$

8)      To Change the Directory:

[mca0610015@linux ~]$ cd sample
[mca0610015@linux sample]$


9)      To Exit to previous directory:

[mca0610015@linux sample]$ cd ..
[mca0610015@linux ~]$

10)  To Create a new File:

[mca0610015@linux ~]$ cat > vel
hi i am velmurugan
[mca0610015@linux ~]$

11)  Display the content of File:

[mca0610015@linux ~]$ cat vel
hi i am velmurugan
[mca0610015@linux ~]$

12)  To Display the Present working directory:

 [mca0610015@linux sample]$ pwd
/home/mca0610015/sample
[mca0610015@linux sample]$


13)  To Display the Long form of information of files:


[mca0610015@linux ~]$ ls -l
total 32
drwxrwxr-x  2 mca0610015 mca0610015 4096 Jan  8 15:26 kgln
drwxrwxr-x  2 mca0610015 mca0610015 4096 Jan  8 15:29 nana
drwxrwxr-x  3 mca0610015 mca0610015 4096 Jan  8 15:40 sample
-rw-rw-r--  1 mca0610015 mca0610015   19 Jan  8 15:47 vel
[mca0610015@linux ~]$



14)  To Display the Hidden files

 [mca0610015@linux ~]$ ls -a
.   .bash_history  .bash_profile  .emacs  .kde  nana    vel      .zshrc
..  .bash_logout   .bashrc        .gtkrc  kgln  sample  .xemacs
[mca0610015@linux ~]$



15)  To Copy the file in to another File

[mca0610015@linux ~]$ cp press vel
[mca0610015@linux ~]$ cat vel
hi i am vel
[mca0610015@linux ~]$

16)  To Display the Process State

[mca0610015@linux ~]$ ps
  PID TTY          TIME CMD
 9307 pts/24   00:00:00 bash
12257 pts/24   00:00:00 ps
[mca0610015@linux ~]$

 17)  To Remove the File:

 Before Remove:

 [mca0610015@linux ~]$ ls -l
total 40
drwxrwxr-x  2 mca0610015 mca0610015 4096 Jan  8 15:26 kgln
drwxrwxr-x  2 mca0610015 mca0610015 4096 Jan  8 15:29 nana
-rw-rw-r--  1 mca0610015 mca0610015   12 Jan  8 16:11 press
drwxrwxr-x  3 mca0610015 mca0610015 4096 Jan  8 16:05 sample
-rw-rw-r--  1 mca0610015 mca0610015   12 Jan  8 16:11 vel

[mca0610015@linux ~]$ rm press

After Remove:
[mca0610015@linux ~]$ ls -l
total 32
drwxrwxr-x  2 mca0610015 mca0610015 4096 Jan  8 15:26 kgln
drwxrwxr-x  2 mca0610015 mca0610015 4096 Jan  8 15:29 nana
drwxrwxr-x  3 mca0610015 mca0610015 4096 Jan  8 16:05 sample
-rw-rw-r--  1 mca0610015 mca0610015   12 Jan  8 16:11 vel
[mca0610015@linux ~]$


18)  To Remove the Directory:

Before Remove:

[mca0610015@linux ~]$ ls -l
total 32
drwxrwxr-x  2 mca0610015 mca0610015 4096 Jan  8 15:26 kgln
drwxrwxr-x  2 mca0610015 mca0610015 4096 Jan  8 15:29 nana
drwxrwxr-x  3 mca0610015 mca0610015 4096 Jan  8 16:05 sample
-rw-rw-r--  1 mca0610015 mca0610015   12 Jan  8 16:11 vel

[mca0610015@linux ~]$ rmdir nana

After Remove:

[mca0610015@linux ~]$ ls -l
total 24
drwxrwxr-x  2 mca0610015 mca0610015 4096 Jan  8 15:26 kgln
drwxrwxr-x  3 mca0610015 mca0610015 4096 Jan  8 16:05 sample
-rw-rw-r--  1 mca0610015 mca0610015   12 Jan  8 16:11 vel
[mca0610015@linux ~]$


19)  To Sort Ascending and descending order:

 [mca0610015@linux ~]$ cat > mani
vel
arun
sathish
press
manikandan
venki
bala

Ascending:

nana[mca0610015@linux ~]$ sort mani
arun
bala
manikandan
nana
press
sathish
vel
venki

Desending:

[mca0610015@linux ~]$ sort -r mani
venki
vel
sathish
press
nana
manikandan
bala
arun
[mca0610015@linux ~]$

To Execute the C program:

[mca0610015@linux ~]$ cat sam
main()
{
 printf("Welcome");
}
                                   
[mca0610015@linux ~]$ cc sam.c
sam.c:5:2: warning: no newline at end of file
[mca0610015@linux ~]$ ./a.out
Welcome[mca0610015@linux ~]$













2. program to create, open, write, Read and Copy.

#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>

int n,n1,nn,nn1,m,nn2,nn3,nn4;
main()
{
 char fname[50];
 char oldfile[50];
 char newfile[50];
 char con[100],con1[100];

 do
 {
   printf("1-Create\n");
   printf("2-Write\n");
   printf("3-Read\n");
   printf("4-Copy\n");
   printf("5-MERGE\n");
   printf("Enter your choice:\n");
   scanf("%d",&m);
   switch(m){
    case 1: printf("Create New:\n");
            printf("Enter the File Name:\n");
           scanf("%s",fname);
           n=creat(fname,00777);
           if(n<0){
               printf("ERROR File not Created\n");
            }
               else
            {
               printf("Successfully Created\n");
           }close(n);

           break;
    case 2: printf("WRITE\n");
            printf("Enter the File Name:\n");
            scanf("%s",fname);
            n = open(fname,O_WRONLY);
            if(n<0)
            {  printf("ERROR\n");
            }
              else
            {
               printf("Enter the Content:\n");
               scanf("%s",con);
               n1 = write(n,con,100);
            }
            close(n);
            break;
    case 3: printf("READ\n");
            printf("Enter the File Name:\n");
            scanf("%s",fname);
            n=open(fname,O_RDONLY);
            n1 = read(n,con,100);
            if(n1<0)
            {  printf("ERROR\n");
            }
               else
            {
               printf("%s",con);

            }
             close(n);
            break;
    case 4: printf("\nCOPY");
            printf("Enter the New File Name:\n");
            scanf("%s",fname);
            n = creat(fname,00777);
            if(n<0)
            {
               printf("Error File Not Created\n");
            }
            else
            {
                printf("Successfully Created");
            }
            close(n);
            printf("Enter the Old File Name\n");
            scanf("%s",oldfile);
            n = open(oldfile,O_RDONLY);
            n1 = read(n,con,100);
            if(n1<0)
            {
              printf("ERROR");
            }
            else
            {
                 printf("%s",con);
            }
            printf("Enter the New File Name\n");
            scanf("%s",newfile);
            nn = open(newfile,O_WRONLY);
            if(nn<0)
            {
              printf("ERROR");
              }
            else
            {
               nn1 = write(nn,con,100);
            }
            close(n);
            close(nn);
            break;

    case 5: printf("Merge\n");
            printf("Enter First File Name:");
            scanf("%s",fname);
            n = open(fname,O_RDONLY);
            n1 = read(n,con,100);
            if(n1<0)
            {
                printf("ERROR...");
            }
            else
            {
                printf("%s",con);
            }
            printf("Enter the Second File:\n");
            scanf("%s",oldfile);
            nn = open(oldfile,O_RDONLY);
            nn1 = read(nn,con1,100);
            if(nn1<0)
            {
                printf("ERROR");
            }
            else
            {
                printf("%s",con1);

            }
            printf("Enter the New File NAME:");
            scanf("%s",newfile);
            nn2 = creat(newfile,00777);
            if(nn2<0)
            {
              printf("ERROR");
            }
             else
            {
               nn3 = write(n,con,100);
               nn4 = write(nn1,con1,100);
            }
            close(n);
            close(n1);
            close(nn);
            close(nn1);
            close(nn2);
            close(nn3);
            close(nn4);
            break;
         }
}while(m<=5);
}
             

OUTPUT:
























3. Directory.c

#include<stdio.h>
#include<dirent.h>
#include<fcntl.h>
#include<sys/types.h>
main()
{
  char d_name[10];
  DIR *dp;
  struct dirent *dest;
  printf("Enter the Directory Name:");
  scanf("%s",d_name);
  dp=opendir(d_name);
  if(dp<0)
  {
    printf("DIRECTORY DOES NOT EXIST...");
  }
  else
  {
    while((dest=readdir(dp))!=NULL)
    {
      printf("%s",dest->d_name);
    }
  }
  closedir(dp);
}

Output:

[mca0610015@localhost ~]$ cc directory.c
[mca0610015@localhost ~]$ ./a.out
Enter the Directory Name:sample
kar...firstoneba[mca0610015@localhost ~]$













4 . File Types:

#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
int main()
{
struct stat *buff;
char fname[20];
int n;
printf("\nEnter File Name:");
scanf("%s",fname);
n=lstat(fname,buff);
if(n<0)
  printf("ERROR");
else
{
 printf("\n\nFile Type:");
 if(S_ISREG(buff->st_mode))
    printf("REGULAR FILE");
 else if(S_ISDIR(buff->st_mode))
    printf("Directory File");
 else if(S_ISCHR(buff->st_mode))
    printf("Character File");
 else if(S_ISBLK(buff->st_mode))
    printf("Block File");
 else if(S_ISFIFO(buff->st_mode))
    printf("Fifo File");
 else
    printf("No Such File");
}
printf("\n\nFile Size: %d",buff->st_size);
printf("\n\nUSER ID: %d",buff->st_uid);
printf("\n\nGroup ID:%d",buff->st_gid);
return 1;
}










OUTPUT:
[mca0610015@linux ~]$ cc fourth.c
[mca0610015@linux ~]$ ./a.out

Enter File Name:sample


File Type:Directory File

File Size: 4096

USER ID: 583

Group ID:584[mca0610015@linux ~]$






























5. SYSTEM DETAILS

#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<sys/utsname.h>
int main()
{
    struct utsname name;
    int n;
    n=uname(&name);
    if(n<0)
       printf("ERROR");
    else
    {

       printf("\n\nSYSTEM NAME: %s",name.sysname);

       printf("\n\nNODE NAME  : %s",name.nodename);

       printf("\n\nRELEASE    : %s",name.release);

       printf("\n\nVERSION    : %s",name.version);

       printf("\n\nMACHINE NAME : %s\n",name.machine);
     }
     return 1;
}

OUTPUT:
[mca0610015@linux ~]$ ./a.out


SYSTEM NAME: Linux

NODE NAME  : linux.example.com

RELEASE    : 2.6.9-5.ELsmp

VERSION    : #1 SMP Wed Jan 5 19:30:39 EST 2005

MACHINE NAME : i686
[mca0610015@linux ~]$



6. FORK:

#include<stdio.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
main()
{
  int no,n;
  char name[20];
  float m1,m2,m3,m4,m5,tot,avg;

  printf("\nSTUDENTS DETAILS\n");
  printf("\nEnter the No:");
  scanf("%d",&no);
  printf("\nEnter the Name:");
  scanf("%s",name);
  printf("Enter Five marks");
  scanf("%f%f%f%f%f",&m1,&m2,&m3,&m4,&m5);
  n = fork();
  printf("%d",n);
  if(n==0)
  {
    tot = m1+m2+m3+m4+m5;
    printf("Total Marks is: %f",tot);
    avg = tot/5;
    printf("Average is :%f",avg);

  }
}


OUTPUT:
[mca0610015@linux ~]$ ./a.out
STUDENTS DETAILS
Enter the No:10
Enter the Name:press
Enter Five marks50
50
50
50
50
0Total Marks is: 250.000000Average is :50.0000007388[mca0610015@linux ~]$



7. VFORK:

#include<stdio.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
int main()
{
 int empno,n;
 char empname[50];
 float bp,hra,da,pf,net;
 printf("\nEnter Employee No:");
 scanf("%d",&empno);
 printf("\nEnter Employee Name:");
 scanf("%s",empname);
 printf("\nEnter Basic Pay::");
 scanf("%f",&bp);
 n =vfork();
 if(n==0)
 {
   printf("Im in Child");
   exit(1);
 }
 else
 {
   hra = bp*3/100;
   da = bp*4/100;
   pf = bp*5/100;
   net = bp+hra+da-pf;
   printf("\nI'm in Parent");
   printf("\nHRA   :%f",hra);
   printf("\nDA    :%f",da);
   printf("\nPF    :%f",pf);
   printf("\n-------------*\n");
   printf("\nNET PAY : %f",net);

 }
}








OUTPUT:

[mca0610015@linux ~]$ ./a.out

Enter Employee No:10

Enter Employee Name:log

Enter Basic Pay::10000
Im in Child
I'm in Parent
HRA   :300.000000
DA    :400.000000
PF    :500.000000
-------------*

NET PAY : 10200.000000

[mca0610015@linux ~]$         



























8. pipe fork – employee details.

#include<stdio.h>
struct emp{
  char name[20];
  int id;
  float bpay,hra,da,pf,np;


}e;
int main()
{
int fd[2],pid=0,n;
char buf[100];
pipe(fd);
printf("Enter the Employee Name:");
scanf("%s",e.name);
printf("Enter the Employee id:");
scanf("%d",&e.id);
printf("Enter the Basic Pay:");
scanf("%f",&e.bpay);
pid=fork();
if(pid==0)
{

  close(fd[1]);

  n=read(fd[0],&e,50);
  printf("n read =%d",n);
  e.hra=e.bpay*.01;
  e.da =e.bpay*.02;
  e.pf =e.bpay*.03;
  e.np =e.bpay+e.hra+e.da-e.pf;



  printf("\n\t\t\tEMPLOYEE DETAILS");
  printf("\nNAME     :%s",e.name);
  printf("\nID       :%d",e.id);
  printf("\nBASIC PAY:%f",e.bpay);
  printf("\nHRA      :%f",e.hra);
  printf("\nDA       :%f",e.da);
  printf("\nPF       :%f",e.pf);
  printf("\nNET PAY  :%f",e.np);
}
else
{
close(fd[0]);
n=write(fd[1],&e,sizeof(e));
printf("n write =%d",n);
//printf("p=%d",e.name);
}
}
[mca0610044@linux ~]$ cc pipeemp.c
[mca0610044@linux ~]$ ./a.out
Enter the Employee Name:jeni
Enter the Employee id:123
Enter the Basic Pay:1000.
n write =44n read =44
                        EMPLOYEE DETAILS
NAME     :jeni
ID       :123
BASIC PAY:1000.000000
HRA      :10.000000
DA       :20.000000
PF       :30.000000
NET PAY  :1000.000000[mca0610044@linux ~]$




9. FIFO FILE:

#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#define FIF1 "one.1"
#define FIF2 "two.1"
int main()
{
   int fd,fd1,pid,fd3;
   char buf[100],buf1[100],fname[100],buf2[100];
   mkfifo(FIF1,00777);
   mkfifo(FIF2,00777);
   pid=fork();
   if(pid==0)
   {
      fd=open(FIF1,O_WRONLY);
      printf("Enter the filename");
      scanf("%s",&fname);
      write(fd,fname,strlen(fname));
      close(fd);
      fd=open(FIF2,O_RDONLY);
      read(fd,buf,100);
      printf("%s",buf);
      close(fd);
   }

   else
   {
      fd1= open(FIF1,O_RDONLY);
      read(fd1,buf1,100);
      printf("%s",buf1);
      close(fd1);
      fd3=open(buf1,O_RDONLY);

 if(fd3>0)
        read(fd3,buf2,100);
     else
      //  strcpy(buf2,"File does not exist");
{
perror("First");
exit(1);
}
     close(fd3);

     fd1=open(FIF2,O_WRONLY);
     write(fd1,buf2,100);
     close(fd1);
  }
Unlink(FIF1);
Unlink(FIF2);
}

OUTPUT:
[mca0610046@linux ~]$ cc fifofile.c
[mca0610046@linux ~]$ ./a.out
Enter the filename/home/mca0610046/check
hai welcome
/home/mca0610046/checkLinux[mca0610046@linux ~]$











10. server.c
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<sys/ipc.h>
#include<sys/msg.h>
int main()
{
  struct msg
  {
    long type;
    char text[10],text1[10];
  }m;
  int n,n1,ms,mr;

  n=msgget((key_t)46,00666|IPC_CREAT);
n1=msgget((key_t)461,00666|IPC_CREAT);
 while(1)
  {
  printf("Enter the text to write in to the queue:");
  scanf("%s",m.text);
m.type=100;
//  printf("\nqid=%d",n);
  ms=msgsnd(n,&m,10,0);
 // printf("\nms=%d",ms);
  if(ms>=0)
  {
 // printf("\nMessage is%s=",m.text);
  printf("\nmessage send successfully");
  }
  else
  {
    printf("\nmessage not send\n");
  }
msgrcv(n1,&m,10,0,0);
printf("\n Received    :%s",m.text);

 }
}


Client.c

#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<sys/ipc.h>
#include<sys/msg.h>
int main()
{
   struct msg
   {
      long type;
      char text[10];
   }m;
   int n,mr,ms,n1;
   char buf[10];

   n=msgget((key_t)46,00666|IPC_CREAT);
   n1=msgget((key_t)461,00666|IPC_CREAT);
   while(1)
   {

//   printf("\n%d",n);
    mr=msgrcv(n,&m,10,0,0);

 printf("\nReceived   :%s",m.text);

printf("Enter message:");
scanf("%s",m.text);
m.type=100;
ms=msgsnd(n1,&m,10,0);
printf("n1=%d",n1);
if(ms<0)
{
perror("send..");
exit(1);
}
}
}

0 comments:

Post a Comment