0% found this document useful (0 votes)
59 views

Linux Lab-1

The document describes a shell script that provides a menu to demonstrate various file commands in Linux. It allows the user to select options to create, display, copy, remove, move, compare, count words, split, and view differences of files. For each selection, it prompts for filenames and displays outputs of the selected file command. The script uses conditionals and functions to call the appropriate file commands based on the user's menu selection.

Uploaded by

Naveen Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Linux Lab-1

The document describes a shell script that provides a menu to demonstrate various file commands in Linux. It allows the user to select options to create, display, copy, remove, move, compare, count words, split, and view differences of files. For each selection, it prompts for filenames and displays outputs of the selected file command. The script uses conditionals and functions to call the appropriate file commands based on the user's menu selection.

Uploaded by

Naveen Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 32

FILE COMMAND IN LINUX

echo "File Command"


echo "1. CREATE"
echo "2. DISPLAY"
echo "3. COPY"
echo "4. REMOVE"
echo "5. MOVE"
echo "6. COMPARE"
echo "7. WOAD COUNT"
echo "8. SPLIT"
echo "9. DIFFERENCE"
echo "10.Exit"
echo -n "Enter Your Choice : "
read ch
if [ "$ch" = 1 ]
then
echo "CREATE COMMAND"
echo " Enter Your File Name"
read a
vi $a
echo "File is Created"
sh f1.sh

elif [ "$ch" = 2 ]
then
echo "DISPLAY"
ls
echo "Enter the File Name to be Display"
read a
cat $a
sh f1.sh

1
elif [ "$ch" = 3 ]
then
echo "COPY COMMAND"
ls
echo "Enter File Name for Already Created :"
read f1
echo "Enter File Name for Copied File : "
read f2
cp $f1 $f2
echo "File is Copied"
cat $f2
sh f1.sh

elif [ "$ch" = 4 ]
then
echo "REMOVE COMMAND"
ls
echo "Enter the File Name for Remove :"
read file1
rm $file1
echo "The File is Removed"
ls
sh f1.sh
elif [ "$ch" = 5 ]
then
echo "MOVE COMMAND"
ls
echo " Enter the File Name to be Moved"
read m
echo " Enter the Moved File Path "
read p
mv $m $p

2
echo " File is Moved"
ls
sh f1.sh

elif [ "$ch" = 6 ]
then
ls
echo "COMPARE COMMAND"
echo "Enter the Already Exist First File Name"
read file1
echo "Enter the Already Exist Second File Name"
read file2
echo "First File Content"
cat $file1
echo "Second File Content"
cat $file2
cmp $file1 $file2
sh f1.sh
elif [ "$ch" = 7 ]
then
echo "WORD COUNT COMMAND"
echo "Enter the Already Exist File Name"
read file1
echo "The Content Of the $file1 is"
cat $file1
echo "The Word Count of $file1 is :"
wc $file1
sh f1.sh

3
elif [ "$ch" = 8]
then
echo "SPLIT COMMAND"
ls
echo "Enter File Name to be Split"
read file1
split -d $file1
ls
sh f1.sh

elif [ "$ch" = 9 ]
then
echo "DIFFERENCE COMMAND"
ls
echo "ENTER THE First File"
read file1
echo "Enter the Second File"
read file2
echo "First File Content"
cat $file1
echo "Second File Content"
cat $file2
diff $file1 $file2
sh f1.sh

elif [ "$ch" = 10 ]
then
echo "exit"
fi

4
OUTPUT

mythili@mythili:~$ nano file.sh


mythili@mythili:~$ chmod +x file.sh
mythili@mythili:~$ ./file.sh
File Command
1. CREATE
2. DISPLAY
3. COPY
4. REMOVE
5. MOVE
6. COMPARE
7. WOAD COUNT
8. SPLIT
9. DIFFERENCE
10.Exit
Enter Your Choice : 1
CREATE COMMAND
Enter Your File Name
sample1
Enter Your Choice : 2
DISPLAY
Desktop Downloads Music Pictures sample1 Templates
Documents file.sh new1 Public sample2 Videos
Enter the File Name to be Display
sample1
hai, my first linux program
Enter Your Choice : 3
COPY COMMAND
Desktop Downloads Music Pictures sample1 Templates
Documents file.sh new1 Public sample2 Videos
Enter File Name for Already Created :
sample1

