Theory - 3 :- Linux file system, The Shell, Users and file permissions, VI editor, X window system, Filter Commands, Processes, Shell Scripting.

1. Linux File System

The Linux File System is organized in a hierarchical, inverted tree structure. Everything in Linux—including hardware devices—is treated as a file.

  • Root Directory (/): The top-level directory from which all other directories branch out.
  • Key Directories:
    • /bin & /sbin: Essential command binaries (e.g., ls, cd) and system administration binaries.
    • /home: User home directories (e.g., /home/username).
    • /etc: System configuration files.
    • /var: Variable data like logs, spools, and temporary mail files.
    • /dev: Device files representing hardware components.
  • Inodes: Every file is associated with an Index Node (inode), which stores metadata about the file (size, permissions, owner, timestamps) except for its actual name and data contents.

2. The Shell

The shell is a command-line interpreter that acts as an interface between the user and the Linux kernel. It accepts human-readable commands and translates them into something the kernel can execute.

  • Common Shells: Bash (Bourne Again Shell - the default on most systems), Zsh, Sh, and Csh.
  • Basic Command Syntax: command [options] [arguments] (e.g., ls -l /var).
  • Standard Streams:
    • stdin (Standard Input): File descriptor 0 (keyboard by default).
    • stdout (Standard Output): File descriptor 1 (screen by default).
    • stderr (Standard Error): File descriptor 2 (error messages on screen).
  • Redirection:
    • > redirects stdout to a file (overwrites).
    • >> appends stdout to a file.
    • < redirects stdin from a file.

3. Users and File Permissions

Linux is a multi-user system, requiring strict security controls over who can access or modify files.

Ownership Levels

Every file/directory has three levels of ownership:

  1. User (u): The individual who owns the file.
  2. Group (g): A collection of users sharing access permissions.
  3. Others (o): Anyone else on the system.

Permission Types

Permissions are split into Read, Write, and Execute:

Permission

File Context

Directory Context

Numeric Value

Read (r)

View file content

List files inside

4

Write (w)

Modify file content

Create/Delete files inside

2

Execute (x)

Run file as a program

Enter (cd into) the directory

1

  • Changing Permissions (chmod): * Absolute mode: chmod 755 filename (User: rwx=7, Group: r-x=5, Others: r-x=5).
    • Symbolic mode: chmod u+x,g-w filename.
  • Changing Ownership (chown): chown username:groupname filename.

4. VI Editor

The vi editor is a powerful, visual text editor available on almost all Unix-like systems. It operates primarily in three distinct modes:

  • Command Mode: The default mode upon opening. Keystrokes are interpreted as navigation or text manipulation commands (e.g., dd deletes a line, yy copies a line).
  • Insert Mode: Used for entering text. Enter this mode from command mode by pressing i, a, or o. Press Esc to return to Command Mode.
  • Last Line (Ex) Mode: Accessed by typing : from Command Mode. Used for saving and quitting:
    • :w – Save (write)
    • :q! – Quit without saving
    • :wq or :x – Save and quit

5. X Window System (X11)

The X Window System provides the underlying framework for a graphical user interface (GUI) on Unix-like operating systems.

  • Client-Server Architecture: * X Server: The program that directly manages the local display hardware, keyboard, mouse, and graphics card.
    • X Client: The graphical application itself (e.g., a web browser or terminal emulator). It sends requests to the X Server to draw elements and receives input events from it.
  • Network Transparency: The X Client and X Server do not need to run on the same machine. You can run a heavy simulation program (Client) on a remote supercomputer and display its window on your local laptop (Server).

6. Filter Commands

Filters are text-processing utilities that take input from standard input, transform data according to specific rules, and output the result to standard output. They are frequently combined using the pipe (|) operator.

  • grep: Searches text for a specified pattern (Regular Expressions).
  • awk: A powerful pattern-scanning and processing language, ideal for manipulating column-based data.
  • sed: Stream Editor, primarily used for find-and-replace text transformations.
  • sort & uniq: sort arranges lines alphabetically/numerically; uniq removes or reports duplicate lines (requires sorted input).
  • head & tail: head -n 5 displays the first 5 lines; tail -n 5 displays the last 5 lines.

7. Processes

A process is an instance of a running program executing in memory.

  • Process ID (PID): A unique numerical identifier assigned by the kernel to every running process. PID 1 is typically systemd or init, the parent of all other processes.
  • States: Running, Sleeping, Stopped, or Zombie (finished execution but waiting for the parent to read its exit status).
  • Management Commands:
    • ps / top / htop: View current active processes and resource usage.
    • kill [PID]: Sends a signal to stop a process (e.g., kill -9 sends a SIGKILL force-stop signal).
    • bg / fg: Moves a process to the background or brings it back to the foreground.

8. Shell Scripting

A shell script is a plain text file containing a sequence of commands that the shell executes automatically.

  • Shebang Line: The very first line specifies the interpreter path: #!/bin/bash.
  • Variables: Defined without spaces around the = sign, and referenced using a $:

Bash

NAME="Linux"

echo "Hello $NAME"

  • Positional Parameters: Arguments passed to the script at execution time ($1 is the first argument, $2 is the second, $# is the total count).
  • Control Structures:
    • Conditional (If/Else):

Bash

if [ $1 -gt 10 ]; then

    echo "Greater than 10"

fi

    • Loops (For/While):

Bash

for i in {1..5}; do

    echo "Iteration $i"

done

 

Post a Comment

0 Comments