Unix Lab Manual Part B PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

lOMoARcPSD|3433744

Unix Lab Manual Part B

UNIX Programming (Bangalore University)

StuDocu is not sponsored or endorsed by any college or university


Downloaded by Manoj BE (manojporsche24@gmail.com)
lOMoARcPSD|3433744

IV BCA - Unix Lab Manual


Part - B

11. Execution of various file/directory handling commands.

1) pwd COMMAND:
pwd - Print Working Directory. pwd command prints the full filename of
the current working directory.
SYNTAX:
The Syntax is
pwd [options]

2) cd COMMAND:
cd command is used to change the directory.

SYNTAX:
The Syntax is
cd [directory | ~ | ./ | ../ | - ]

3) ls COMMAND:
ls command lists the files and directories under current working directory.
SYNTAX:
The Syntax is
ls [OPTIONS]... [FILE]
OPTIONS:
-l Lists all the files, directories and their mode, Number of links,
owner of the file, file size, Modified date and time and filename.
-t Lists in order of last modification time.
-a Lists all entries including hidden files.
-d Lists directory files instead of contents.
-p Puts slash at the end of each directories.
-u List in order of last access time.
-i Display inode information.

4) rm COMMAND:
rm linux command is used to remove/delete the file from the directory.
SYNTAX:
The Syntax is
rm [options..] [file | directory]
OPTIONS:
-f Remove all files in a directory without prompting the user.
-i Interactive. With this option, rm prompts for confirmation before
removing any files.

Unix Lab Manual -B Page 1

Downloaded by Manoj BE (manojporsche24@gmail.com)


lOMoARcPSD|3433744

5) mv COMMAND:
mv command which is short for move. It is used to move/rename file from one directory to
another. mv command is different from cp command as it completely removes the file from the
source and moves to the directory specified, where cp command just copies the content from one
file to another.
SYNTAX:
The Syntax is
mv [-f] [-i] oldname newname
OPTIONS:
-f This will not prompt before overwriting (equivalent to --
reply=yes). mv -f will move the file(s) without prompting even
if it is writing over an existing target.
-i Prompts before overwriting another file.

6) cat COMMAND:
cat linux command concatenates files and print it on the standard output.
SYNTAX:
The Syntax is
cat [OPTIONS] [FILE]...
OPTIONS:
-A Show all.
-b Omits line numbers for blank space in the output.
-E Displays a $ (dollar sign) at the end of each line.
-n Line numbers for all the output lines.

7) cp COMMAND:
cp command copy files from one location to another. If the destination is an existing file, then
the file is overwritten; if the destination is an existing directory, the file is copied into the
directory (the directory is not overwritten).
SYNTAX:
The Syntax is
cp [OPTIONS]... SOURCE DEST

8) echo COMMAND:
echo command prints the given input string to standard output.
SYNTAX:
The Syntax is
echo [options..] [string]

9)mkdir COMMAND:
mkdir command is used to create one or more directories.
SYNTAX:
The Syntax is
mkdir [options] directories

Unix Lab Manual -B Page 2

Downloaded by Manoj BE (manojporsche24@gmail.com)


lOMoARcPSD|3433744

OPTIONS:
-m Set the access mode for the new directories.
-p Create intervening parent directories if they don't exist.
-v Print help message for each directory created.

10) rmdir COMMAND:


rmdir command is used to delete/remove a directory and its subdirectories.
SYNTAX:
The Syntax is
rmdir [options..] Directory
OPTIONS:
-p Allow users to remove the directory dir name and its parent
directories which become empty.

12. Shell script to check whether the given number is even or odd.

clear
echo "enter a number"
read n
if [ `expr $n % 2` -eq 0 ]; then
echo " $n is even"
else
echo "$n is odd"
fi

13. Shell script to find gcd and lcm of given two numbers.
clear
echo "Enter two intergers"
read m n
echo " To find GCD and LCM"
echo "===================="
echo "given two numbers are"
echo "m= $m and n=$n"
temp=`expr $m \* $n`

while [ $m != $n ]
do
Unix Lab Manual -B Page 3

Downloaded by Manoj BE (manojporsche24@gmail.com)


lOMoARcPSD|3433744

if [ $m -gt $n ]
then
m=`expr $m - $n`
else
n=`expr $n - $m`
fi
done

echo GCD=$n
lcm=`expr $temp / $n`
echo LCM=$lcm

14.Shell script to create a simple calculator that performs basic arithmetic


operations
clear
sum=0
i="y"
echo " Enter one no."
read n1
echo "Enter second no."
read n2
while [ $i = "y" ]
do
echo "1.Addition"
echo "2.Subtraction"
echo "3.Multiplication"
echo "4.Division"
echo "Enter your choice"
read ch
case $ch in
1)sum=`expr $n1 + $n2`
Unix Lab Manual -B Page 4

Downloaded by Manoj BE (manojporsche24@gmail.com)


lOMoARcPSD|3433744

echo "Sum ="$sum;;


