DEV Community

Abhishek Gupta
Abhishek Gupta

Posted on

Linux And Shell Programing

Q1. Explain the basic architecture of the Unix/Linux system and discuss different flavors of Linux.

Unix/Linux uses a layered structure, where each layer has a specific function.

1. Hardware

  • Includes CPU, memory, hard disk, and input/output devices.
  • It is the lowest layer and works under the control of the operating system.

2. Kernel

  • The core part of Unix/Linux.
  • Directly interacts with hardware.
  • Main functions:
    • Process management
    • Memory management
    • File system management
    • Device management
    • Security control

3. Shell

  • Acts as an interface between the user and the kernel.
  • Accepts user commands and sends them to the kernel.
  • Examples: sh, bash, csh, ksh

4. Utilities and Application Programs

  • Provide tools for users.
  • Examples:
    • File commands (ls, cp)
    • Editors (vi, nano)
    • Compilers and browsers

5. Users

  • Unix/Linux supports multiple users at the same time.
  • Users access the system using the shell.

Architecture Diagram (Text Form)

Users
|
Shell
|
Utilities & Applications
|
Kernel
|
Hardware

Enter fullscreen mode Exit fullscreen mode

Different Flavors (Distributions) of Linux

Linux is available in many distributions, designed for different uses.

  • Ubuntu: Easy to use, best for beginners.
  • Debian: Very stable and secure, mainly used for servers.
  • Fedora: Uses the latest features, good for developers.
  • Red Hat Enterprise Linux (RHEL): Commercial distribution, used in companies.
  • Linux Mint: Simple and user-friendly, Windows-like interface.
  • Kali Linux: Used for security and penetration testing.

# Q2. Explain the basic file system features and commands for file and directory in Linux System

## Basic File System Features of Linux

- Linux follows a **hierarchical file system** structure.
- Everything in Linux is treated as a **file** (files, directories, devices).
- File names are **case-sensitive**.
- Supports **multi-user** and **multi-tasking** environment.
- Each file has **permissions** (read, write, execute).
- Supports file ownership (user and group).
- Provides strong **security and access control**.
- Uses a single root directory `/`.

### Common Linux Directories
- `/` : Root directory
- `/home` : User home directories
- `/bin` : Essential user commands
- `/etc` : Configuration files
- `/var` : Variable data (logs)
- `/tmp` : Temporary files

---

## File Commands in Linux

- `ls` – List files and directories
- `touch` – Create an empty file
- `cat` – Display file content
- `cp` – Copy files
- `mv` – Move or rename files
- `rm` – Delete files
- `chmod` – Change file permissions
- `chown` – Change file ownership

---

## Directory Commands in Linux

- `pwd` – Show present working directory
- `mkdir` – Create a directory
- `rmdir` – Remove an empty directory
- `cd` – Change directory
- `rm -r` – Delete a directory with contents
- `tree` – Display directory structure

# Q3. Write commands for creating file, viewing file, disk-related tasks, and checking disk free space

## Commands for Creating a File
- `touch filename` – Creates an empty file  
- `cat > filename` – Creates a file and allows writing content  
- `nano filename` – Creates and edits a file using nano editor  
- `vi filename` – Creates and edits a file using vi editor  

## Commands for Viewing a File
- `cat filename` – Displays entire file content  
- `more filename` – Views file content page by page  
- `less filename` – Advanced page-by-page viewing  
- `head filename` – Displays first 10 lines of a file  
- `tail filename` – Displays last 10 lines of a file  

## Disk-Related Commands
- `mount` – Mounts a file system  
- `umount` – Unmounts a file system  
- `lsblk` – Displays block devices  
- `df` – Shows disk space usage  
- `du` – Shows disk usage of files and directories  

## Command for Checking Disk Free Space
- `df -h` – Displays disk free space in human-readable format




# Q4. Explain process fundamentals and basic Linux commands

## Process Fundamentals in Linux

- A **process** is a program in execution.
- Each process has a unique **Process ID (PID)**.
- Processes are managed by the **kernel**.
- Types of processes:
  - **Foreground process** – Runs in front and interacts with the user.
  - **Background process** – Runs in the background.
