0% found this document useful (0 votes)
48 views20 pages

Linux Lab Practical

The document provides information about several practical Linux sessions: 1. Getting familiar with Linux login/logout commands and the vi editor. 2. Developing simple shell programs using commands like ls, cd, mkdir. 3. Using the zip and unzip commands to compress and uncompress files. 4. Demonstrating conditional statements like if/else in shell scripts. 5. Explaining control statements like break, continue, and switch. 6. Demonstrating commands of the stream editor sed for text manipulation.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
48 views20 pages

Linux Lab Practical

The document provides information about several practical Linux sessions: 1. Getting familiar with Linux login/logout commands and the vi editor. 2. Developing simple shell programs using commands like ls, cd, mkdir. 3. Using the zip and unzip commands to compress and uncompress files. 4. Demonstrating conditional statements like if/else in shell scripts. 5. Explaining control statements like break, continue, and switch. 6. Demonstrating commands of the stream editor sed for text manipulation.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 20

Practical Session 1.

To get familiarized with Linux Log In/Log Out and various other commands & vi
editor.

Linux - Linux is an open-source operating system renowned for its stability, security, and flexibility. It
powers diverse devices, from servers to smartphones. With a robust command-line interface, it offers a
wide range of free software, fostering a vibrant community and supporting various computing needs
worldwide.

Logging In and Out

Logging into a Linux system is typically done through a terminal. When you boot up a Linux system,
you'll be prompted with a login screen. You enter your username and password to log in.

To log in via the terminal, you can use the ssh command: ssh username@your_server_ip

To log out of a terminal session, you can use the exit command or logout command.

Various Commands

Here are some commonly used commands in Linux:

ls: List files and directories.

cd: Change directory.

pwd: Print the current working directory.

mkdir: Create a new directory.

rm: Remove files or directories.

cp: Copy files or directories.

mv: Move or rename files or directories.

cat: Display the content of a file.

grep: Search for specific patterns in files.

chmod: Change file permissions.

sudo: Execute commands with administrative privileges.

Vi Editor
Vi is a versatile text editor present in Unix and Linux systems. Notable for its two modes, command and
insert, Vi offers powerful features for efficient text editing. Its commands, including search, replace, and
navigation, make it a favorite among experienced users, although it has a steep learning curve for
beginners. To open a file using vi: vi filename

Common vi commands:

i to enter insert mode and start typing.

Esc to exit insert mode.

:w to save the changes.

:q to quit vi.

:wq to save and quit.


Practical Session 2. Develop simple shell programs using Bash or any other shell in Linux.

Shell programming involves writing scripts or programs using shell commands. Shells, like Bash or sh,
are interfaces between users and the operating system, interpreting commands and executing them.
Shell scripts contain sequences of commands, control structures, and functions, enabling automation,
system administration, and repetitive task handling in a Linux-based environment.

Here are some fundamental shell commands commonly used in Linux operating systems:

ls - List files and directories.

cd - Change directory.

pwd - Print the current working directory.

mkdir - Create a new directory.

rm - Remove files or directories.

cp - Copy files or directories.

mv - Move or rename files or directories.

cat - Display the content of a file.

grep - Search for specific patterns in files.

chmod - Change file permissions.

sudo - Execute commands with administrative privileges.

Here are the five basic shell programs:

Hello World:

#!/bin/bash

echo "Hello, World!"

Simple Calculator (Addition):

#!/bin/bash

echo "Enter two numbers:"

read num1

read num2
sum=$((num1 + num2))

echo "Sum is: $sum"

File Backup:

#!/bin/bash

echo "Enter file to backup:"

read filename

cp "$filename" "$filename.backup"

echo "Backup created: $filename.backup"

List Files in Directory:

#!/bin/bash

echo "Contents of the current directory:"

ls

Simple Calculator:

#!/bin/bash

echo "Welcome to the Basic Calculator"

echo "Enter two numbers:"

read num1

read num2

echo "Select an operation: "

echo "1. Addition (+)"

echo "2. Subtraction (-)"

echo "3. Multiplication ()"

echo "4. Division (/)"

read choice

case $choice in
1)

result=$((num1 + num2))

operator="+"

;;

2)

result=$((num1 - num2))

operator="-"

;;

3)

result=$((num1 * num2))

operator=""

;;

4)

result=$(awk "BEGIN {printf "%.2f", $num1 / $num2}")

operator="/"

;;

*)

echo "Invalid choice"

exit 1

;;

esac

echo "Result: $num1 $operator $num2 = $result"

Practical Session 3. Using Zip and Unzip commands in linux


