Skip to content

linux experiments

Experiment – 1

Aim:

Getting familiar with UNIX/Linux by using at least 10 basic commands.

Commands with Description

1) pwd – Prints the current working directory. ​Example:
pwd
2) ls – Lists files and folders in a directory. ​Example:
ls -la
3) cd – Changes the current directory. ​Example:
cd Documents
4) mkdir – Creates a new directory. ​Example:
mkdir test-folder
5) rmdir – Removes an empty directory. ​Example:
rmdir test-folder
6) touch – Creates an empty file. ​Example:
touch sample.txt
7) cat – Displays the content of a file. ​Example:
cat sample.txt
8) cp – Copies a file. ​Example:
cp Sample.txt copy-sample.txt
9) mv – Moves or renames a file. ​Example:
mv copy-sample.txt Documents/
10) rm – Removes a file. ​Example:
rm sample.txt

Output

ubuntu@ubuntu:~$ whoami
ubuntu

ubuntu@ubuntu:~$ pwd
/home/ubuntu

ubuntu@ubuntu:~$ ls
Desktop Documents Downloads Music Pictures
Public Templates Videos snap

ubuntu@ubuntu:~$ cd Documents

ubuntu@ubuntu:~/Documents$ pwd
/home/ubuntu/Documents

ubuntu@ubuntu:~/Documents$ cd ..

ubuntu@ubuntu:~$ mkdir test-folder

ubuntu@ubuntu:~$ touch Sample

ubuntu@ubuntu:~$ cat Sample
hello

2. experiment 2 : Write a shell script to generate a multiplication table

Ans:

Program:

#!/bin/bash
echo "Enter a number:"
read num

for i in {1..10}
do
echo "$num x $i = $((num * i))"
done

Output:

ubuntu@ubuntu:~$ nano table.sh
ubuntu@ubuntu:~$ chmod +x table.sh
ubuntu@ubuntu:~$ ./table.sh
Enter a number:
5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

3. Copy multiple files to a directory

Ans:

Program:

#!/bin/bash

mkdir source
mkdir destination
touch source/{file1.txt,file2.txt,file3.txt}

echo "Enter source directory:"
read src

echo "Enter destination directory:"
read dest

if [ -d "$src" ] && [ -d "$dest" ]
then
cp "$src"/* "$dest"/
echo "Files copied successfully."
else
echo "Error: Invalid directory name."
fi

Output:

ubuntu@ubuntu:~$ nano copyfiles.sh
ubuntu@ubuntu:~$ chmod +x copyfiles.sh
ubuntu@ubuntu:~$ ./copyfiles.sh
Enter source directory:
source
Enter destination directory:
destination
Files copied successfully.

4. Factorial of a Number

Ans:

Program:

#!/bin/bash

echo "Enter a number:"
read num

fact=1

for ((i=1; i<=num; i++))
do
fact=$((fact * i))
done

echo "Factorial of $num is $fact"

Output:

ubuntu@ubuntu:~$ nano fact.sh
ubuntu@ubuntu:~$ chmod +x fact.sh
ubuntu@ubuntu:~$ ./fact.sh
Enter a number:
5
Factorial of 5 is 120

Exp 5 — Prime Numbers Between m and n

echo "Enter starting number:"
read m

echo "Enter ending number:"
read n

for ((num=m; num<=n; num++))
do
count=0
for ((i=2; i<=num/2; i++))
do
if [ $((num%i)) -eq 0 ]
then
count=1
break
fi
done

if [ $count -eq 0 ] && [ $num -gt 1 ]
then
echo $num
fi
done

▶ Output

Enter starting number:
10
Enter ending number:
20
11
13
17
19

Exp 7 — Maximum & Minimum Numbers

echo "Enter numbers separated by space:"
read -a arr

max=${arr[0]}
min=${arr[0]}

for num in "${arr[@]}"
do
if [ $num -gt $max ]
then
max=$num
fi

if [ $num -lt $min ]
then
min=$num
fi
done

echo "Maximum: $max"
echo "Minimum: $min"

▶ Output

Enter numbers separated by space:
4 9 2 15 7
Maximum: 15
Minimum: 2

Exp 8 — Palindrome Check

echo "Enter a string:"
read str

reverse=$(echo $str | rev)

if [ "$str" = "$reverse" ]
then
echo "Palindrome"
else
echo "Not a Palindrome"
fi

▶ Output

Enter a string:
madam
Palindrome

Exp 9 — Count Vowels

echo "Enter a string:"
read str

count=0

for ((i=0; i<${#str}; i++))
do
char=${str:$i:1}

case $char in
[aeiouAEIOU])
count=$((count + 1))
;;
esac
done

echo "Total vowels: $count"

▶ Output

Enter a string:
Hello World
Total vowels: 3

Exp 10 — Simple Interest

echo "Enter Principal:"
read p

echo "Enter Rate:"
read r

echo "Enter Time:"
read t

si=$((p * r * t / 100))

echo "Simple Interest is $si"

▶ Output

Enter Principal:
1000
Enter Rate:
5
Enter Time:
2
Simple Interest is 100

Want to print your doc?
This is not the way.
Try clicking the ··· in the right corner or using a keyboard shortcut (
CtrlP
) instead.