- Process states:
  - Running
  - Waiting
  - Sleeping
  - Stopped
- Linux supports **multitasking**, allowing multiple processes to run at the same time.

## Process-Related Linux Commands

- `ps` – Displays running processes
- `top` – Shows real-time process information
- `htop` – Interactive process viewer
- `kill PID` – Terminates a process
- `bg` – Runs a process in background
- `fg` – Brings a process to foreground
- `nice` – Sets process priority

---

## Basic Linux Commands

- `ls` – List files and directories
- `pwd` – Show current directory
- `cd` – Change directory
- `mkdir` – Create a directory
- `rm` – Remove files or directories
- `cp` – Copy files
- `mv` – Move or rename files
- `cat` – Display file content
- `clear` – Clear the terminal screen
- `exit` – Exit from the terminal


# Q5. What do you understand by managing multiple processes and scheduling of processes?

## Managing Multiple Processes

Managing multiple processes means the operating system can **handle several processes at the same time**.  
In Linux/Unix, the kernel creates, manages, and terminates processes. Each process is identified by a **Process ID (PID)**.  
The OS allocates CPU time, memory, and input/output resources to each process, enabling **multitasking**, where multiple programs run simultaneously.

## Scheduling of Processes

Scheduling of processes is the technique used by the operating system to **decide which process will use the CPU and for how long**.  
The scheduler selects processes from the ready queue and assigns CPU time based on priority and scheduling rules.  
This ensures **efficient CPU utilization, fairness, and system responsiveness**.


# Q6. Explain different printing commands and mathematical commands in Linux

## Printing Commands in Linux
Printing commands are used to **display text or file content** on the terminal.

- `echo "text"` – Prints the given text or variable value on the screen.
- `printf "format"` – Prints formatted text (like in C programming).
- `cat filename` – Displays the content of a file.
- `more filename` – Views long files **page by page**.
- `less filename` – Advanced version of `more`; allows scrolling forward and backward.
- `lp filename` – Sends a file to the printer (if a printer is configured).
- `lpr filename` – Alternative command to print files.

---

## Mathematical Commands in Linux
Linux provides commands to perform **basic arithmetic and calculations**.

- `expr` – Evaluates expressions. Example: `expr 5 + 3` → 8  
- `bc` – Calculator utility for precise arithmetic. Example:  
Enter fullscreen mode Exit fullscreen mode


bash
echo "5+3" | bc


* `$(( expression ))` – Bash arithmetic expansion. Example:

  ```

bash
  result=$((5 * 3))
  echo $result


  ```

* `factor number` – Finds the factors of a number. Example: `factor 12` → 2 2 3
* `seq` – Generates a sequence of numbers. Example: `seq 1 5` → 1 2 3 4 5


# Q7. Explain the significance of shell programming and various types of shell available in Linux

## Significance of Shell Programming

Shell programming (or shell scripting) is writing **scripts using shell commands** to automate tasks in Linux. It is significant because:

- **Automation:** Automates repetitive tasks like backups, file management, and system monitoring.
- **Efficiency:** Reduces manual effort and errors by executing multiple commands together.
- **Task Scheduling:** Can be combined with cron jobs to run tasks at specific times.
- **System Administration:** Helps administrators manage users, files, processes, and system resources easily.
- **Flexibility:** Allows use of loops, conditions, and variables to create complex programs.
- **Portability:** Shell scripts can run on any Linux/Unix system without modification.

---

## Types of Shell in Linux

Linux supports various types of shells. Each shell has its own syntax and features:

1. **Bourne Shell (sh)**  
   - Original Unix shell  
   - Simple and widely available  

2. **Bash (Bourne Again Shell)**  
   - Default in most Linux distributions  
   - Combines features of Bourne Shell and C Shell  

3. **C Shell (csh)**  
   - Uses C-like syntax  
   - Provides command history and scripting features  

4. **Korn Shell (ksh)**  
   - Combines features of Bourne and C Shell  
   - Supports functions and advanced scripting  

5. **Z Shell (zsh)**  
   - Highly customizable  
   - Includes auto-completion and improved scripting features  