The zip command in Linux compresses files or directories into a ZIP archive, reducing their size for easier
storage or transfer. It's used with zip filename.zip file/directory. Conversely, unzip decompresses files
from a ZIP archive with unzip filename.zip, restoring the original content. These commands help manage
and compress data efficiently in a compressed format.

Example of using zip:

To compress a file or directory into a ZIP archive:

zip -r archive.zip directory/

Example of using unzip:

To extract files from a ZIP archive:

unzip archive.zip

Additional options and flags can be used with both zip and unzip commands to specify more detailed
operations, file names, or directories.

The zip and unzip commands offer various options in Linux:

zip command options:

-r: Recursively compresses directories.

-q: Quiet mode, suppresses normal output.

-9: Sets maximum compression level.

-e: Encrypts the archive.

-u: Updates existing archive with new files.

unzip command options:

-l: Lists content of the ZIP file.

-d: Extracts files to a specific directory.

-q: Quiet mode, suppresses informational messages.


-o: Overwrites files without prompting.

-P: Provides a password to extract encrypted files.


Practical Session 4. Demonstrate conditional statements using shell scripting in Linux.

The conditional statements allow you to execute different commands based on the evaluation of specific
conditions in a shell script. We can Adjust the conditions and actions based on our specific
requirements.

IF Condition: if [ condition ]; then

# Statements to execute if the condition is true

Fi

Example: #!/bin/bash

# Check if a number is greater than 10

num=15

if [ $num -gt 10 ]; then

echo "The number is greater than 10."

fi

IF-ELSE Condition: if [ condition ]; then

# Statements to execute if the condition is true

else

# Statements to execute if the condition is false

Fi

Example: #!/bin/bash

# Check if the number is greater than 5

num=7

if [ $num -gt 5 ]; then

echo "The number is greater than 5."

else

echo "The number is less than or equal to 5."


fi

IF-ELSEIF-ELSE Condition : if [ condition1 ]; then

# Statements to execute if condition1 is true

elif [ condition2 ]; then

# Statements to execute if condition2 is true

else

# Statements to execute if both condition1 and condition2 are false

fi

Example: #!/bin/bash

# Check if the number is positive, negative, or zero

num=-3

if [ $num -gt 0 ]; then

echo "The number is positive."

elif [ $num -lt 0 ]; then

echo "The number is negative."

else

echo "The number is zero."

fi
Practical Session 4. Demonstrate control statements using shell scripting in Linux.

Write down about break continue and switch statements


Practical Session 5. Demonstrate and perform different commands of stream editor in Linux/Unix.

Objective: The objective of this practical session is to demonstrate and execute various
commands associated with the Stream Editor ( sed) in a Linux/Unix environment. The focus is
on familiarizing users with the sed utility and its versatile commands for text manipulation,
editing, and stream processing.

Equipment/Tools Required:

1. Linux/Unix operating system


2. Terminal or command-line interface

Procedure:

1. Initialization:
 Ensure the Linux/Unix system is powered on and accessible through the terminal or
command-line interface.
2. Accessing sed:
 Open the terminal window or access the command-line interface.
 Use the sed command followed by appropriate options and expressions to execute text
manipulation operations. For instance:
arduino
sed 's/old_pattern/new_pattern/' filename.txt
This command substitutes the first occurrence of 'old_pattern' with 'new_pattern' in the
filename.txt file.
3. Basic sed Commands:
 s: Substitution command to replace text.
 p: Print command to display selected lines.
 d: Delete command to remove specific lines.
 g: Global replacement command to replace all occurrences on a line.
 w: Write command to save selected lines to a file.
 q: Quit command to exit sed after processing specified commands.
4. Usage Examples:
 Substitute text:
arduino
sed 's/apple/orange/' fruits.txt
This command substitutes the first occurrence of 'apple' with 'orange' in the fruits.txt
file.
 Print specific lines:
kotlin
sed -n '5,10p' data .txt
This command prints lines 5 to 10 from the data.txt file.
 Delete lines matching a pattern:
javascript
sed '/pattern/d' document . txt
This command deletes all lines containing the specified 'pattern' from the document.txt
file.
5. Advanced Operations:
 Utilize regular expressions and address ranges to perform complex text manipulation
tasks.
 Experiment with combining multiple sed commands or using flags for more intricate text
processing.
6. Save and Exit:
 Redirect output or use appropriate options to save changes made by sed commands to the
original or new files as required.
7. Experimentation:
 Practice various sed commands, explore their combinations, and apply them to different
files to grasp the breadth of sed functionalities for text editing and manipulation.
8. Conclusion:
 Summarize the practical experience gained from utilizing sed for text processing and
stream editing tasks in Linux/Unix. Reflect on the significance of sed commands and
their applications in stream editing operations.
Practical Session 7. Develop advanced shell programs using grep & egrep

