0% found this document useful (0 votes)
9 views9 pages

Assignment

The document provides an overview of various Linux commands including grep, head, tail, pwd, and cal, explaining their syntax, usage, and real-time examples. It also compares the cp, mv, and ln commands, detailing their purposes and providing use cases for hard and symbolic links. Additionally, it includes a shell script example for checking even or odd numbers and discusses system calls in Linux with a focus on the fork() system call and its process hierarchy.

Uploaded by

Emo Theledu
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)
9 views9 pages

Assignment

The document provides an overview of various Linux commands including grep, head, tail, pwd, and cal, explaining their syntax, usage, and real-time examples. It also compares the cp, mv, and ln commands, detailing their purposes and providing use cases for hard and symbolic links. Additionally, it includes a shell script example for checking even or odd numbers and discusses system calls in Linux with a focus on the fork() system call and its process hierarchy.

Uploaded by

Emo Theledu
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
You are on page 1/ 9

Assignment

Q1) Write the syntax and explain the usage of the following Linux commands with one
real-time example each:

a) grep

b) head

c) tail

d) pwd

e) cal

Answer:

a. grep

Syntax: grep [options] pattern [file...]

Explanation & Usage:


The grep command is a powerful tool for searching through text. It scans files or input
for lines that match a specified pattern, which can be a word, phrase, or even a
regular expression. This is especially useful for quickly locating relevant information
in large files.

Real-Time Example:
Suppose a system administrator wants to find all lines containing the word "error" in the
system log file. This helps in identifying issues or failures in the system.

Command: grep "error" /var/log/syslog

This command will display all lines from /var/log/syslog that contain the word "error".
b. head

Syntax: head [options] [file...]

Explanation & Usage:


The head command is used to view the beginning of a file. By default, it shows the first
10 lines, but you can specify a different number if needed. This is handy when you want
to quickly check the top portion of a large file, such as configuration files or data logs.

Real-Time Example:
Imagine you want to see the first 5 entries in the /etc/passwd file to check the initial user
accounts.

Command: head -n 5 /etc/passwd

This will display the first five lines of the /etc/passwd file.

c. tail

Syntax: tail [options] [file...]

Explanation & Usage:


The tail command allows you to view the end of a file, which is particularly useful for
monitoring log files as new entries are added. By default, it shows the last 10 lines, but
you can also use options to follow the file in real-time.

Real-Time Example:
Imagine you are troubleshooting a running service and want to observe new system
events as they are recorded. By watching the /var/log/syslog file in real-time, you can
instantly see any new log entries as they appear.

Command: tail -f /var/log/syslog

This command will continuously display the latest lines added to the syslog file.
d. pwd

Syntax: pwd

Explanation & Usage:


The pwd (print working directory) command displays the full path of your current
directory. This is essential when working with multiple directories, as it helps you
confirm your present location in the filesystem.

Real-Time Example:
If you’re navigating through directories and want to double-check where you are, simply
use:

Command: pwd

This will print the absolute path to your current directory.

e. cal

Syntax: cal [options] [[month] year]

Explanation & Usage:


The cal command is used to display a calendar in the terminal. By default, it shows the
current month, but you can specify any month and year to view the corresponding
calendar. This is useful for quick date references or planning.

Real-Time Example:
If you want to see the calendar for July 2025, you can use:

Command: cal 7 2025

This will display the calendar for July 2025, helping you check dates without leaving the
command line.
Q2) Compare and contrast the cp, mv, and In commands in Linux. Provide use cases
and demonstrate their usage with suitable examples. Discuss when you would use a
hard link versus a symbolic link.

Answer:

Comparison of cp, mv, and ln Commands in Linux

The commands cp, mv, and ln are essential for managing files on a Linux system.
Below is a comparison, use cases, and practical examples using a
file named (hameed.txt).

1. cp — Copy Files

• Purpose: Creates a duplicate of a file or directory.

• Syntax: cp [options] source destination

• Use Case: Backing up a file before making changes.

Example:
Suppose you want to create a backup of hameed.txt before editing it.

