ADAMA SCIENCE AND TECHNOLOGY UNIVERSITY
SCHOOL OF ELECTRICAL ENGINEERING AND
COMPUTING
DEPARTMENT :SOFTWARE ENGINEERING
Programming Under Unix/Linux LAB(SEng3208 ) Assignment-1 & 2
Section: 3
Name ID_Number
1. Amanuel Assefa ………………………………..ugr/25354/14
2. Dejene Samuel………………………………….ugr/25459/14
3. Debela Berako ………………………………….ugr/25581/14
4. Eliyas Siyum…………………………………….ugr/25385/14
5. Hana Kebede…………………………………….ugr/25754/14
Submitted to: Dr.T.Gopi Krishna
Submission Date: December 10, 2024
PART-I
1) Write the linux command to display calendar with various options
cal -y
-Display the calendar for the current year.
cal -m
-Display the calendar with Monday as the first day of the week.
cal 12 2024
-Display the calendar for December 2024.
2) Write a linux command to display dates with various options.
date
-Display the current date and time.
date +"%A, %d-%B-%Y"
-Custom format (e.g., Monday, 08-December-2024).
3) Write a linux command to display the list of users who are currently using linux server.
who
-List all logged-in users.
w
-Show users along with their activity.
4) Write a linux command to display your system details.
uname -a
-Display detailed system information.
hostnamectl
- Display system detailed system and OS information
5) Write a linux command to display your user name.
whoami
- Display the current username.
6) Write a linux command to list all the directories and files on the server.
ls -LaR /
- List all files and directories, including hidden ones, from the root.
7) Write the linux command to display the content of a file
cat filename
e.g. cat file.txt
- Display the content of the file.
less filename
e.g. less doc.txt
- View file content page by page.
more filename
- Another pager to view file content.
8) Write the linux command to print the content on a standard output device.
echo "Hello, World!"
- Print a string to standard output.
cat filename - Print the content of a file to standard output.
9) Write the linux command to perform calculations.
expr 5 + 3 - Perform basic arithmetic operations.
bc - Open a calculator utility for more complex calculations.
.
10) Write the linux command to show the current working directory.
pwd - Print the current working directory.
11) Write the linux command to get help with various options.
man command_name - Display the manual page for a command.
command_name --help - Display help information for a specific command.
12) Write the linux command to display what all users are currently doing
w - Show a summary of logged-in users and their activity.
13) Write the linux command to create a directory.
mkdir directory_name - Create a new directory.
14) Write the linux command to change the directory
cd directory_name - Change to the specified directory.
15) Write the linux command to remove a directory
rmdir directory_name - Remove an empty directory.
rm -r directory_name - Remove a directory and its contents.
PART-II
16 Write the linux command to delete a file.
rm filename -Delete the specified file.
17) Write the linux Command to copy a file to some other location.
cp source_file target_location
● Copy the file to the specified location.
18) Write the linux command to move a file to some different location.
mv source_file target_location
● Move (or rename) the file to the new location.
19) Write the linux command to compare the contents of two files.
cmp file1 file2
● Compare the two files byte by byte.
20) Write the linux command to show the difference between the contents of two files.
diff file1 file2
● Show line-by-line differences between two files.
21) Write the linux command to show the common data of two files.
comm file1 file2
● Display common lines between two sorted files.
22 ) Write the linux command to count the number of words, lines and sentences in the file.
wc filename
-Display the count of lines, words, and characters.
wc -l filename-Count only lines.
wc -w filename-Count only words.
23) Write the linux command to see the data in octal format.
od -b filename
-Display file content in octal format.
24) Write the linux command to give the alias name.
alias alias_name='command'
Create an alias for a command.
25) Write the linux command to view the existing aliases.
Alias
If we want to see the specific aliases, we can use:
alias | grep "alias"
26) Write the linux command to unalias the existing alias name.
unalias alias_name - Remove the specified alias.
unalias -a Remove all aliases.
27) Write the linux command to display the hostname of the system.
Hostname - Display the system's hostname.
28) Write the linux command to get information about the operating System.
uname -a - Display system information.
cat /etc/os-release –Show details about the OS.
29) Write the linux command to check whether a command is Internal or external.
type command_name —>Show if the command is built-in (internal) or external.
PART-III
30) Write the linux command to display the unique contents of a file.
sort filename | uniq →Display unique lines from the file.
31)Write the linux command to view the first 5 lines of a file.
head -n 5 filename →Display the first 5 lines of the file.
32) Write the linux command to view the last 20 lines of a file.
tail -n 20 filename → Display the last 20 lines of the file.
33) Write the linux command to extract specific column and field from a file
cut -d 'delimiter' -f column_number filename
Example: cut -d ',' -f 2 file.csv (Extract the 2nd column from a CSV file).
34) Write the linux command to merge two files vertically.
cat file1 file2 > merged_file → Combine the contents of file1 and file2 vertically.
35) Write the linux command to sort a file.
sort filename —>Sort the file alphabetically or numerically.
36) Write the linux command to find any character and replace it with some other character.
tr 'old_char' 'new_char' < input_file > output_file
Example: tr 'a' 'b' < file.txt > new_file.txt
37) Write the linux command to check the default permission of a file
umask —>Display the default permission mask.
38) Write the linux command to chan:e the default permission of a file
umask new_mask
Example: umask 022 (Sets default permissions for new files to 755).
39) Write the linux command to change the permission of an existing file in relative manner
chmod u+x filename - Add execute permission for the user.
chmod g-w filename - Remove write permission for the group.
chmod o+r filename - Add read permission for others.
40) Write the linux command to change the permission of an existing file in absolute manner
chmod 755 filename - Set permissions to rwxr-xr-x.
chmod 644 filename - Set permissions to rw-r--r--.
41) Write the linux command to create a hard link of a file
ln existing_file hard_link_name
42)Write the linux command to create a soft link of a file as well as directory
ln -s existing_file soft_link_name - Create a soft link for a file.
ln -s existing_directory soft_link_name - Create a soft link for a directory.
43)Write the linux command to search for a specific pattern in a file.
grep "pattern" filename
e.g grep 'hello' file.txt
To search for the pattern in all files in a directory and its subdirectories:
grep -r 'pattern' /path/to/directory
To count the number of lines that match the pattern:
grep -c 'example' file.txt
44) Write the linux command to show the use of Basic Regular Expressions using grep
Command.
grep '^pattern' filename - Match lines starting with "pattern".
grep 'pattern$' filename - Match lines ending with "pattern".
grep 'pa.ttern' filename -Match Multiple Characters:
grep '[cbh]at' file.txt
This matches "cat", "bat", or "hat". - Match lines containing "pa" followed by any character and
then "ttern".
45)Write the linux command to show the use of Extended Regular Expressions using grep
Command.
grep -E 'pattern1|pattern2' filename - Match lines containing either pattern1 or
pattern2.
grep -E 'pattern[0-9]+' filename - Match lines with "pattern" followed by one or
more digits.
46) Write the linux command to print the result of a specific pattern.
grep -o "pattern" filename - Print only the matching pattern from the file.
47) Write the linux command to copy the specified pattern of one file in another file using sed
Command.
sed -n '/pattern/p' source_file > target_file
48) Write the linux command to copy the data except specified pattern of one file in another file
using sed Command
sed '/pattern/d' source_file > target_file
49) Write the linux command to replace any character/string with specified character/string
using sed Command
sed 's/old_string/new_string/g' filename - Example: sed 's/foo/bar/g' file.txt
PART-V
51) Write the linux command to show the use of awk Command
awk '{print $1, $3}' filename - Print the 1st and 3rd columns from a file.
awk '/pattern/ {print $0}' filename - Print lines containing a specific pattern.
awk 'BEGIN {sum=0} {sum+=$1} END {print sum}' filename - Calculate the sum of
the first column.
52) Write the linux command to list the processes for the current shell(ps).
ps - List processes for the current shell.
ps -f - Show detailed information about processes in the current shell.
53) Write the linux command to display detailed information about processes.
ps aux - Show all running processes with detailed information.
top - Display processes dynamically with resource usage.
54) Write the linux command to stop any currently running process
Ctrl + Z - Pause the currently running process.
kill -STOP pid - Stop a process with a specific process ID.
55) Write the linux command to run the process in the background
command & - Run the process in the background.
bg - Resume a paused process in the background.
56) Write the linux command to Kill a process
Write a shell script to find the largest of two numbers
echo "Enter two numbers:"
read a b
if [ $a -gt $b ]; then
echo "$a is larger"
else
echo "$b is larger"
fi
57) Write the linux command to create a tar file
tar -cvf archive_name.tar file1 file2 directory/ - Create a tar archive.
58) Write the linux command to create a Zip file and Unzip it
zip archive_name.zip file1 file2 directory/ - Create a zip archive.
unzip archive_name.zip - Extract the zip file.
59) Write the linux command to create a GZip file and Unzip it
gzip filename - Compress a file into `.gz` format.
gunzip filename.gz - Decompress the GZip file.
60)How to save and execute a file in VI
1. Open the file in vi editor
vi filename
2. Enter insert mode
Press i to edit the file.
3. Write the script/code
Add your desired content.
4. Save and exit
Press Esc, then type :wq and press Enter to save and exit.
5. Make the file executable
chmod +x filename
6. Execute the file
./filename
ASSIGNMENT-2 SHELL PROGRAMMING
PART-I
61) Write a shell script to print all the users who have currently logged in
who
62)Write a shell script to display only the size of files from the current working directory
ls -lh | awk '{print $5, $9}'
63)Write a shell script to create a directory and files and subdirectories
mkdir -p parent_directory/subdirectory
touch parent_directory/file1.txt parent_directory/subdirectory/file2.txt
64)Write a shell script to find the largest of two numbers
echo "Enter two numbers:"
read a b
if [ $a -gt $b ]; then
echo "$a is larger"
else
echo "$b is larger"
fi
65) Write a shell script to check whether a number is even or odd
echo "Enter a number:"
read num
if [ $((num % 2)) -eq 0 ]; then
echo "$num is even"
else
echo "$num is odd"
fi
66)Write a shell script to print the values of Environment variables
#!/bin/bash
echo "Environment Variables:"
echo "-----------------------"
Printenv
67) Write a shell script to extract certain values from Environment variable PATH
echo $PATH | tr ':' '\n'
68)Write a shell script to find the largest of three given numbers.
echo "Enter three numbers:"
read a b c
if [ $a -ge $b ] && [ $a -ge $c ]; then
echo "$a is the largest"
elif [ $b -ge $a ] && [ $b -ge $c ]; then
echo "$b is the largest"
else
echo "$c is the largest"
fi
69)Write a shell script to greet the user according to the week of the day.
day=$(date +%A)
echo "Happy $day! Have a great day!"
PART-II
70)Write a shell script to display the number of vowels and consonants in a string
echo "Enter a string:"
read str
vowels=$(echo "$str" | grep -o -i '[aeiou]' | wc -l)
consonants=$(echo "$str" | grep -o -i '[bcdfghjklmnpqrstvwxyz]' | wc -l)
echo "Vowels: $vowels"
echo "Consonants: $consonants"
71) Write a shell script to perform arithmetic operation using case—esac.
echo "Enter two numbers:"
read a b
echo "Choose operation: 1) Add 2) Subtract 3) Multiply 4) Divide"
read choice
case $choice in
1) echo "Sum: $((a + b))" ;;
2) echo "Difference: $((a - b))" ;;
3) echo "Product: $((a * b))" ;;
4) echo "Quotient: $((a / b))" ;;
*) echo "Invalid choice" ;;
esac
72)Write a shell script to count words, lines and characters in a file
echo "Enter filename:"
read filename
if [ -f "$filename" ]; then
echo "Lines: $(wc -l < $filename)"
echo "Words: $(wc -w < $filename)"
echo "Characters: $(wc -m < $filename)"
else
echo "File not found!"
fi
73) Write a shell script to find out which shell you are working in.
echo "You are using: $SHELL"
74) Write a shell script to show the output of who command sorted on 3rd column
who | sort -k3
75)Write a shell script to display the number of daemon processes.
ps -eo stat,cmd | grep '^D' | wc -l
76) Write a shell script to check whether the year entered is leap or not.
echo "Enter a year:"
read year
if (( (year % 4 == 0 && year % 100 != 0) || year % 400 == 0 )); then
echo "$year is a leap year."
else
echo "$year is not a leap year."
fi
77) Write a shell script to find the factorial of a number.
echo "Enter a number:"
read num
factorial=1
for (( i=1; i<=num; i++ )); do
factorial=$((factorial * i))
done
echo "Factorial of $num is $factorial"
78) Write a shell script to generate a multiplication table
echo "Enter a number:"
read num
for (( i=1; i<=10; i++ )); do
echo "$num x $i = $((num * i))"
done
79) Write a shell script to add the digits of a number
echo "Enter a number:"
read num
sum=0
while [ $num -gt 0 ]; do
digit=$((num % 10))
sum=$((sum + digit))
num=$((num / 10))
done
echo "Sum of digits: $sum"
80)Write a shell script to reverse a string.
echo "Enter a string:"
read str
echo "$str" | rev
81)Write a shell script to check whether a number entered is prime or not.
echo "Enter a number:"
read num
is_prime=1
for (( i=2; i*i<=num; i++ )); do
if (( num % i == 0 )); then
is_prime=0
break
fi
done
if (( is_prime && num > 1 )); then
echo "$num is a prime number."
else
echo "$num is not a prime number."
fi
82)Write a shell script to accept a filename as command line argument and print its contents
if [ -f "$1" ]; then
cat "$1"
else
echo "File not found!"
fi
PART-III
83)Write a shell script to generate Fibonacci Series.
echo "Enter the number of terms:"
read n
a=0
b=1
echo "Fibonacci Series:"
for (( i=0; i<n; i++ )); do
echo -n "$a "
temp=$((a + b))
a=$b
b=$temp
done
echo
84)Write a shell script to check whether a given number is a palindrome
echo "Enter a number:"
read num
original=$num
reverse=0
while [ $num -gt 0 ]; do
digit=$((num % 10))
reverse=$((reverse * 10 + digit))
num=$((num / 10))
done
if [ $original -eq $reverse ]; then
echo "$original is a palindrome."
else
echo "$original is not a palindrome."
fi
85) Write a shell script to copy the contents of one file to another file in a separate directory.
echo "Enter the source file:"
read src
echo "Enter the destination directory:"
read dest
if [ -f "$src" ] && [ -d "$dest" ]; then
cp "$src" "$dest"
echo "File copied successfully to $dest"
else
echo "Source file or destination directory does not exist!"
fi
86) Write the linux command to display a list of CPU-intensive tasks on the system.
top
87) Write the linux command to change process priority.
renice -n <priority> -p <process_id>
-Replace `<priority>` with a value between `-20` (highest priority) and `19` (lowest
priority), and `<process_id>` with the PID of the process.
88)Write the linux command to display the processes on the system in the form of a tree.
pstree