Objective: The aim of this practical session is to create advanced shell programs utilizing grep
and egrep commands in a Linux/Unix environment. This exercise focuses on leveraging the
capabilities of grep (Global Regular Expression Print) and egrep (Extended Global Regular
Expression Print) for text searching, pattern matching, and filtering within shell scripts.

Equipment/Tools Required:

1. Linux/Unix operating system


2. Terminal or command-line interface

Procedure:

1. Initialization:
 Ensure the Linux/Unix system is accessible via the terminal or command-line interface.
2. Understanding grep and egrep:
 grep and egrep are command-line tools used for searching patterns within files or
streams of text.
 grep searches using Basic Regular Expressions (BRE), while egrep extends this
capability to support Extended Regular Expressions (ERE).
3. Basic Usage:
 grep command syntax:
perl
grep 'pattern' filename
 egrep command syntax:
arduino
egrep 'pattern' filename
4. Development of Advanced Shell Programs:
 Create shell scripts that utilize grep and egrep commands in combination with other
shell utilities to perform specific tasks, such as:
 Log File Analysis: Develop a script that utilizes grep to extract specific
information from log files based on defined patterns or keywords.
 Data Extraction and Filtering: Create a script that utilizes egrep to filter and
extract specific data from text-based files or streams.
 Pattern Matching and Reporting: Develop a script that employs grep and
egrep to search for complex patterns within files and generate reports based on
the matched patterns.
5. Advanced grep and egrep Commands:
 Explore advanced options and flags available with grep and egrep, such as:
 -i: Ignore case distinctions in patterns.
 -v: Invert the match to select non-matching lines.
 -o: Display only the matched parts of a line.
 -E: Use Extended Regular Expressions (ERE) with grep.
 -r: Recursively search directories.
 Experiment with combining multiple options to enhance the functionality of shell
programs.
6. Testing and Execution:
 Test the developed shell programs on various files or data sets to ensure their
functionality and accuracy in performing the intended tasks.
7. Documentation and Comments:
 Document the purpose, functionality, and usage instructions within the shell scripts using
comments for ease of understanding and future reference.
8. Conclusion:
 Summarize the experience gained from creating advanced shell programs using grep and
egrep. Reflect on the effectiveness of these tools in text searching, pattern matching, and
data extraction within shell scripting.
Practical Session 8. Write a C program to create child process and allow parent process to display
“parent” and the child to display “child” on the screen

Objective: The objective of this practical session is to write and execute a C program in a
Linux/Unix environment that creates a child process from the parent process. The parent process
will display "parent" on the screen, while the child process will display "child".

Equipment/Tools Required:

1. Linux/Unix operating system


2. C Compiler (e.g., GCC)
3. Terminal or command-line interface

Procedure:

1. Initialization:
 Ensure the presence of a C programming environment on the Linux/Unix system, such as
GCC compiler.
2. Open Text Editor:
 Open a text editor (e.g., nano, vim, gedit) to write the C program.
3. Write the C Program:
 Implement the C program to create a child process using system calls like fork() to fork
the process into parent and child. Use printf() to display "parent" in the parent process
and "child" in the child process.
c
#include <stdio.h> #include <unistd.h> int main() { pid_t pid; // variable to hold process ID pid =
fork(); // create a child process if (pid < 0 ) { fprintf ( stderr , "Error in forking process\n" ); return 1 ; //
exit with error code } else if (pid == 0 ) { printf ( "Child\n" ); } else { printf ( "Parent\n" ); } return 0 ; //
exit the program }
4. Save and Compile:
 Save the C program with an appropriate filename (e.g., child_parent.c).
 Compile the program using the C compiler (e.g., gcc child_parent.c -o
child_parent).
5. Execute the Program:
 Run the compiled program from the terminal by executing the generated binary file:
bash
./child_parent
6. Observation:
 Observe the output displayed on the terminal, where "parent" should be printed by the
parent process, and "child" should be printed by the child process.
7. Testing:
 Test the program by executing it multiple times and confirm that each execution results in
the creation of a parent and child process with appropriate outputs.
8. Conclusion:
 Summarize the experience gained from creating a C program that demonstrates the
creation of parent and child processes and their respective outputs. Reflect on the
understanding acquired about process creation and management in C programming.
Practical Session 9. Learning of installation of dual operating systems with Linux having previously
installed or other window based OS. Both OS should be working in operating mode.

Objective: The objective of this practical session is to guide the process of installing a dual-boot
configuration with Linux alongside an existing Windows-based operating system (OS). This
exercise aims to enable both operating systems to function properly and independently, allowing
users to choose between them during system boot.

Equipment/Tools Required:

1. Computer with Windows OS already installed


2. Linux installation media (e.g., USB drive or DVD)
3. Internet connection (for downloading Linux distribution if needed)
4. Backup of important data (recommended)

Procedure:

1. Preparation:
 Ensure data backup from the Windows OS to prevent data loss during the installation
process.
 Obtain the Linux distribution you wish to install (e.g., Ubuntu, Fedora, etc.) and create a
bootable USB drive or DVD.
2. Access BIOS/UEFI Settings:
 Restart the computer and access the BIOS or UEFI settings by pressing a specific key
(usually Del, F2, F12, or Esc) during the boot process. Check the manufacturer's
instructions for the exact key.
3. Configure Boot Order:
 Set the boot order to prioritize the bootable media (USB/DVD) containing the Linux
installer.
4. Start Linux Installation:
 Insert the bootable USB drive or DVD containing the Linux distribution.
 Restart the computer to boot from the Linux installation media.
 Follow the on-screen instructions provided by the Linux installer to initiate the
installation process.
5. Partitioning:
 During the installation, choose the option to install Linux alongside the existing operating
system (Windows).
 Allocate disk space for the Linux installation by resizing the existing Windows partition
or creating a new partition.
6. Grub Boot Loader Configuration:
 The installer will likely install the GRUB (Grand Unified Bootloader) by default. GRUB
provides the boot menu to select between Windows and Linux at startup.
7. Complete Installation:
 Continue with the Linux installation process by configuring language, time zone, user
accounts, etc.
 Follow the prompts and allow the installation to complete.
8. Restart and Boot Selection:
 Once the installation finishes, restart the computer.
 The GRUB boot menu should appear, allowing you to select between Linux and
Windows OS.
9. Testing:
 Test both operating systems by booting into each to ensure they function correctly and
independently.
10. Conclusion:
 Summarize the successful installation of a dual-boot system with Linux and an existing
Windows-based OS.
 Reflect on the experience gained and the ability to switch between operating systems
during system startup.

Note:

 Carefully follow the on-screen instructions and be cautious while partitioning the disk to avoid
accidental data loss.
 Consult the documentation or online resources specific to the Linux distribution for any
installation-related queries or issues.
 Take precautions and ensure proper backups of essential data before performing any disk
partitioning or OS installation.
Practical Session 10. As Supervisor create and maintain user accounts, learn package installation, taking
backups, creation of scripts for file and user management, creation of startup and shutdown scripts
using at, batch, cron etc

Objective: The objective of this practical guide is to provide step-by-step instructions to


supervisors for various administrative tasks in a Linux/Unix environment. This includes creating
and managing user accounts, package installation, backup procedures, script creation for file and
user management, and the implementation of startup and shutdown scripts using scheduling tools
like at, batch, and cron.

Equipment/Tools Required:

1. Computer running a Linux/Unix operating system


2. Terminal or command-line interface
3. Access with administrative/supervisor privileges

Procedure:

1. User Account Management:


 Create User Accounts:
 Use the useradd command to create new user accounts. For instance:

sudo useradd -m username


 Modify User Accounts:
 Utilize usermod to modify user account properties like username or group.
Example:

sudo usermod -l new_username old_username


2. Package Installation:
 Package Installation:
 Use the package manager ( apt, yum, etc.) to install software packages. For
instance:

sudo apt install package_name


3. Backup Procedures:
 Backup Creation:
 Utilize tools like rsync, tar, or cp to create backups of files and directories.
Example:

rsync -av source_directory destination_directory


 Automated Backups:
 Schedule regular backups using cron by creating a cron job to run backup scripts
at specified intervals.
4. Script Creation for File and User Management:
 File Management Script:
 Create a shell script using commands like cp, mv, rm, etc., to perform file
management tasks.
 User Management Script:
 Develop a script using useradd, usermod, passwd, etc., to automate user
management tasks.
5. Startup and Shutdown Scripts using at, batch, cron, etc.:
 Schedule Startup Tasks:
 Use cron to schedule scripts or tasks to run automatically at system startup.
 Schedule Shutdown Tasks:
 Use at or batch to schedule tasks to run at a specified time before system
shutdown.
6. Testing and Validation:
 Test each created script or command in a controlled environment to ensure proper
functionality.
 Verify scheduled tasks using cron, at, or batch to ensure they execute as intended.
7. Documentation and Maintenance:
 Document all procedures, commands, and scripts created for future reference and training
purposes.
 Regularly review and update user accounts, backup schedules, and scripts to ensure
efficiency and security.
8. Conclusion:
 Summarize the supervisor's experience in creating and maintaining user accounts,
installing packages, performing backups, managing files and users via scripts, and
automating startup and shutdown tasks using scheduling utilities.

You might also like