[go: up one dir, main page]

0% found this document useful (0 votes)
24 views22 pages

Linux Practical File for BCA Students

The document is a practical file for a Linux OS lab course, detailing various exercises and commands related to Linux. It includes tasks on connecting to a Linux server, using help commands, navigating directories, and writing shell scripts. Each task is aimed at enhancing the understanding of Linux commands and scripting for students in the BCA program.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views22 pages

Linux Practical File for BCA Students

The document is a practical file for a Linux OS lab course, detailing various exercises and commands related to Linux. It includes tasks on connecting to a Linux server, using help commands, navigating directories, and writing shell scripts. Each task is aimed at enhancing the understanding of Linux commands and scripting for students in the BCA program.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Maharaja Surajmal

Institute
C-4, Janakpuri, New Delhi

PRACTICAL FILE

BCA-373
Practical Linux - OS lab

Submitted to: Submitted by:

Mrs. Pooja Singh Name: Mohd Noor Aman

Associate Professor Class: BCA 5A - Morning

Department of Computer Science Enroll no. : 06114902021


S. No. Programs Sign

1 Connect to the Linux Server and understand the basic


Directory Structure of Linux.

2 To understand help commands like:-man, info, help,


whatis, apropos

3 To understand basic directory navigation commands like


cat,ed, mv, cp, rm, mkdir, rmdir file, pwd command.

4 To understand basic commands like:- date,cal,


echo,bc,ls, who, whoami, hostname, uname, tty,aliase

5 To understand vi basics, Three modes of vi Editor, how


to write, save, execute a shell script in vi editor.

6 To understand process related commands like: -ps, top,


pstree, nice, renice in Linux.

7 To understand how to examine and change File


permissions.

8 Set a file to be read-only with the chmod command.


Interpret the file permissions displayed by the Is -I
command.

9 Delete one or more directories with the rmdir command.


See what happens if the directory is not empty.
Experiment (carefully!) with the rm -r command to
delete a directory and its content.

10 Change your directory to the directory exercises.


Create a file in that directory, named the file as
examplel using the cat command containing the
following text: water, water everywhere and all the
boards did shrink; water, water everywhere, no drop to
drink.

11 Write basic shell script to display the table of a


number.

12 Write basic shell script to input a character from


user and then check whether it is uppercase, lowercase
or digit.
13 Write basic shell script to calculate factorial of a
number.

14 Write basic shell script to input the month number and


generate corresponding calendar.

15 Write basic shell script to list all directories.

16 Write basic shell script to display greatest of three


numbers.

17 Write basic shell script to check whether the number


entered by user is prime or not.
Q1 Connect to the Linux Server and understand the
basic Directory Structure of Linux.
● / (Root directory): The top-level directory, everything stems from
here.
● /bin: Contains essential binary/executable files.
● /sbin: Similar to /bin, but holds binaries used by the system
administrator.
● /etc: Configuration files for the system and installed programs.
● /home: Home directories for users.
● /var: Variable files like logs, spool files, etc.
● /tmp: Temporary files.
● /usr: User-related programs and files.
● /lib and /lib64: Essential shared libraries.
● /opt: Optional software packages.
● /mnt and /media: Mount points for removable media.
● /dev: Device files.
● /proc and /sys: Virtual filesystems that provide information about
processes, hardware, etc.

To connect to a Linux server, you'd typically use an SSH client like


PuTTY (on Windows) or the ssh command on a Unix-based system.
Q2 To understand help commands like:-man,
info,help, whatis, apropos
● man: Manual pages command that displays the manual of a specific
command or a topic. Example: man ls shows the manual for the ls
command.

● info: Another documentation system often more detailed than man


pages, with hyperlinks and structured sections. Example: info ls
for information about ls.
● help: Shell built-in command providing information on shell
commands. For instance, help cd provides details about the cd
command within the shell.

● whatis: Provides a brief description of a command. Example: whatis


ls gives a short description of the ls command.

