LINUX LAB assignment 2 MILAN VAS
LINUX LAB assignment 2 MILAN VAS
boot block, super block, inode block, and data block. Create a directory
structure and demonstrate the use of basic and advanced file commands
(such as mkdir, rmdir, ls, cp, mv, rm) to manipulate the file system. Include
to as a directory tree. The top of the hierarchy is called the root directory,
denoted by a slash (/). Below the root directory, there are several important
1) Boot Block:- The boot block is the first block of the storage device. It
contains the boot loader, which is responsible for loading the operating
2) Super Block:- The super block contains metadata about the file system,
such as its size, block size, number of free blocks, and information about
the inode table. It is crucial for the file system's integrity and operation.
3) Inode Block:- Inodes are data structures that store information about files
The inode contains pointers to these data blocks. Directory Structure and
mkdir -p /home/krishcity113/myproject/{src,bin,docs}
Code
/home
└── krishcity113
└── myproject
├── src
├── bin
└── docs
ls -l /home/krishcity113/myproject
This command lists the contents of the myproject directory in long format,
cp /home/krishcity113/original.txt /home/krishcity113/myproject/docs/
mv /home/krishcity113/myproject/docs/original.txt
/home/krishcity113/myproject/docs/renamed.txt
rm /home/krishcity113/myproject/docs/renamed.txt
rmdir /home/krishcity113/myproject/bin
This command removes the bin directory (only works if the directory is
empty).
$ mkdir -p /home/krishcity113/myproject/{src,bin,docs}
$ ls -l /home/krishcity113/myproject
total 12
Copy File:-
$ cp /home/krishcity113/original.txt /home/krishcity113/myproject/docs/
$ mv /home/krishcity113/myproject/docs/original.txt
/home/krishcity113/myproject/docs/renamed.txt
Remove File:-
$ rm /home/krishcity113/myproject/docs/renamed.txt
Remove Directory:-
$ rmdir /home/krishcity113/myproject/bin
Screenshot (Conceptual)
Since I cannot provide actual screenshots, below is a conceptual representation
of the command outputs:
Code
total 12
Summary
mechanism in Linux and how cron, at, and batch are used to manage
scheduler decides which process runs at any given time, based on process
deterministic execution.
immediate execution.
intervals.
batch:- batch is used to schedule tasks to run when the system load is low.
It is useful for non-urgent tasks that can wait until the system is less busy.
Let's create a scheduled task that runs a specific script at regular intervals
1. Create a Script
For example:-
let's create a script that logs the current date and time to a file.
# /home/krishcity113/log_date.sh
#!/bin/bash
chmod +x /home/krishcity113/log_date.sh
* * * * * /home/krishcity113/log_date.sh
Save and close the editor. This cron job will run the log_date.sh script every
minute.
crontab -l
Code
* * * * * /home/krishcity113/log_date.sh
Crontab Editor:-
Code
$ crontab -e
* * * * * /home/krishcity113/log_date.sh
Code
$ crontab -l
* * * * * /home/krishcity113/log_date.sh
Q-3 Explore networking tools in Linux by using command such as ping,
nslookup, telnet, arp, netstat, route, Ōp, & rlogin. Provide examples of each
and management.
Answer:-
1. ping:- The ping command is used to check the connectivity between your
Example:-nslookup google.com
the Telnet protocol. It's often used to test connectivity to TCP ports.
Example:- telnet google.com 80
(HTTP). It's useful for checking if a specific port is open and accessible.
4. arp:- The arp command is used to display and manipulate the ARP
Example:- arp -a
Explanation:- This command displays all listening TCP and UDP ports with
numerical addresses. It's useful for checking which ports are open and
listening on a system.
routing table.
Example:- route -n
numerical addresses. It's useful for viewing and managing the routing table.
Example:- ip a
addresses.
rlogin protocol.
for accessing remote systems, though it's less common now due to security
disk usage, clear temporary files, and generate a report. Provide the script
Answer:-
#!/bin/bash
LOGFILE="/var/log/system_maintenance.log"
check_disk_usage()
df -h | tee -a $LOGFILE
clear_temp_files() {
TEMP_DIR="/tmp"
generate_report()
check_disk_usage
clear_temp_files
if [ ! -f $LOGFILE ]; then
touch $LOGFILE
fi
generate_report
echo "System maintenance completed." | tee -a $LOGFILE
Explanation:-
This line indicates that the script should be run using the Bash shell.
This variable defines the path to the log file where the report will be stored.
check_disk_usage()
df -h | tee -a $LOGFILE
This function uses the df -h command to check disk usage and appends the
clear_temp_files()
{
TEMP_DIR="/tmp"
This function clears all files in the /tmp directory and logs the action.
generate_report()
check_disk_usage
clear_temp_files
if [ ! -f $LOGFILE ]; then
touch $LOGFILE
fi
generate_report
This section starts the system maintenance, checks if the log file exists
function.
Usage:-
chmod +x maintenance.sh
Run the script with superuser privileges to ensure it can access and modify
necessary files:
sudo ./maintenance.sh
This script will check disk usage, clear temporary files, and generate a
Q-5 Write a shell script that accepts command-line arguments and performs
opons for file manipulaon (e.g., copy, move, delete) and display appropriate
messages for each operaon. Provide the script and examples of its
execuon.
Answer:-
#!/bin/bash
usage()
exit 1
if [ $# -lt 2 ]; then
usage
fi
# Get the operation and source file from the command-line arguments
operation=$1
source=$2
destination=$3
case $operation in
copy)
if [ -z "$destination" ]; then
usage
fi
cp "$source" "$destination"
if [ $? -eq 0 ]; then
else
fi
;;
move)
if [ -z "$destination" ]; then
usage
fi
mv "$source" "$destination"
if [ $? -eq 0 ]; then
else
fi
;;
delete)
rm -f "$source"
if [ $? -eq 0 ]; then
else
fi
;;
*)
usage
;;
Esac
Explanation:-
This line indicates that the script should be run using the Bash shell.
Usage Function:-
usage()
exit 1
This function displays the correct usage of the script and exits with a status
code of 1.
Argument Check:-
if [ $# -lt 2 ]; then
usage
Fi
This checks if at least two arguments are provided. If not, it calls the usage
function.
Retrieve Arguments:-
operation=$1
source=$2
destination=$3
This retrieves the operation (e.g., copy, move, delete), source file, and
Perform Operation:-
The script uses a case statement to perform the requested operation based
Copy Operation:-
cp "$source" "$destination"
if [ $? -eq 0 ]; then
else
Fi
Move Operation:-
mv "$source" "$destination"
if [ $? -eq 0 ]; then
else
Fi
Delete Operation:-
rm -f "$source"
if [ $? -eq 0 ]; then
else
Fi
Invalid Operation:-
Usage:-
Usage Examples:-
Copy a File:-
./file_ops.sh copy /path/to/source.txt /path/to/destination.txt
Output:-
Code
Move a File:-
Output:-
Code
Delete a File:-
Output:-
Code
Invalid Usage:-
./file_ops.sh copy /path/to/source.txt
Output:-
Code
Save the script to a file named file_ops.sh, make it executable, and run it