2)sum=`expr $n1 - $n2`
echo "Sub = "$sum;;
3)sum=`expr $n1 \* $n2`
echo "Mul = "$sum;;
4)sum=`expr $n1 / $n2`
echo "Div = "$sum;;
*)echo "Invalid choice";;
esac
echo "Do u want to continue ?"
read i
if [ $i != "y" ]
then
exit
fi
done

15. Shell script to reverse a given string

clear
echo "enter the string"
read str
len=`expr $str | wc -c`
len=`expr $len - 1`
while [ $len -gt 0 ]
do
ch=`echo $str | cut -c $len`
echo -n $ch
len=`expr $len - 1`
done

Unix Lab Manual -B Page 5

Downloaded by Manoj BE (manojporsche24@gmail.com)


lOMoARcPSD|3433744

16.Shell script to wish according to day time.

clear
hours=`date|cut -c12-13`
if [ $hours -le 12 ]
then
echo "Good Morning"
else
if [ $hours -le 16 ]
then
echo "Good Afternoon"
elif [ $hours -le 20 ]
then
echo "Good Evening"
else
echo "Good Night"
fi
fi

17.Shell script to assign a file permission to a given file using


a) symbolic mode b) absolute mode
clear
option=y
while [ $option = y ]
do
echo "1. Symbolic mode 2. Absolute mode"
echo "enter your choice"
read ch

case $ch in
1) clear
echo "changing the file permission in symbolic mode"
echo `pwd` contents are
Unix Lab Manual -B Page 6

Downloaded by Manoj BE (manojporsche24@gmail.com)


lOMoARcPSD|3433744

ls -l
echo "enter the file name"
read fname
echo "enter the file type u/g/o"
read type
echo "enter one of the permission r/w/x"
read permission

#changing the mode with given input


chmod $type+$permission $fname
echo "after changing the permission"
ls -l
echo;;
2) clear
echo "changing the permission is absolute mode"
echo `pwd` contents are
ls -l
echo "enter the filename"
read fname
echo "enter the permission"
read permission
chmod $permission $fname
echo "after changind the file permission"
ls -l
echo;;
esac
echo "do you want to continue y/n "
read option

Unix Lab Manual -B Page 7

Downloaded by Manoj BE (manojporsche24@gmail.com)


lOMoARcPSD|3433744

18.Shell program to compression and decompression of files using gzip and


gunzip
clear
echo "enter the file name"
read fname
echo "size of $file name before compression is"
wc -c $fname
gzip $fname
echo "size of file name after compression"
wc -c $fname.gz
#unzip the file
gunzip $fname.gz
echo "after decompression $fname"
wc -c $fname
echo $fname

19.To search a pattern using grep and fgrep command


clear
echo "enter the file name"
read fname1
echo "enter the contents of $fname1 and press ctrl+d"
cat >$fname1
echo "enter the file name 2"
read fname2
echo "enter the contents of $fname2 and press ctrl+w"
cat >$fname2
echo "enter the pattern"
read ptrn
#echo $ptrn $fname1 $fname2
grep -i $ptrn $fname1 $fname2
fgrep -i $ptrn $fname1 $fname2

Unix Lab Manual -B Page 8

Downloaded by Manoj BE (manojporsche24@gmail.com)


lOMoARcPSD|3433744

20. Menu driven shell script to demonstrate the use of tail , cmp, uniq and
rm commands
clear
opt=y
while [ $opt = y ]
do
clear
echo "----------Menu driven program --------"
echo
echo
echo "1. Usage of tail command "
echo
echo "2. Usage of cmp command"
echo
echo "3. Usage of uniq command"
echo
echo "4. Usage of rm -r command"
echo
echo "enter your choice"
read ch
case $ch in
1) clear
echo "================================"
echo "tail displays lines from bottom of the file"
echo
printf "enter number of lines to display"
read n
echo
echo last $n lines of file temp "
echo "------------------------"
echo

Unix Lab Manual -B Page 9

Downloaded by Manoj BE (manojporsche24@gmail.com)


lOMoARcPSD|3433744

tail -$n temp;;


2) clear
echo "================================"
echo "cmp compares two files and gives the point of difference"
echo
echo temp1 and temp2 are compared
echo "------------------------"
echo
cmp temp1 temp2;;
3) clear
echo "================================"
echo "uniq eliminates adjacent duplicate lines"
echo "======================================="
echo
echo unique lines of temp1
echo
echo "------------------------"
cat temp1 | sort | uniq;;
4) clear
echo "================================"
echo " rm -r deletes the directory along wih contents"
echo "============================================="
echo
echo "enter the subdirectory name"
read subdir
mkdir $subdir
cd $subdir
touch file1 file2
echo current directory is `pwd`
cd ..
if rm -r $subdir

Unix Lab Manual -B Page 10

Downloaded by Manoj BE (manojporsche24@gmail.com)


lOMoARcPSD|3433744

then
echo $subdir removed succesffully
fi;;
*) echo "invalid choice"
esac
echo "do you like to continue ?"
read opt
done
echo
tail -$n temp;;

*********

Unix Lab Manual -B Page 11

Downloaded by Manoj BE (manojporsche24@gmail.com)

You might also like