0% found this document useful (0 votes)
13 views18 pages

Linux Rec 2024 III BSC CS

The document outlines various shell commands and scripts used for file manipulation, system configuration, and basic programming tasks. It includes commands for removing files, copying, concatenating, and comparing files, as well as scripts for displaying system information and performing calculations. Additionally, it provides examples of using pipes, redirection, and functions in shell scripting.

Uploaded by

rajmanisridhar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views18 pages

Linux Rec 2024 III BSC CS

The document outlines various shell commands and scripts used for file manipulation, system configuration, and basic programming tasks. It includes commands for removing files, copying, concatenating, and comparing files, as well as scripts for displaying system information and performing calculations. Additionally, it provides examples of using pipes, redirection, and functions in shell scripting.

Uploaded by

rajmanisridhar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Ex.

No : 1
COMMANDS:

a)
rm (Remove) - Delete a file or directory tree

sh-4.4$ rma.txt

b)

cp – Copy a file or directory to another location

sh-4.4$ cpa.txt b.txt

c)

cat – Concatenate files to standard output

sh-4.4$ cat a.txt b.txt

sh-4.4$ cat a.txt b.txt >c.txt

d)

mv – Move or rename a file or directory

sh-4.4$ mva.txt b.txt

e)

cmp – Compare two files byte for byte

sh-4.4$ cmp a.txt b.txt

f)

wc – Word/line/byte count

#$wc – l File_Name - is used to calculate the no of lines in specific file

sh-4.4$ wc -l a.txt

2 aa.txt

#$wc – w File_Name - is used to calculate the no of wordsin specific file.


sh-4.4$ wc -w a.txt4 a.txt

#$wc – c File_Name - is used to calculate the no of characters in file.

sh-4.4$ wc -c a.txt

16 a.txt

#$wc –lwFile_Name - is used to calculate line and word for specific file.

sh-4.4$ wc -lc a.txt

sh-4.4$ wc -lc a.txt

#$wc –lcFile_Name - is used to calculate line and character for specific file.

sh-4.4$ wc -lw aa.txt

2 4 aa.txt

g)

split – Split a file into pieces

sh-4.4$ split -d a.txt

sh-4.4$ ls

x00 xaaa.txt

h)

diff – Compare two text files line by line

sh-4.4$ diff a.txt b.txt

2a3,5
Ex.No : 2

COMMANDS:

a) currently logged user and his log name

sh-4.4$ whoami

b) current shell , home directory , Operating System type , current path setting , current

working director

Current Shell

sh4.4$ echo $0

sh

Home Directory

sh4.4$ echo $HOME

/home/cg/root/users/desktop/a/·

Operating System type

sh4.4$ cat /etc/os-release

NAME=Fedora

VERSION="26 (Twenty Six)"

ID=fedora

VERSION_ID=26

PRETTY_NAME="Fedora 26 (Twenty Six)"

ANSI_COLOR="0;34"

CPE_NAME="cpe:/o:fedoraproject:fedora:26"
HOME_URL="https://fedoraproject.org/"

BUG_REPORT_URL="https://bugzilla.redhat.com/"

REDHAT_BUGZILLA_PRODUCT="Fedora"

REDHAT_BUGZILLA_PRODUCT_VERSION=26

REDHAT_SUPPORT_PRODUCT="Fedora"

REDHAT_SUPPORT_PRODUCT_VERSION=26

PRIVACY_POLICY_URL=https://fedoraproject.org/wiki/Legal:PrivacyPolicy

Current Path Settings:

sh4.4$ echo $PATH

/home/cg/root/a/

Current Working Directory:

sh4.4$ pwd

/home/cg/root

c) show currently logged number of users, show all available shells

sh4.4$ w

16:34:03 up 8 days, 22:48, 0 users, load average: 19.03, 21.40, 25.56

USER TTY

LOGIN@ IDLE JCPU PCPU WHATd) show CPU information like processor type ,

speed

sh-4.4$ less /proc/cpuinfo

processor:0
vendor_id: GenuineIntel

cpu family : 6

model: 63

model name : Intel(R) Xeon(R) CPU E5-1650 v3 @ 3.50GHz

stepping:2

microcode : 0x39

cpu Mhz : 3599.941

cache size : 15360 KB

physical id : 0

siblings: 12

core id: 0

cpu cores: 6

apicid : 0

initial apicid : 0

fpu : yes

fpu_exception : yes

cpuid level : 15

wp : yes

flags : fpuvme de psetscmsrpaemce cx8 apicsepmtrrpgemcacmov pat pse36

clflushdtsacpi mmx fxsrsse sse2 ssht tm pbesyscallnx pdpe1gb rdtscp lm

constant_tscar
ch_perfmonpebsbtsrep_goodnoplxtopologynonstop_tscaperfmperfeagerfpupnipclm

ulqdq dtes64 monitor ds_cplvmxsmxest tm2 ssse3 fma cx16 xtprpdcmpciddca

sse4_1 sse4_2 x2apic movbepopcnttsc_deadline_timeraesxsaveavx f16c

rdrandlahf_lmabmidaaratepbpln pts

dthermtpr_shadowvnmiflexpriorityeptvpidfsgsbasetsc_adjust bmi1 avx2 smep bmi2

erms invpcidcqmxsaveoptcqm_llccqm_occup_llc

bogomips : 6984.03

clflush size : 64

cache_alignment : 64

address sizes : 46 bits physical, 48 bits virtual

power management:

processor:1

vendor_id: GenuineIntel

cpu family:6

model: 63

model name: Intel(R) Xeon(R) CPU E5-1650 v3 @ 3.50GHz

stepping:2

microcode: 0x39e) show memory information

$ cat /proc/meminfo

Active: 12559184 kB

Active(anon): 9262084 kB

Inactive(anon): 308464 kB
Active(file): 3297100 kB

Inactive(file): 1225900 kB

Unevictable:0 kB

SwapTotal: 4194300 kB

SwapFree:

2898672 kB

Writeback:0 kB

AnonPages:9319384 kB

Shmem:174832 kB

SReclaimable: 4224068 kB

SUnreclaim: 2775868 kB

KernelStack: 111920 kB

PageTables:

123656 kB

NFS_Unstable: 0 kB

WritebackTmp:0 kB

CommitLimit: 136125132 kB

Committed_AS: 186068300 kB

VmallocTotal: 34359738367 kB

VmallocUsed: 747216 kB

VmallocChunk: 34358861532 kB

HardwareCorrupted: 0 kB
AnonHugePages: 3532800 kB

HugePages_Total: 0

HugePages_Free:0

HugePages_Rsvd:0

HugePages_Surp:0

Hugepagesize:2048 kB

DirectMap2M: 7020544 kB

DirectMap1G: 263192576 Kb

Inactive:1534364 kB

Mlocked:0 kB

Dirty:456

kBMapped:Slab:146324 kB 6999936 kB

Bounce:0 kB

DirectMap4k:
Ex.No 3

COMMANDS:

# Redirect stdout to a file

echo "Tamilnadu is a Wonderful place to Visit." > output.txt

# Create a file with some content

echo "Hello, World!" > input.txt

# Use a pipe to send the content of input.txt to a command and then redirect the

OUTPUT:

to another file

cat input.txt | tr '[:lower:]' '[:upper:]' > uppercase.txt

# Use the tee command to simultaneously display the output and save it to a file

cat input.txt | tee output.txt

# Append the date and time to the output file

date | tee -a output.txt


Ex.NO 4

SCRIPT CODE:

disp_menu()
{

echo "==========main menu=========="


echo "1.Display Current Data"
echo "2.Display User Name"
echo "3.List of Files in the Current Directory"
echo "4.List Directories in the Current Directory"
echo "5.Exit"
echo "============================="
}
display_date()
{
echo "Current Date:"
date;
}

display_user()
{
echo "Current Username:"
echo "USERNAME:$(whoami)"
}

list_files()
{
echo "File List in the Directory:"
find -type f
}
list_dir()
{
echo "Directories in the Current directory:"
find -type d
}
while true
do
disp_menu
read choice
case $choice in
1)
display_date;;
2)
display_user;;
3)
list_files;;
4)
list_dir;;
5)
echo "Existing the script, Thank you..!"
exit 0;;*)
echo "Invalid choice, Please try again..!"
esac
echo "press Enter to continue"
read enter_key
done

