CS4458-Operating Systems Department of ADS 2023-2024
Ex No :1 BASICS OF UNIX COMMANDS
I File and Directory Related commands
1) pwd
This command prints the current working directory
[user@sys108 ~]$ pwd
/home/user
2) ls
This command displays the list of files in the current working directory.
$ls –l Lists the files in the long format
$ls –t Lists in the order of last modification time
$ls –d Lists directory instead of contents
$ls -u Lists in order of last access time
[user@sys108 ~]$ ls
[Link] first.c perl1 sample hai.c
3) cd
This command is used to change from the working directory to any other directory specified.
$cd directoryname
[user@sys108 ~]$ cd jean
[user@sys108 jean]$
4) cd ..
This command is used to come out of the current working directory.
$cd ..
[user@sys108 jean]$ cd ..
[user@sys108 ~]$
5) mkdir
This command helps us to make a directory.
$mkdir directoryname
[user@sys108 ~]$ mkdir jean
[user@sys108 ~]$ ls
[Link] first.c perl1 sample hai.c jean
6) rmdir
This command is used to remove a directory specified in the command line. It requires the
specified directory to be empty before removing it.
$rmdir directoryname
[user@sys108 ~]$ rmdir jean
[Link] first.c perl1 sample hai.c
7) cat
This command helps us to list the contents of a file we specify.
$cat [option][file]
cat > filename – This is used to create a new file.
cat >>filename – This is used to append the contents of the file
[user@sys108 ~]$ cat > jean1
[Link]’ s Institute of Technology 1
CS4458-Operating Systems Department of ADS 2023-2024
Have a Good Day
[user@sys108 ~]$ cat >> jean1
Welcome
[user@sys108 ~]$ cat jean1
Have a Good Day
Welcome
8) cp
This command helps us to create duplicate copies of ordinary files.
$cp source destination
[user@sys108 ~]$ cp jean1 jean2
[user@sys108 ~]$ cat jean2
Have a Good Day
Welcome
9) mv
This command is used to move or rename files.
$mv source destination
[user@sys108 ~]$ mv jean1 jean3
[user@sys108 ~]$ cat jean3
Have a Good Day
Welcome
10) ln
This command is to establish an additional filename for the same ordinary file.
$ln firstname secondname
user@sys108 ~]$ ln jean2 cse1
11) rm
This command is used to delete one or more files from the directory.
$rm [option] filename
$rm –i Asks the user if he wants to delete the file mentioned.
$rm –r Recursively delete the entire contents of the directory as well as the
directory itself.
[user@sys108 ~]$ rm jean2
II) Process and status information commands
1) who
This command gives the details of who all have logged in to the UNIX system currently.
$ who
[user@sys108 ~]$ who
user tty1 2014-11-11 19:16 (:0)
user pts/0 2014-11-11 19:19 (:0.0)
user pts/1 2014-11-11 19:19 (:0.0)
2) who am i
This command tells us as to when we had logged in and the system’s name for the
connection being used. $who am i
[user@sys108 ~]$ who am i
user pts/0 2014-11-11 19:19 (:0.0)
3) date
[Link]’ s Institute of Technology 2
CS4458-Operating Systems Department of ADS 2023-2024
This command displays the current date in different formats.
+%D mm/dd/yy +%w Day of the week
+%H Hr-00 to 23 +%a [Link]
+%M Min-00 to 59 +%h [Link]
+%S Sec-00 to 59 +%r Time in AM/PM
+%T HH:MM:SS +%y Last two digits of the year
[user@sys108 ~]$ date
Tue Nov 11 19:39:14 IST 2014
4) echo
This command will display the text typed from the keyboard.
$echo
[user@sys108 ~]$ echo welcome to 2015
welcome to 2015
III Text related commands
1. head
This command displays the initial part of the file. By default it displays first ten lines of the
file.
$head [-count] [filename]
[user@sys108 ~]$ head -1 jean3
Have a Good Day
2. tail
This command displays the later part of the file. By default it displays last ten lines of the
file.
$tail [-count] [filename]
[user@sys108 ~]$ tail -1 jean3
Welcome
3. wc
This command is used to count the number of lines, words or characters in a file.
$wc [-lwc] filename
[user@sys108 ~]$ wc -lwc jean3
2 4 24 jean3
4. find
The find command is used to locate files in a directory and in a subdirectory.
The –name option
This lists out the specific files in all directories beginning from the named
directory. Wild cards can be used.
The –type option
This option is used to identify whether the name of files specified are
ordinary files or directory files. If the name is a directory then use “-type d
“and if it is a file then use “-type f”.
The –mtime option
This option will allow us to find that file which has been modified before
or after a specified time. The various options available are –mtime n(on a
particular day),-mtime +n(before a particular day),-mtime –n(after a particular
[Link]’ s Institute of Technology 3
CS4458-Operating Systems Department of ADS 2023-2024
day)
The –exec option
This option is used to execute some commands on the files that are found
by the find command.
[user@sys108 ~]$ find fc.c
fc.c
IV File Permission commands
1) chmod
Changes the file/directory permission mode: $ chmod 777 file1
Gives full permission to owner, group and others
$ chmod o-w file1
Removes write permission for others.
[user@sys108 ~]$ chmod 777 [Link]
V Useful Commands:
1) exit - Ends your work on the UNIX system.
[user@sys108 ~]$ exit
exit
There are stopped jobs.
2) Ctrl-l or clear
Clears the screen.
[user@sys108 ~]$ clear
[user@sys108 ~]$
3) Ctrl-c
Stops the program currently running.
4) Ctrl-z
Pauses the currently running program.
5) man COMMAND
Looks up the UNIX command COMMAND in the online manual pages.
[user@sys108 ~]$ man
What manual page do you want?
[user@sys108 ~]$ man ls
6) history
List all commands typed so far.
[user@sys108 ~]$ history
109 man
110 man help
111 man ls
112 history
7) more FILE
Display the contents of FILE, pausing after each screenful.
There are several keys which control the output once a screenful has been printed.
[Link]’ s Institute of Technology 4
CS4458-Operating Systems Department of ADS 2023-2024
<enter> Will advance the output one line at a time.
<space bar> Will advance the output by another full screenful.
"q" Will quit and return you to the UNIX prompt.
[user@sys108 ~]$ more jean3
Have a Good Day
Welcome
8) less FILE
"less" is a program similar to "more", but which allows backward movement
in the file as well as forward movement.
[user@sys108 ~]$ less jean3
Have a Good Day
Welcome
jean3 (END)
9) lpr FILE
To print a UNIX text or PostScript file, type the following command at the system
prompt:
[user@sys108 ~]$ lpr jean3
lpr: Error - no default destination available.
RESULT: Thus the basic UNIX commands was successfully executed and verified
[Link]’ s Institute of Technology 5
CS4458-Operating Systems Department of ADS 2023-2024
EXP NO: 2(i) COMPARISON OF TWO STRINGS USING SHELL PROGRAM
AIM: To write a shell program to compare the two strings.
ALGORITHM:
1. Enter into the vi editor and go to the insert mode for entering the code
2. Read the first string.
3. Read the second string
4. Compare the two strings using the if loop
5. If the condition satisfies then print that two strings are equal else print two
strings are not equal.
6. Enter into the escape mode for the execution of the result and verify the output
PROGRAM:
#!/bin/bash
echo "Enter the first String : "
read str1
echo "Enter the second String : "
read str2
if [ $str1 = $str2 ]
then
echo "Strings are Equal"
else
echo "Strings are Unequal"
fi
OUTPUT:
[Link]’ s Institute of Technology 6
CS4458-Operating Systems Department of ADS 2023-2024
RESULT: Thus the shell program was successfully executed and verified.
EXP NO: 2(ii) MAXIMUM OF THREE NUMBERS USING SHELL PROGRAM
AIM: To write a shell program to find greatest of three numbers.
ALGORITHM:
1. Declare the three variables.
2. Check if A is greater than B and C.
3. If so, print A is greater.
4. Else check if B is greater than C.
5. If so, print B is greater.
6. Else print C is greater.
PROGRAM:
#!/bin/bash
echo "Enter A : "
read a
echo "Enter B : "
read b
echo "Enter C : "
read c
if [ $a -gt $b -a $a -gt $c ]
then
echo "A is greater"
elif [ $b -gt $a -a $b -gt $c ]
then
echo "B is greater"
else
echo "C is greater"
fi
OUTPUT:
[Link]’ s Institute of Technology 7
CS4458-Operating Systems Department of ADS 2023-2024
RESULT: Thus the shell program was successfully executed and verified.
EXP NO: 2(iii) MULTIPLICATION TABLE USING SHELL PROGRAM
AIM: To write the shell program to check whether the number is odd or even.
ALGORITHM:
Step 1: Start the program
Step 2: Get the input, use a for loop to read numbers in the range from 1 to 10
Step 3: Calculate the result
Step 4: Increment the variable and repeat
Step 5: Display the result.
Step 6: Terminate the program.
PROGRAM:
#!/bin/bash
echo "which number to generate multiplication table"
read number
i=1
while [ $i -le 10 ]
do
echo "$number * $i =`expr $number \* $i ` "
i=`expr $i + 1`
done
OUTPUT:
[Link]’ s Institute of Technology 8
CS4458-Operating Systems Department of ADS 2023-2024
RESULT: Thus the shell program was successfully executed and verified.
EXP NO: 2(iv) BASIC ARITHMETIC OPERATION USING SHELL PROGAMMING
AIM: To write the shell program to perform the basic arithmetic operations.
ALGORITHM:
Step 1: Start the program.
Step 2: Read the inputs
Step 3: Perform the arithmetic calculation.
Step 4: Print the result.
Step 5: Stop the execution.
PROGRAM:
#!/bin/bash
echo "Enter value A :"
read a
echo "enter value B : "
read b
c=`expr $a + $b`
echo "sum:"$c
c=`expr $a - $b`
echo "sub:"$c
c=`expr $a \* $b`
echo "mul:"$c
c=`expr $a / $b`
echo "div:"$c
[Link]’ s Institute of Technology 9
CS4458-Operating Systems Department of ADS 2023-2024
OUTPUT :
RESULT: Thus the shell program was successfully executed and verified.
[Link]’ s Institute of Technology 10
CS4458-Operating Systems Department of ADS 2023-2024
[Link]’ s Institute of Technology 11
CS4458-Operating Systems Department of ADS 2023-2024
[Link]’ s Institute of Technology 12
CS4458-Operating Systems Department of ADS 2023-2024
[Link]’ s Institute of Technology 13
CS4458-Operating Systems Department of ADS 2023-2024
[Link]’ s Institute of Technology 14
CS4458-Operating Systems Department of ADS 2023-2024
[Link]’ s Institute of Technology 15