5
Enter File Name for Copied File :
sample2
File is Copied
hai, my first linux program
Enter Your Choice : 4
REMOVE COMMAND
Desktop Downloads Music Pictures sample1 Templates
Documents file.sh new1 Public sample2 Videos
Enter the File Name for Remove :
new1
The File is Removed
Desktop Downloads Music Public sample2 Videos
Documents file.sh Pictures sample1 Templates
Enter Your Choice : 5
MOVE COMMAND
Desktop Downloads Music Public sample2 Videos
Documents file.sh Pictures sample1 Templates
Enter the File Name to be Moved
sample1
Enter the Moved File Path
sample2
File is Moved
Desktop Downloads Music Public Templates
Documents file.sh Pictures sample2 Videos
nter Your Choice : 6
Desktop Downloads Music Public sample2 Videos
Documents file.sh Pictures sample1 Templates
COMPARE COMMAND
Enter the Already Exist First File Name
sample1
Enter the Already Exist Second File Name
sample2
First File Content

6
welcome to linux program
Second File Content
hai, my first linux program
sample1 sample2 differ: byte 1, line 1
Enter Your Choice : 7
WORD COUNT COMMAND
Enter the Already Exist File Name
sample1
The Content Of the sample1 is
welcome to linux program
The Word Count of sample1 is :
1 4 25 sample1

Enter Your Choice : 8


Enter File Name to be Split
sample1
Desktop Downloads Music Public sample2 Videos
Documents file.sh Pictures sample1 Templates xaa
Enter Your Choice : 9
DIFFERENCE COMMAND
Desktop Downloads Music Public sample2 Videos
Documents file.sh Pictures sample1 Templates xaa
ENTER THE First File
sample1
Enter the Second File
sample2
First File Content
welcome to linux program
Second File Content
hai, my first linux program
1c1
< welcome to linux program
---

7
> hai, my first linux program
Enter Your Choice : 10
mythili@mythili:~$

RESULT:

8
SYSTEM CONFIGURATION DETAILS

clear
echo "SYSTEM CONFIGURATION DETAILS"
echo "----------------------------------------------------- "
echo "a. Currently Loged User : $USER"
echo " Log Name : $LOGNAME"
echo "b. Current Shell : $SHELL"
echo " Home Directory : $HOME"
echo " Operating System Type : $OSTYPE"
echo " Current Path Setting : $PATH"
echo " Current Working Directory : $PWD"
echo "c. Current Logged No.Of.Users :"
who | wc -l
echo " Available Shells:"
cat /etc/shells
echo "d. CPU Information:"
grep "model name" /proc/cpuinfo
echo "e. Memory Information:"
vmstat

9
OUTPUT
mythili@mythili:~$ nano system.sh
mythili@mythili:~$ chmod +x system.sh
mythili@mythili:~$ ./system.sh
SYSTEM CONFIGURATION DETAILS
-----------------------------------------------------
a. Currently Loged User : mythili
Log Name : mythili
b. Current Shell : /bin/bash
Home Directory : /home/mythili
Operating System Type : linux-gnu
Current Path Setting :
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/
snap/bin
Current Working Directory : /home/mythili
c. Current Logged No.Of.Users :
1
Available Shells:
# /etc/shells: valid login shells
/bin/sh
/bin/bash
/usr/bin/bash
/bin/rbash
/usr/bin/rbash
/bin/dash
/usr/bin/dash

d. CPU Information:
model name : AMD PRO A4-4350B R4, 5 COMPUTE CORES 2C+3G
model name : AMD PRO A4-4350B R4, 5 COMPUTE CORES 2C+3G
e. Memory Information:
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----

10
r b swpd free buff cache si so bi bo in cs us sy id wa st
1 0 0 321588 46652 817924 0 0 64 10 101 153 2 1 96 1 0

RESULT

11
PIPES, REDIRECTION AND TEE COMMANDS

echo "1.PIPES"
echo "2.STANDARD INPUT REDIRECTION"
echo "3.STANDARD OUTPUT REDIRECTION"
echo "4.TEE COMMAND"
echo "5.QUIT"
echo -n "ENTER YOUR CHOICE : "
read ch
if [ "$ch" = 1 ]
then
ls -l | grep "^-"
sh pro3.sh
elif [ "$ch" = 2 ]
then
ls
echo "Enter the File Name"
read file1
cat<$file1
sh pro3.sh
elif [ "$ch" = 3 ]
then
ls
echo "Enter the File Name1"
read file1
echo "Enter the File Name2"
read file2
cat $file1 >> $file2
echo "Content of File1"
cat $file1
echo "Content of File2"
cat $file2

12
sh pro3.sh
elif [ "$ch" = 4 ]
then
ls
echo "Enter the File Name"
read file1
cat temp | tree $file1
echo "Content of Temp"
cat temp
sh pro3.sh
elif [ "$ch" = 5 ]
then
echo "Exit"
fi

13
OUTPUT
ythili@mythili:~$ nano pipe.sh
mythili@mythili:~$ chmod +x pipe.sh
mythili@mythili:~$ ./pipe.sh
1.PIPES
2.STANDARD INPUT REDIRECTION
3.STANDARD OUTPUT REDIRECTION
4.TEE COMMAND
5.QUIT
ENTER YOUR CHOICE : 1
-rwxrwxr-x 1 mythili mythili 1896 Mar 26 04:49 file.sh
-rwxrwxr-x 1 mythili mythili 848 Mar 26 05:15 pipe.sh
-rw-rw-r-- 1 mythili mythili 25 Mar 26 04:40 sample1
-rw-rw-r-- 1 mythili mythili 28 Mar 26 04:31 sample2
-rwxrwxr-x 1 mythili mythili 626 Mar 26 05:10 system.sh
-rw-rw-r-- 1 mythili mythili 25 Mar 26 04:52 xaa
ENTER YOUR CHOICE : 2
Desktop Downloads Music pipe.sh sample1 system.sh Videos
Documents file.sh Pictures Public sample2 Templates xaa
Enter the File Name
sample1
welcome to linux program
ENTER YOUR CHOICE : 3
Desktop Downloads Music pipe.sh sample1 system.sh Videos
Documents file.sh Pictures Public sample2 Templates xaa
Enter the File Name1
sample2
Enter the File Name2
sample1
Content of File1
hai, my first linux program
Content of File2

14
welcome to linux program
hai, my first linux program
ENTER YOUR CHOICE : 4
Desktop Downloads Music pipe.sh sample1 system.sh Videos
Documents file.sh Pictures Public sample2 Templates xaa
Enter the File Name
Music
cat: tempMusic
├── f1
│   └── f3
│   └── f4
│   └── f5
└── f2

5 directories, 0 files
ENTER YOUR CHOICE : 5
Exit
mythili@mythili:~$

RESULT:

15
DISPLAYING CURRENT DATE,USER NAME,FILE NAME AND DIRECTION

echo "1. DATE"


echo "2. USER NAME"
echo "3. LISTING FILES & DIRECTORIES"
echo "4. EXIT"
echo -n "Enter Your Choice : "
read ch

if [ "$ch" = 1 ]
then
echo "The Date is :"
date
sh pro4.sh

elif [ "$ch" = 2 ]
then
echo "The User Name is :"
who am i
sh pro4.sh

elif [ "$ch" = 3 ]
then
echo "Listing Files and Directories"
ls -l
sh pro4.sh

elif [ "$ch" = 4 ]
then
echo "Exit"
fi

16
OUTPUT
ythili@mythili:~$ nano display.sh
mythili@mythili:~$ chmod +x display.sh
mythili@mythili:~$ ./display.sh
1. DATE
2. USER NAME
3. LISTING FILES & DIRECTORIES
4. EXIT
Enter Your Choice : 1
The Date is :
Friday 26 March 2021 05:50:07 AM IST
Enter Your Choice : 2
The User Name is :
Mythili
nter Your Choice : 3
Listing Files and Directories
total 52
drwxr-xr-x 2 mythili mythili 4096 Feb 20 10:41 Desktop
-rwxrwxr-x 1 mythili mythili 410 Mar 26 05:59 display.sh
drwxr-xr-x 2 mythili mythili 4096 Feb 19 23:04 Documents
drwxr-xr-x 2 mythili mythili 4096 Feb 19 23:04 Downloads
-rwxrwxr-x 1 mythili mythili 1896 Mar 26 04:49 file.sh
drwxr-xr-x 4 mythili mythili 4096 Mar 26 05:39 Music
drwxr-xr-x 2 mythili mythili 4096 Feb 19 23:04 Pictures
-rwxrwxr-x 1 mythili mythili 849 Mar 26 05:43 pipe.sh
drwxr-xr-x 2 mythili mythili 4096 Feb 19 23:04 Public
-rw-rw-r-- 1 mythili mythili 0 Mar 26 05:37 sample1
-rw-rw-r-- 1 mythili mythili 0 Mar 26 05:23 sample2
-rwxrwxr-x 1 mythili mythili 626 Mar 26 05:10 system.sh
drwxr-xr-x 2 mythili mythili 4096 Feb 19 23:04 Templates
drwxr-xr-x 2 mythili mythili 4096 Feb 19 23:04 Videos
-rw-rw-r-- 1 mythili mythili 25 Mar 26 04:52 xaa
Enter Your Choice : 4

17
Exit
mythili@mythili:~$

RESULT

18
FILTER COMMAND IN LINUX

echo "1. GREP FILTER"


echo "2. WC FILTER"
echo "3. CUT FILTER"
echo "4. TR FILTER"
echo "5. EXIT"

echo -n "ENTER YOUR CHOICE : "


read ch

if [ "$ch" = 1 ]
then
ls
echo "Enter the File Name : "
read a
echo "The File Content is : "
cat $a
echo "The Result is "
grep "file2" $a
sh pro5.sh
elif [ "$ch" = 2 ]
then
ls
echo "Enter the File Name : "
read a
echo "The File Content is : "
cat $a

echo "The File Contain the following No.of.Lines,Words & Characters :"
wc $a
sh pro5.sh

19
elif [ "$ch" = 3 ]
then
ls
echo "Enter the File Name : "
read a
echo "The File Content is :"
cat $a
echo "The Cut File Content is :"
cut -c1,3 $a
sh pro5.sh
elif [ "$ch" = 4 ]
then
ls
echo "Enter the File Name : "
read a
echo "The Content is : "
cat $a
echo "The Tr command Result is :"
cat $a|tr "[a-z]" "[A-Z]"
sh pro5.sh
elif [ "$ch" = 5 ]
then
echo "Exit"
fi

20
OUTPUT
ythili@mythili:~$ nano filter.sh
mythili@mythili:~$ ./filter.sh
1. GREP FILTER
2. WC FILTER
3. CUT FILTER
4. TR FILTER
5. EXIT
ENTER YOUR CHOICE : 1
esktop Documents file.sh Music pipe.sh sample1 system.sh Videos
display.sh Downloads filter.sh Pictures Public sample2 Templates xaa
Enter the File Name :
sample1
The File Content is :
welcome to linux program
ENTER YOUR CHOICE : 2
Desktop Documents file.sh Music pipe.sh sample1 system.sh Videos
display.sh Downloads filter.sh Pictures Public sample2 Templates xaa
Enter the File Name :
sample1
The File Content is :
welcome to linux program
The File Contain the following No.of.Lines,Words & Characters :
1 4 25 sample1

NTER YOUR CHOICE : 3


Desktop Documents file.sh Music pipe.sh sample1 system.sh Videos
display.sh Downloads filter.sh Pictures Public sample2 Templates xaa
Enter the File Name :
sample1
The File Content is :
welcome to linux program
The Cut File Content is :

21
Wl
NTER YOUR CHOICE : 4
Desktop Documents file.sh Music pipe.sh sample1 system.sh Videos
display.sh Downloads filter.sh Pictures Public sample2 Templates xaa
Enter the File Name :
sample1
The Content is :
welcome to linux program
The Tr command Result is :
WELCOME TO LINUX PROGRAM
ENTER YOUR CHOICE : 5
Exit
mythili@mythili:~$