● apropos: Searches the manual page names and descriptions for the
given keyword. For example, apropos network searches for commands
related to "network."
Q3 To understand basic directory navigation
commands like cat,ed, mv, cp, rm, mkdir,rmdir file,
pwd command.
● cd: Change directory. Use cd followed by a directory name to
navigate. Example: cd Documents moves to the "Documents"
directory.
● ls: List directory contents. Use ls to display files and
directories in the current directory. Example: ls -l shows
detailed information.
● pwd: Print working directory. Displays the current directory path.
● cat: Concatenate and display file content. Example: cat [Link]
shows the content of "[Link]".
● ed: Line-oriented text editor. Used in the terminal for editing
files.
● mv: Move or rename files/directories. Example: mv [Link]
[Link] renames "[Link]" to "[Link]".
● cp: Copy files/directories. Example: cp [Link] /destination
copies "[Link]" to the /destination directory.
● rm: Remove files/directories. Be cautious, as it deletes files
without confirmation. Example: rm [Link] deletes "[Link]".
● mkdir: Create directories. Example: mkdir new_directory creates a
directory named "new_directory".
● rmdir: Remove directories. It only works if the directory is
empty. Example: rmdir empty_directory removes the
"empty_directory".
Q4 To understand basic commands like:- date,cal,
echo,bc,ls, who, whoami, hostname, uname,
tty,aliase
● date: Displays the current date and time. Example: date
● cal: Shows a calendar for the current month or a specified
month/year. Example: cal or cal 12 2023 for December 2023.
● echo: Prints text or variables to the terminal. Example: echo
Hello, World!
● bc: Basic calculator. Launches an interactive calculator. Example:
bc
● ls: Lists files and directories in the current directory. Example:
ls
● who: Displays information about currently logged-in users.
Example: who
● whoami: Prints the username of the current user. Example: whoami
● hostname: Displays the system's hostname. Example: hostname
● uname: Shows system information. Example: uname -a displays all
system information.
● tty: Prints the file name of the terminal connected to the
standard input. Example: tty
● alias: Creates an alias for a command. Example: alias ll='ls -l'
creates an alias 'll' for the ls -l command
Q5 To understand vi basics, Three modes of vi
Editor, how to write, save, execute a shell script in vi
editor.
1. Command Mode: The default mode when you open Vi. Used for
navigating, deleting, copying, and pasting text. To enter Command
Mode from another mode, press Esc.
2. Insert Mode: Used for typing and inserting text. To enter Insert
Mode from Command Mode, press i for insert before the cursor, or a
for insert after the cursor.
3. Visual Mode: Used for selecting and manipulating blocks of text.
To enter Visual Mode from Command Mode, press v.

To write, save, and execute a shell script in Vi:

1. Open Vi: Type vi [Link] in the terminal to open Vi with a


new or existing script named [Link].
2. Enter Insert Mode: Press i to start typing your script.
3. Write Your Script: Enter the script commands.
4. Save and Exit:
○ Save: Press Esc to enter Command Mode, then type :w and
press Enter to save.
○ Save and Exit: To save and exit, enter Command Mode (Esc),
then type :wq and press Enter.
Q6 To understand process related commands like:
-ps, top, pstree, nice, renice in Linux.
● ps: Process Status command used to display information about
active processes. Commonly used options include ps aux to display
all processes running on the system and ps -ef to show detailed
information about all processes.

● top: Interactive real-time process viewer. It displays system


summary information as well as a list of processes currently being
managed by the Linux kernel. top provides a dynamic view of system
processes, their resource usage, and system performance.

● pstree: Displays a tree diagram of processes, showing their


hierarchical structure. It illustrates the parent-child
relationship between processes.
● nice: A command used to execute processes with a specified
priority. By default, processes are executed with a certain
priority level (usually 0). nice allows setting the priority level
of a process, with lower values indicating higher priority (a
higher priority process gets more CPU time).

● renice: Command to change the priority of already running


processes. It alters the scheduling priority of one or more
running processes. For instance, renice +10 PID increases the
priority of a process with ID PID by 10 (lowering its priority),
and renice -5 PID decreases the priority by 5 (raising its
priority).
Q7 To understand how to examine and change File
permissions.
Examining File Permissions:

● ls -l Command: Use this command to view permissions. It displays


file/directory information, including permissions, owners, groups,
and more.
For example:

● Here, -r--r--r-- indicates permissions for the owner, group, and


others.

Changing File Permissions:

● chmod Command: Used to change file permissions.


