Unit i Linux
Unit i Linux
Unix is a powerful, multiuser, multitasking operating system widely known for its robustness,
portability, and versatility. Here are the salient features of Unix:
1. Portability
Unix is designed to be hardware-independent, meaning it can run on a wide range of
hardware platforms with minimal modifications. This is achieved by writing the
majority of the system in C, a high-level language.
2. Multitasking
Unix supports multiple tasks running simultaneously. Each task, or process, operates
independently, making it suitable for server and high-performance computing
environments.
3. Multiuser System
Unix allows multiple users to work on the same system at the same time. Each user
can have a separate environment and permissions to ensure security and resource
management.
4. Hierarchical File System
Unix uses a tree-structured file system where files are organized into directories
(folders). This structure makes data management intuitive and efficient.
5. Security
Unix provides robust security features, such as:
File permissions (read, write, execute) for the owner, group, and others.
Password-protected user accounts.
Encrypted data transfer using tools like SSH.
6. Shell Interface
Unix provides a command-line interface (CLI) through its shell. Users can interact
with the system using powerful commands and shell scripting for automation.
7. Modularity
Unix is built on the principle of "small is beautiful," with many small, modular
utilities that perform specific tasks. These can be combined (via pipes and redirection)
to achieve complex operations.
8. Process Management
Unix efficiently manages processes, allowing users to:
Create and terminate processes.
Assign priorities.
Use features like foreground and background process execution.
9. Networking
Unix has built-in support for networking, making it ideal for servers and systems
requiring remote communication. Features like FTP, SSH, and mail services are
integral.
10. Shell Scripting
Unix supports scripting in its shell (e.g., Bash, C shell, Korn shell). Scripts can
automate repetitive tasks and perform complex operations.
2. Standard Directories
The Unix file system is organized into standard directories, each serving a specific purpose:
/bin (Binaries):
o Contains essential command binaries (programs) needed by all users, such as
ls, cat, cp, etc.
/sbin (System Binaries):
o Contains essential system binaries used for system administration, such as
ifconfig, reboot, etc.
/etc (Configuration Files):
o Holds system-wide configuration files and scripts, such as passwd, hosts, and
fstab.
/home (User Home Directories):
o Contains personal directories for each user, e.g., /home/user1 and /home/user2.
/root (Root User's Home Directory):
o The home directory for the superuser (administrator).
/var (Variable Files):
o Contains files that are expected to change frequently, such as logs (/var/log),
mail (/var/mail), and temporary data.
/tmp (Temporary Files):
o Used to store temporary files that may be cleared after a reboot.
/usr (User System Resources):
o Holds user utilities and applications. Subdirectories include:
/usr/bin: Non-essential binaries for users.
/usr/lib: Libraries for applications in /usr/bin.
/usr/share: Shared files like documentation and icons.
/lib (Libraries):
o Contains shared library files essential for binaries in /bin and /sbin.
/dev (Device Files):
o Contains device files that represent hardware components like disks
(/dev/sda), terminals (/dev/tty), and printers.
/proc (Process Information):
o A virtual directory that provides information about system processes and
kernel in real-time.
/sys (System Information):
o Contains information about the system's hardware and kernel modules.
/opt (Optional Software):
o Used to install third-party or optional software packages.
TYPES OF SHELL
In Unix and Linux, a shell is a command-line interface that allows users to interact with the
operating system by executing commands, running scripts, and managing system tasks.
1. Bourne Shell (sh)
Developer: Stephen Bourne at AT&T Bell Labs.
Description:
o One of the earliest Unix shells and the foundation for many modern shells.
o Focused on scripting with powerful programming constructs.
Path: /bin/sh
Features:
o Simple syntax for scripting.
o Limited interactivity compared to modern shells.
Uses: Writing shell scripts for portability.
2. C Shell (csh)
Developer: Bill Joy at the University of California, Berkeley.
Description:
o A shell with C-like syntax for scripting.
Path: /bin/csh
Features:
o Built-in support for aliases.
o Job control features.
o History substitution.
Limitations: Poor error handling in scripts compared to Bourne-based shells.
3. Korn Shell (ksh)
Developer: David Korn at AT&T Bell Labs.
Description:
o Combines features of Bourne Shell and C Shell.
Path: /bin/ksh
Features:
o Command-line editing.
o Advanced scripting features (arrays, floating-point arithmetic).
o Job control.
Uses: Preferred for scripting in enterprise environments.
/bin Essential binaries (e.g., ls, cp, mv) for basic system operation.
/var Variable files like logs (/var/log), mail (/var/mail), and caches.
/dev Device files for hardware devices (e.g., /dev/sda for hard drives).
CREATING FILES
Creating files in the Unix operating system can be done using several commands and tools.
1. Using the touch Command
The touch command creates an empty file if it does not already exist.
If the file exists, it updates the file's timestamp.
This creates a file named myfile.txt and lets you enter its content directly.
Use Ctrl+D to signal the end of input.
This creates a file named myfile.txt with the text "Hello, Unix!" inside it.
4. Using the vi or vim Editor
Interactive text editors like vi or vim can be used to create and edit files.
This opens the vi editor. You can start typing after entering insert mode by pressing i.
Save the file by pressing Esc, typing :wq, and hitting Enter.
Common Options:
ls -l: Displays detailed information (permissions, ownership, size, modification date).
ls -a: Shows all files, including hidden files (those starting with a dot .).
ls -lh: Displays file sizes in a human-readable format (e.g., KB, MB).
ls -R: Recursively lists files in subdirectories.
ls -t: Sorts files by modification time.
ls -r: Reverses the sort order.
Directory Permissions
In Unix, directory permissions define who can read, write, and execute (access) a directory.
Each file and directory has three levels of permissions for three types of users. Here’s a
breakdown:
Structure of Permissions
Permissions are displayed as a string when using ls -l (e.g., drwxr-xr--):
Example Breakdown: drwxr-xr--
First character (d): Indicates the type:
o d: Directory
o -: Regular file
o l: Symbolic link
Next nine characters: Indicate permissions, divided into three groups:
1. Owner (user): rwx (read, write, execute)
2. Group: r-x (read, no write, execute)
3. Others: r-- (read, no write, no execute)
Permission Types
Read (r):
o View the contents of the directory (list files).
Write (w):
o Create, delete, or rename files in the directory.
o Requires execute (x) permission to take effect.
Execute (x):
o Access (cd into) the directory.
o Without execute permission, you cannot enter the directory even if you can list
its contents.
Bit of Mathematics
Unix provides several command-line tools for performing basic mathematical operations
directly in the shell.
1. expr
The expr command is used for simple arithmetic and string operations in Unix.
Example
2. factor
The factor command is used to find the prime factors of a number.
Example
3. bc (Basic Calculator)
The bc command is a powerful calculator that supports floating-point arithmetic and more
advanced mathematical operations.
Example
4. Square Root (sqrt)
The sqrt function is available through bc for square root operations. Ensure you use the scale
variable for precision.
Example:
1. Navigating Directories
pwd (Print Working Directory): Displays the current directory.
Blocks
In Unix operating systems, "blocks" refer to fixed-size chunks of data that are used to manage
and store information on disk. The size of a block is typically 512 bytes, 4 KB, or larger,
depending on the file system.Understanding blocks is essential when dealing with file
systems, storage, and performance.
Boot Blocks
Purpose: Contain boot-related information for the file system or operating system.
Typically located at the beginning of the disk.
Store the boot loader, which is responsible for loading the operating system into
memory.
Superblock
Purpose: Stores critical information about the file system itself.
Contains:
o File system size
o Block size
o Number of inodes and data blocks
o File system state (e.g., clean, needs repair)
o Location of free blocks and inodes
The superblock is crucial for file system integrity and recovery.
Typically, multiple copies of the superblock are stored for redundancy.
Inode Blocks
Purpose: Store metadata about files and directories.
Each file or directory is represented by an inode, which contains:
o File type (regular file, directory, symbolic link, etc.)
o File size
o File ownership (user and group IDs)
o Permissions
o Timestamps (creation, modification, access)
o Pointers to data blocks
Inode blocks do not store the actual file content—only metadata and pointers.
Data Blocks
Purpose: Store the actual content of files.
Files in the file system are split into fixed-size chunks (blocks) and stored here.
For small files, a single data block may suffice; larger files require multiple blocks.