RESULT

22
REMOVING ZERO BYTE FILES

echo "Current Path"


pwd
echo "List of Files & Directories of Current Directory"
ls

for i in *
do
if [ -f $i -a ! -s $i ]
then
rm $i
sleep 1
echo "$i file is Removed"
fi
done
ls

23
OUTPUT

mythili@mythili:~$ nano zero.sh


mythili@mythili:~$ chmod +x zero.sh
mythili@mythili:~$ ./zero.sh
Current Path
/home/mythili
List of Files & Directories of Current Directory
Desktop Downloads Music Public system.sh xaa
display.sh file.sh Pictures sample1 Templates zero.sh
Documents filter.sh pipe.sh sample2 Videos
sample2 file is Removed
Desktop Downloads Music Public Templates zero.sh
display.sh file.sh Pictures sample1 Videos
Documents filter.sh pipe.sh system.sh xaa

RESULT:

24
SUM OF THE INDIVIDUAL DIGITS

echo "Enter the Number : "


read n
a=0
sum=0

while [ $n -gt 0 ]
do
a=$(($n%10))
n=$(($n/10))
sum=$(($sum+$a))
done

echo "The SUM OF THE INDIVIDUAL DIGITS OF A GIVEN NO IS: $sum"

25
OUTPUT

ythili@mythili:~$ nano digit.sh


mythili@mythili:~$ chmod +x digit.sh
mythili@mythili:~$ ./digit.sh
Sum of Individual Digits
********************
Enter the Number :
123456
The SUM OF THE INDIVIDUAL DIGITS OF A GIVEN NO IS: 21

RESULT:

26
GREATEST AMONG THE GIVEN NUMBERS

echo "Enter the First number"


read n1
echo "Enter the Second number"
read n2
echo "Enter the third number"
read n3

if [ $n1 -eq $n2 ] && [ $n1 -eq $n3 ]


then
echo "Numbers are equal"

elif [ $n1 -gt $n2 ] && [ $n1 -gt $n3 ]


then
echo "First Number is greatest"

elif [ $n2 -gt $n1 ] && [ $n2 -gt $n3 ]


then
echo "Second Number is greatest"
else
echo "Third Number is greatest"
fi

27
OUTPUT

mythili@mythili:~$ nano great.sh


mythili@mythili:~$ chmod +x great.sh
mythili@mythili:~$ ./great.sh
Enter the First number
5
Enter the Second number
5
Enter the third number
5
Numbers are equal
mythili@mythili:~$ ./great.sh
Enter the First number
5
Enter the Second number
9
Enter the third number
7
Second Number is greatest

RESULT:

28
PALINDROME CHECKING

echo "Enter the String:"


read str
len=${#str}
flag=1
for((i=0;i<=len/2;i++))
do
c1="${str:$i:1}"
c2="${str:$len-$i-1:1}"
if [ $c1 != $c2 ]
then
flag=0
echo "String is not palindrome"
break
fi
done

if(( $flag==1))
then
echo "Input String is Palindrom"
fi

29
OUTPUT
mythili@mythili:~$ nano palin.sh
mythili@mythili:~$ chmod +x palin.sh
mythili@mythili:~$ ./palin.sh
Enter the String:
mam
Input String is Palindrom
Enter the String:
java
String is not palindrome

RESULT:

30
MULTIPLICATION TABLE

echo "Enter the Row Value :"


read r
echo "Enter the Table Value :"
read n
for((x=1;x<=r;x=x+1))
do
echo "$x * $n = $(($x*$n))"
done

31
OUTPUT

mythili@mythili:~$ nano multi.sh


mythili@mythili:~$ chmod +x multi.sh
mythili@mythili:~$ ./multi.sh
Enter the Row Value :
8
Enter the Table Value :
8
1*8=8
2 * 8 = 16
3 * 8 = 24
4 * 8 = 32
5 * 8 = 40
6 * 8 = 48
7 * 8 = 56
8 * 8 = 64
mythili@mythili:~$

32

You might also like