○ Symbolic Mode: Modifies permissions symbolically using
letters (u, g, o for user, group, others; +, -, = for add,
remove, set).
■ Example: chmod u+x filename adds execute permission for
the file's owner.
○ Numeric Mode: Assigns permissions using numeric values (each
digit represents user, group, others).
■ Example: chmod 755 filename gives read, write, and
execute permissions to the owner, and read/execute to
group and others.

Changing Ownership:

● chown Command: Changes file ownership.


○ Example: chown user:group filename changes the owner and
group of the file.

Changing Group Ownership:

● chgrp Command: Changes group ownership of a file.


○ Example: chgrp newgroup filename changes the group of the
file to newgroup.
Q8 Set a file to be read-only with the chmod
command. Interpret the file permissions displayed by
the Is -I command.
Q9 Delete one or more directories with the rmdir
command. See what happens if the directory is not
empty. Experiment (carefully!) with the rm -r
command to delete a directory and its content.
Q10 Change your directory to the directory exercises.
Create a file in that directory, named the file as
example using the cat command containing the
following text: water, water everywhere and all the
boards did shrink; water, water everywhere, no drop
to drink.
Q11 Write basic shell script to display the table of a
number.
#!/bin/bash

# Get user input for the number


echo "Enter a number to display its table:"
read num

# Loop to display the table


echo "Table of $num:"
for (( i=1; i<=10; i++ ))
do
echo "$num x $i = $((num*i))"
done
Q12 Write basic shell script to input a character from
user and then check whether it is uppercase,
lowercase or digit.
#!/bin/bash

# Get user input for a character


echo "Enter a character:"
read char

# Check if the input is uppercase, lowercase, or a digit


if [[ $char =~ [[:upper:]] ]]; then
echo "The character is uppercase."
elif [[ $char =~ [[:lower:]] ]]; then
echo "The character is lowercase."
elif [[ $char =~ [[:digit:]] ]]; then
echo "The character is a digit."
else
echo "The character is neither uppercase, lowercase, nor a digit."
fi
Q13 Write basic shell script to calculate factorial of a
number.
#!/bin/bash

# Function to calculate factorial


factorial() {
if [ $1 -eq 0 ] || [ $1 -eq 1 ]; then
echo 1
else
fact=1
for (( i=1; i<=$1; i++ ))
do
fact=$((fact * i))
done
echo $fact
fi
}

# Get user input for the number


echo "Enter a number to calculate its factorial:"
read num

# Call the factorial function and display the result


result=$(factorial $num)
echo "The factorial of $num is: $result"
Q14 Write basic shell script to input the month
number and generate corresponding calendar.
#!/bin/bash

# Get user input for the month number


echo "Enter a month number (1-12) to generate the calendar:"
read month

# Check if the input is a valid month number


if [ $month -ge 1 ] && [ $month -le 12 ]; then
cal $month $(date +"%Y")
else
echo "Invalid month number. Please enter a number between 1 and
12."
fi
Q15 Write basic shell script to list all directories.
#!/bin/bash

# List all directories in the current directory


echo "List of directories:"
for dir in */; do
if [ -d "$dir" ]; then
echo "$dir"
fi
done
Q16 Write basic shell script to display greatest of
three numbers.
#!/bin/bash

# Get user input for three numbers


echo "Enter three numbers:"
read num1
read num2
read num3

# Check which number is the greatest


if [ $num1 -gt $num2 ] && [ $num1 -gt $num3 ]; then
echo "The greatest number is: $num1"
elif [ $num2 -gt $num1 ] && [ $num2 -gt $num3 ]; then
echo "The greatest number is: $num2"
else
echo "The greatest number is: $num3"
fi
Q17 Write basic shell script to check whether the
number entered by user is prime or not.
#!/bin/bash

# Function to check if a number is prime


is_prime() {
num=$1
if [ $num -le 1 ]; then
echo "Not a prime number."
elif [ $num -eq 2 ] || [ $num -eq 3 ]; then
echo "Prime number."
else
for (( i=2; i<=($num/2); i++ ))
do
if [ $((num%i)) -eq 0 ]; then
echo "Not a prime number."
exit
fi
done
echo "Prime number."
fi
}

# Get user input for the number


echo "Enter a number to check if it's prime:"
read number

# Call the function to check if the number is prime


is_prime $number

You might also like