Command: cp hameed.txt hameed_backup.txt

After this, both hameed.txt and hameed_backup.txt exist separately.

2. mv — Move or Rename Files

• Purpose: Moves a file to a new location or renames it.

• Syntax: mv [options] source destination

• Use Case: Organizing files or renaming them.

Example:
To rename hameed.txt to hameed_final.txt:

Command: mv hameed.txt hameed_final.txt

Now, only hameed_final.txt exists; the original name is gone.


3. ln — Create Links

• Purpose: Creates links (hard or symbolic) to files.

• Syntax: ln [options] target link_name

• Types:

• Hard Link: Another name for the same file data.

• Symbolic Link (Symlink): A shortcut pointing to the file.

a) Hard Link

• Use Case: Need another reference to the same file data on the same filesystem.

Example:
Create a hard link to hameed.txt:

Command: ln hameed.txt hameed_hardlink.txt

Both files now point to the same content. Deleting one does not remove the data as long
as the other exists.

b) Symbolic Link (Symlink)

• Use Case: Create shortcuts, link across filesystems, or link to directories.

Example:
Create a symbolic link to hameed.txt:

Command: ln -s hameed.txt hameed_symlink.txt

hameed_symlink.txt acts as a pointer to hameed.txt. If the original is deleted, the


symlink becomes broken.
Q3) Write a shell script to accept a number from the user and check whether it is even
or odd. Also explain how shell scripts are executed in Linux.

Answer:

Shell Script to Check Whether a Number is Even or Odd

Code:

#!/bin/bash

# Accept a number from the user

echo "Enter a number:"

read number

# Check if the number is even or odd using modulo operator

if [ $((number % 2)) -eq 0 ]

then

echo "$number is Even"

else

echo "$number is Odd"

fi

nano even_odd.sh

1. Create the Script File:


Open a terminal and use a text editor to create the script file.

Command: nano even_odd.sh

2. Make the Script Executable:


Before running the script, give it execute permission.

Permission: chmod +x even_odd.sh

3. Run the Script:


Execute the script by typing: ./even_odd.sh
Enter a number when prompted, and the script will display whether it is even or odd.

How Shell Scripts are Executed in Linux

1. Writing the Script:

• Write the shell script in a text file, usually with a .sh extension.

• The first line (#!/bin/bash) is called the shebang and tells the system
which interpreter to use.

2. Making the Script Executable:

• Use the chmod +x filename.sh command to give the script execute


permissions.

3. Running the Script:

• You can run the script by specifying its path, for example: ./filename.sh

Q4) Explain the concept of system calls in Linux. Write a simple C program using the
fork() system call and explain its output and process hierarchy.

Answer:

System Calls in Linux :

System calls are the fundamental interface between a user application and the Linux
operating system kernel. When a program needs to perform operations that require
direct interaction with the hardware or core OS services—such as file manipulation,
process creation, or communication—it uses system calls. These act as controlled
entry points, allowing user programs to request services from the kernel while
maintaining security and stability.

• Examples of system calls: read(), write(), open(), close(), fork(), exec(), wait(),
etc.
• Why use system calls? User programs cannot access hardware or kernel
resources directly for security and stability reasons. System calls provide a safe
mechanism for such access.

Example: Using fork() System Call in C

The fork() system call is used to create a new process by duplicating the calling process.
The new process is called the child process, and the original is the parent process.

Code:

Explanation of the Output

• fork() returns twice:

• In the parent process, it returns the PID of the child.

• In the child process, it returns 0.

• Both the parent and child execute the code after fork(), but the if statement
distinguishes their roles.

• Output:

Process Hierarchy

After calling fork():

• The parent process continues running and now has a child process.

• The child process is a copy of the parent but has a unique process ID (PID).

• The child’s parent process ID (PPID) is the PID of the parent.

• The shell (or any program that started your C program) is the ancestor of both.
Hierarchy Example:

Process Type PID Parent PID (PPID)

Parent 1751 (shell or other)

Child 1752 1751

You might also like