Linux Lab Practical
Linux Lab Practical
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 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
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:
:q to quit vi.
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:
cd - Change directory.
Hello World:
#!/bin/bash
#!/bin/bash
read num1
read num2
sum=$((num1 + num2))
File Backup:
#!/bin/bash
read filename
cp "$filename" "$filename.backup"
#!/bin/bash
ls
Simple Calculator:
#!/bin/bash
read num1
read num2
read choice
case $choice in
1)
result=$((num1 + num2))
operator="+"
;;
2)
result=$((num1 - num2))
operator="-"
;;
3)
result=$((num1 * num2))
operator=""
;;
4)
operator="/"
;;
*)
exit 1
;;
esac
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 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.
Fi
Example: #!/bin/bash
num=15
fi
else
Fi
Example: #!/bin/bash
num=7
else
else
fi
Example: #!/bin/bash
num=-3
else
fi
Practical Session 4. Demonstrate control statements using shell scripting in Linux.
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:
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:
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:
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:
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
Equipment/Tools Required:
Procedure: