Practical program Exercise
1. Linux Basic commands
File and Directory Management
Command Description
ls List files and directories
pwd Show current directory path
cd Change directory
mkdir Create a new directory
rmdir Remove an empty directory
rm -r Remove a directory and its contents
cp Copy files and directories
mv Move or rename a file/directory
find Search for files in a directory
touch Create a new empty file
File Operations
Command Description
cat Display file contents
less View file contents page by page
head Display the first 10 lines of a file
tail Display the last 10 lines of a file
grep Search for text within files
wc Count lines, words, and characters in a
file
cut Extract specific columns from a file
sort Sort the lines of a file
diff Compare two files line by line
echo Print text to the terminal or a file
User and Permission Management
Command Description
whoami Show current logged-in user
id Show user ID (UID) and group ID (GID)
chmod Change file permissions
chown Change file owner
groups Show groups a user belongs to
passwd Change user password
useradd Add a new user
usermod Modify user information
userdel Delete a user
su Switch to another user
sudo Run commands with superuser
privileges
2. File and Directory Management
Problem:
Perform the following operations using Linux commands:
● Create a new directory named my_folder.
● List all files (including hidden ones) in the current directory.
● Remove a directory named old_folder along with all its contents.
● Rename a file from old_file.txt to new_file.txt.
● Copy file1.txt to the backup/ directory.
Solution:
mkdir my_folder # Create a directory named my_folder
ls -a # List all files, including hidden ones
rm -r old_folder # Remove a directory and all its contents
mv old_file.txt new_file.txt # Rename old_file.txt to new_file.txt
cp file1.txt backup/ # Copy file1.txt to backup/ directory
3. File Operations
Problem:
Using Linux commands, complete the following file operations:
● Display the contents of a file named notes.txt.
● Show only the first 10 lines of a file log.txt.
● Append the text "Hello, World!" to an existing file data.txt.
● Find the word "error" inside a file report.log.
● Count the number of lines in a file data.csv.
Solution:
cat notes.txt # Display file contents
head -10 log.txt # Show first 10 lines of log.txt
echo "Hello, World!" >> data.txt # Append text to data.txt
grep "error" report.log # Search for "error" in report.log
wc -l data.csv # Count lines in data.csv
4. Program to find the given number is ODD or EVEN.
<?php
$num = 7;
if ($num % 2 == 0) {
echo "$num is Even";
} else {
echo "$num is Odd";
}
?>
5. If-Else Statement: Check if a Number is Positive, Negative, or Zero
<?php
$num = -5;
if ($num > 0) {
echo "$num is Positive";
} elseif ($num < 0) {
echo "$num is Negative";
} else {
echo "$num is Zero";
}
?>
6. (a) For Loop: Print Even Numbers from 1 to 20
<?php
for ($i = 2; $i <= 20; $i += 2) {
echo "$i ";
}
?>
6. (b) While Loop: Sum of First 10 Natural Numbers
<?php
$sum = 0;
$i = 1;
while ($i <= 10) {
$sum += $i;
$i++;
}
echo "Sum of first 10 natural numbers is: $sum";
?>
6. (c).Do-While Loop: Print Multiplication Table of 5
<?php
$num = 5;
$i = 1;
do {
echo "$num x $i = " . ($num * $i) . "<br>";
$i++;
} while ($i <= 10);
?>
7.(a) One-Dimensional Array: Store and Display 5 Student Names
<?php
$students = array("John", "Alice", "Mike", "Sara", "Tom");
foreach ($students as $student) {
echo "$student <br>";
}
?>
7. (b) Associative Array: Student Marks
<?php
$marks = array("John" => 85, "Alice" => 90, "Mike" => 78);
foreach ($marks as $name => $score) {
echo "$name scored $score marks.<br>";
}
?>
7. (c) Multidimensional Array: Student Details
<?php
$students = array(
array("John", 85, "A"),
array("Alice", 90, "A+"),
array("Mike", 78, "B")
);
foreach ($students as $student) {
echo "Name: " . $student[0] . ", Marks: " . $student[1] . ", Grade: " .
$student[2] . "<br>";
}
?>
8. Sorting an Array
<?php
$numbers = array(5, 2, 8, 1, 3);
sort($numbers);
echo "Sorted numbers: ";
foreach ($numbers as $num) {
echo "$num ";
}
?>
9. Find Maximum and Minimum in an Array
<?php
$numbers = array(10, 20, 5, 40, 30);
$max = max($numbers);
$min = min($numbers);
echo "Maximum value: $max <br>";
echo "Minimum value: $min";
?>