# Q8. Explain different shell variables and their various looping and conditional statements in Linux

## 1. Shell Variables

Shell variables are used to **store data** (like numbers, text, or command outputs) in shell scripting.

### Types of Shell Variables

1. **User-Defined Variables**  
   - Created by the user in the script or terminal.  
   - Example:  
     ```

bash
     name="Alice"
     age=20


     ```

2. **Environment Variables**  
   - Predefined variables available to all processes.  
   - Example:  
     - `HOME` – User’s home directory  
     - `PATH` – Directories for command lookup  
     - `USER` – Current logged-in user  

3. **Special Variables**  
   - `$0` – Name of the script  
   - `$1, $2…$n` – Arguments passed to the script  
   - `$#` – Number of arguments  
   - `$?` – Exit status of the last command  
   - `$$` – Process ID of the current script

---

## 2. Looping Statements in Shell

Loops are used to **repeat commands** multiple times.

1. **for loop**  
   ```

bash
   for i in 1 2 3
   do
     echo "Number: $i"
   done


Enter fullscreen mode Exit fullscreen mode
  1. while loop

bash
   count=1
   while [ $count -le 3 ]
   do
     echo "Count: $count"
     ((count++))
   done


Enter fullscreen mode Exit fullscreen mode
  1. until loop

bash
   count=1
   until [ $count -gt 3 ]
   do
     echo "Count: $count"
     ((count++))
   done


Enter fullscreen mode Exit fullscreen mode

3. Conditional Statements in Shell

Conditional statements allow decision-making in scripts.

  1. if statement

bash
   age=20
   if [ $age -ge 18 ]
   then
     echo "Adult"
   else
     echo "Minor"
   fi


Enter fullscreen mode Exit fullscreen mode
  1. if…elif…else statement

bash
   marks=75
   if [ $marks -ge 90 ]
   then
     echo "A+"
   elif [ $marks -ge 75 ]
   then
     echo "A"
   else
     echo "B"
   fi


Enter fullscreen mode Exit fullscreen mode
  1. case statement

bash
   day="Mon"
   case $day in
     Mon) echo "Monday";;
     Tue) echo "Tuesday";;
     *) echo "Other day";;
   esac


Enter fullscreen mode Exit fullscreen mode

`md
# Q9. Discuss filters using regular expressions with the help of examples

## 1. Introduction to Filters

In Linux, **filters** are commands that **take input from a file or standard input, process it, and display the output**.  
Filters are commonly used with **regular expressions (regex)** to search, match, and manipulate text.

---

## 2. Common Filter Commands Using Regular Expressions

### 1. `grep` (Global Regular Expression Print)
- Searches for lines matching a pattern in a file.
- Example:
  ```bash
  grep "Linux" file.txt
````

Output: Displays all lines containing the word "Linux".

* Using regular expressions:

  ```bash
  grep "^A" file.txt
  ```

  `^A` matches lines starting with the letter **A**.

  ```bash
  grep "end$" file.txt
  ```

  `end$` matches lines ending with **end**.

---

### 2. `egrep` / `grep -E`

* Supports **extended regular expressions**.
* Example:

  ```bash
  egrep "cat|dog" file.txt
  ```

  Matches lines containing **cat** or **dog**.

---

### 3. `fgrep` / `grep -F`

* Searches **fixed strings**, not patterns.
* Example:

  ```bash
  fgrep "Linux is fun" file.txt
  ```

---

### 4. `sed` (Stream Editor)

* Used for **search, replace, insert, or delete** text using regex.
* Example:

  ```bash
  sed 's/old/new/' file.txt
  ```

  Replaces the first occurrence of **old** with **new** in each line.

---

### 5. `awk`

* Pattern scanning and processing language.
* Example:

  ```bash
  awk '/error/ {print $0}' log.txt
  ```

  Prints lines containing the word **error**.

---

## 3. Common Regular Expressions

* `^` : Start of a line
* `$` : End of a line
* `.` : Any single character
* `*` : Zero or more occurrences
* `[]` : Character class
* `|` : OR operator






Enter fullscreen mode Exit fullscreen mode

Top comments (0)