You are on page 1of 9

EX: 1 BASIC OF UNIX COMMANDS

AIM
Write the basics of UNIX commands for file processing and working with
Directories.

WORKING WITH FILES


CREATING A FILE
DESCRIPTION
This command is used to create a file.
SYNTAX
$cat>filename
EXAMPLE
$cat>Demo.dat

COPY COMMAND
DESCRIPTION
This command is used to copy a file from one to another file.
SYNTAX
$cp <oldfilename> <newfilename>
EXAMPLE
$cp aaa bbb

1
REMOVE COMMAND
DESCRIPTION
This command is used to remove a file.
SYNTAX
$rm <filename>
EXAMPLE
$rm abc

MOVE COMMAND
DESCRIPTION
This command is used to move the file from one directory to another directory.
SYNTAX
$mv <filename> <filename/directoryname>
EXAMPLE
$mv aaa bbb

VIEW COMMAND
DESCRIPTION
This command is used to view the contents of the file.
SYNTAX
$cat <filename>
EXAMPLE
$cat xyz

ADD COMMAND
DESCRIPTION
This command is used to add the contents to the existing file.
SYNTAX
$cat>>filename

2
EXAMPLE
$cat>>aaa

WORKING WITH DIRECTORIES

CREATE A DIRECTORY
DESCRIPTION
This command is used to create a new directory.
SYNTAX
$mkdir directoryname
EXAMPLE
$mkdir book

CHANGE COMMAND
DESCRIPTION
This command is used to change the new directory.
SYNTAX
$cd directoryname
EXAMPLE
$cd flowers

REMOVE DIRECTORY COMMAND


DESCRIPTION
This command is used to remove a directory.
SYNTAX
$rm directoryname
EXAMPLE
$rm fruits

PATH COMMAND
DESCRIPTION

3
This command is used to display the path of the current file.
SYNTAX
$pwd

DATE COMMAND
DESCRIPTION
This command is used to display the date in month, year, day, hours, minutes and
seconds.
SYNTAX
$date

WHO COMMAND
DESCRIPTION
This command is used to see who are the users connected to the server.
SYNTAX
$who
CALENDER COMMAND
DESCRIPTION
This command is used to display the calendar with month and year.
SYNTAX
$cal month year
EXAMPLE
$cal 01 2011
WHO AM I COMMAND
DESCRIPTION
This command is used to know the system number.
SYNTAX
$who am i

CLEAR COMMAND
DESCRIPTION

4
This command is used to clear the screen.
SYNTAX
$clear
FILE COMMAND
DESCRIPTION
This command is used to display the contents of more than one file.
SYNTAX
$more <file1> <file2>
EXAMPLE
$more aaa bbb
SORT COMMAND
DESCRIPTION
This command is used to sort the contents of file in a predefined order.
SYNTAX
$sort filename
EXAMPLE
$sort name
ATTRIBUTES
A -> Display the numerical value order.
R -> Display the reverse order.
M -> The case distinction is ignored.

GREP COMMAND
DESCRIPTION
This command is used for certain kind of pattern matching.
SYNTAX
$grep string filename
EXAMPLE
$grep earth planet

ECHO COMMAND

5
DESCRIPTION
This command is used to display the text.
SYNTAX
$echo text/variable
EXAMPLE
$echo hello

WC COMMAND
DESCRIPTION
This command is used to display the number of lines, words and character.
SYNTAX
wc l filename
wc w filename
wc c filename

LIST COMMAND
DESCRIPTION
This command is used to list the current file in the directory.
SYNTAX
$ls filename
EXAMPLE
$ls aaa
ATTRIBUTES
$ls l -> List of files in long format.
$ls u -> Display the files in the order of last access time.
$ls a -> It displays all the files and directories including hidden files.
$ls p -> Put slash after the directories.
$ls t -> Displays the file in order of modified time.

HEAD COMMAND
DESCRIPTION

6
This command is used to display the specified line in the specified file.
SYNTAX
$head ~count <filename>
TAIL COMMAND
DESCRIPTION
This command is used to retrieve specified lines from given file in order we choose.
SYNTAX
$tail [+/- count] <filename>
EXAMPLE
$tail -3 book
WILD CHAR COMMAND/PATTERN
DESCRIPTION
It is used to display the file starting with specified character.
SYNTAX
$ls [alphabet]*

MAN COMMAND
DESCRIPTION
It is used to help for a command, it can be abstracted manually.
SYNTAX
$man[command]
EXAMPLE
$man cat

DISPLAY FILES IN A PATTERN


DESCRIPTION
It is used to display the file starting with a to m.
SYNTAX
$ls[a-m]*

7
RESULT
Thus the basics of UNIX commands are studied and executed successfully.

EX: 2 SHELL PROGRAMMING


THEORY

A Linux shell is a command language interpreter, the primary purpose of which is to


translate the command lines typed at the terminal into system actions. The shell itself is a
program through which other programs are invoked

Shell script:
- A shell script is a file containing a list of commands to be executed by the Linux
shell. Shell script provides the ability to create your own customized Linux
commands
- Linux shell have sophisticated programming capabilities which makes shell script
powerful Linux tools

EX :2a SHELL PROGRAM TO SIMULATE THE FORK( ) AND GETPID( )


SYSTEM CALLS

AIM
Write the shell program to simulate the fork and getpid() system calls.

ALGORITHM
Step 1: Initialise the max count value and buffer size.
Step 2: Get the fork() system calls to execute the process. It makes two identical copies of
address the space one for the parent and other for the child.
Step 3: Both process start their execution right after the system call fork()
a) If both the processes have identical but separate address space.
b) The fork() call have the same values in both address space, since every process
has its own address space, any modification will be independent of the others.

8
Step 4: Get the process id, by using getpid() of system call.
Step 5: The printf() is buffer meaning printf() will group the output of a process together it
print the process id and value.

PROGRAM
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#define MAX_COUNT 5
#define BUF_SIZE 100
main(void)
{
pid_t pid;
int i;
char buf[BUF_SIZE];
fork();
pid=getpid();
for(i=1;i<MAX_COUNT;i++)
{
sprintf(buf,"this line is from pid %d value=%d \n",pid,i);
write(1,buf,strlen(buf));
}
}

OUTPUT

RESULT
Thus the shell program to simulate fork() and getpid() system call is executed and the
output is verified.