OUTPUT:
==========main menu==========
1.Display Current Data
2.Display User Name
3.List of Files in the Current Directory
4.List Directories in the Current Directory
5.Exit
=============================
3

File List in the Directory:


./atdc.java
./az.txt
./generics.java
./PPP.SH
./pro6.txt
./s.png
./test1.py
./ttt.py
press Enter to continue
Ex.No : 5

SCRIPT CODE:
#Function to Display Usage Information
usage()
{
echo "Usage: $0 <Command><Pattern>[File]"
echo "Available Commands: grep, sed, awk"
exit 0
}
#Check if the Correct No of Arguments is Provided
if [ $# -ne 3 ]
then
usage
fi
#Extract Arguments:

command="$1"
pattern="$2"
file="$3"

#Perform the filtering based command


case "$command" in
"grep")
grep "$pattern" "$file";;
"sed")
sed "$pattern" "$file";;
"awk")
awk "$pattern" "$file";;
*)
echo "Error..Invalid Command
Available Commands: grep, sed, awk"
esac

OUTPUT:

$ ./PPP.sh sed "s/ /@/g" az.txt


Hello@World
Thank@You@for@Your@Support
Ex.No :6

SCRIPT CODE:
#Set Directory Name
Directory=“/c/Users/ADMIN/Desktop”
#Check if file or folder and check size 0
find”$directory”-type f -size 0 -delete
#Delete Info
echo”zero byte file in $directory have been removed”;

OUTPUT:

$ ./Pro6.sh
zero byte file in /c/Users/ADMIN/Desktop/a/ have been removed
Ex.No : 7

SCRIPT CODE:
echo "enter a number:"
read number
sum=0
echo $digit
while [ $number -gt 0 ]
do
digit=$((number % 10))
sum=$((sum + digit))
number=$((number/10))
done
echo "The Sum of the Digit is:$sum"

OUTPUT:

Enter a Number:
1234
The Sum of the Digit is:10
Ex.NO : 8

SCRIPT CODE:
if [ "$#" -lt 2 ]; \
then
echo "Usage: $0 <number1><number2> ... <numberN>"
exit 1
fi
max=$1
for number in "$@"; do
if [ "$number" -gt "$max" ];
then
max=$number
fi
done
echo "The greatest number is: $max"

OUTPUT:
$ ./Pro8.sh 45 37 18 22 97 74
The greatest number is: 97
Ex.No : 9
SCRIPT CODE:
is_palindrome()
{
input=$(echo "$1" | tr -d ' ' | tr '[:upper:]' '[:lower:]')
reverse=$(echo "$input" | rev)
if [ $input = $reverse ]
then
echo "It is a Palindrome"
else
echo "It is not a Palindrome"
fi
}
if [ $# -eq 0 ]
then
echo "usage: $0 <word or phase>"
exit 1
fi
is_palindrome $1

OUTPUT:

$ ./PPP.sh amma
It is a Palindrome
Ex.No : 10
SCRIPT CODE:
if [ $# -eq 0 ]
then
echo "usage :$0<number>"
exit 1
fi
number=$1
for i in {1..100}
do
result=$((number*i))
echo "$number * $i = $result"
done

OUTPUT:

$ ./Pro.sh 5
1*5 =5
2 * 5 = 10
3 * 5 = 15
4 * 5 = 20
5 * 5 = 25
6 * 5 = 30
7 * 5 = 35
8 * 5 = 40
9 * 5 = 45
10 * 5 = 50
CONTENT

PAGE REMARK
Sl.No DATE TITLE OF THE SHELL SCRIPT
NO S
1 FILE COMMANDS

2 SYSTEM CONFIGURATION

3 PIPES, REDIRECTION AND TEE

4 FILE LISTING AND DIRECTORIES

5 FILTER COMMAND IMPLEMENTATION

6 REMOVE FILE BY SIZE 0

SUM OF INDIVIDUAL DIGITS FOR


7
GIVEN NUMBER
FIND GREATEST NUMBER USING
8
COMMAND LINE ARGUMENTS
9 PALINDROME CHECKING
MULTIPLICATION TABLE USING FOR
10
LOOP

You might also like