Unix Mod1pdf
Unix Mod1pdf
• An operating system is software that controls the computer’s hardware and helps run programs safely and easily.
• >serves as an interface between applications and hardware resources, such as memory, storage devices (hard disks), and peripherals (printers). I
• it acts as a bridge between programs and hardware like memory, storage, and printers.
• The OS starts when the computer is turned on and works as long as the computer is running.
• UNIX is a collection of programs that acts as a link between the user and the computer.
• It is the foundation for many operating systems like Ubuntu, Solaris, and POSIX.
• Developed in the 1970s by Ken Thompson, Dennis Ritchie, and others at AT&T Laboratories, it was originally written in assembly language but later rewritten in C.
• The core part of UNIX is the kernel, and users interact with it through a program called the shell.
• The shell is a command-line interpreter that works as an interface between the user and the system.
UNIX Architecture diagram from pdf ang go through the pdf once
1. Division of Labor
o It gets loaded into memory when the system starts and directly interacts with the hardware.
o Programs use the kernel’s services (called system calls) to access hardware.
o The kernel manages memory, schedules tasks, sets priorities, and handles many other background tasks.
o Even if no programs are running, the kernel keeps the system working.
o )
▪ Manages memory, schedules processes, sets priorities, and performs essential tasks even when no user program is running.
o The shell is the interface between the user and the kernel.
o Even though there's only one kernel, multiple users can have different shells running at the same time.
Different Shells
C Shell (%)
o The name comes from its commands, which look like C programming language statements.
System Prompt: For Bourne, Bash, and Korn shells, the prompt is usually a dollar sign ($).)
▪ Each user can have their own shell, even though only one kernel runs on the system.
▪ Types of shells:
▪ Korn Shell (Ksh): Combines features of Sh and Csh, offering advanced scripting.
▪ Bash (Bourne Again Shell): Enhanced version of Sh, default in most Linux systems.
o File:
▪ Text files are dominant and can be manipulated with various tools.
o Process:
▪ Processes can be controlled (moved between foreground and background) with UNIX tools.
System Calls( System Calls in Unix
• The Unix system (including the kernel, shell, and applications) is written in C.
• System calls are functions used by commands to communicate with the kernel.
• For example, commands like write(), read(), open(), and close() are system calls that help the shell interact with files.
• The same system calls can be used for both files and devices.
• )
o POSIX: A standard ensuring compatibility between UNIX-like systems by defining consistent system call interfaces.
o Examples:
Key Features
• Kernel-Shell Relationship: The kernel manages the hardware, and the shell serves as a user interface for executing commands.
Features of UNIX
1. Multiuser Support
o UNIX is a multiuser system, allowing multiple users to access the same computer system simultaneously.
o Resources like CPU, memory, and disk space are efficiently shared between users.
o Fair Resource Allocation: The system ensures that all users get an equal and fair share of resources without affecting overall performance.
o Useful in environments like servers, where many users may work on the same system.
2. Multitasking
o UNIX is a multitasking operating system, meaning it can run multiple tasks or processes at the same time.
o Process Scheduler: Manages how processes are executed, ensuring fair CPU time for each.
o Time-Sharing Mechanism: Divides CPU time among active processes to ensure responsiveness.
▪ Background Processes: Run without user interaction, allowing the user to work on other tasks simultaneously.
▪ Example: Use & to run a process in the background (e.g., command &).
3. Building-Block Approach
o UNIX provides numerous small, simple commands, each designed for a specific task.
o Pipes (|): Connect commands to pass the output of one as the input to another, enabling the creation of complex workflows.
o Benefits:
4. UNIX Toolkit
UNIX Toolkit
• A collection of small, flexible command-line tools for performing tasks like text manipulation, system administration, networking, and programming.
• Files, commands, and devices are treated uniformly, enabling seamless interaction between them.
o The UNIX toolkit refers to a collection of powerful and flexible command-line utilities.
o Tools include:
▪ General-Purpose Tools: Perform tasks like file editing (cat, vi), sorting (sort), and counting (wc).
▪ Text Manipulation Utilities: Such as grep (pattern searching), sed (stream editing), and awk (text processing).
▪ Networked Applications: Tools for networking like ftp, ssh, and curl.
▪ System Administration Tools: For managing users, processes, and file systems.
▪ In UNIX, files, commands, and devices are treated as files, enabling seamless interaction.
▪ Tools accept input from files, other commands, or standard input (keyboard).
5. Pattern Matching
o A powerful feature in UNIX for searching and manipulating text using regular expressions and metacharacters.
▪ Examples:
o Tools like grep, sed, and awk use pattern matching extensively.
6. Programming Facility
Programming Facility
• The UNIX shell functions as a programming language with support for variables, loops, and control structures.
o The UNIX shell is not just a command interpreter but also a programming language.
o Features include:
▪ Shell Scripts: Scripts are text files containing sequences of UNIX commands.
o Flexibility: Users can combine commands and logic to create custom solutions.
7. Documentation
o UNIX offers comprehensive documentation for every command and system feature:
▪ Man Pages (Manual Pages): The primary source of documentation, offering detailed information on commands, system calls, libraries, etc.
▪ Info Pages: Provide more detailed documentation for some commands and topics.
▪ Help Options: Many commands include a --help option to display usage information.
▪ Example: ls --help.
o Importance: Documentation ensures that users can understand and utilize system features effectively.
The UNIX operating system is built on a layered architecture that provides a robust and efficient environment for multitasking, resource management, and user interaction. Here is a detailed breakdown of its key
components and their relationships:
1. The Kernel
The kernel is the core of the UNIX operating system. It is responsible for managing the system's resources and facilitating communication between hardware and software.
Key Functions
1. Process Control
▪ Process Scheduling: Determines the order in which processes are executed, ensuring fair CPU allocation.
▪ Inter-Process Communication (IPC): Enables processes to share data and coordinate activities using mechanisms like pipes, message queues, and shared memory.
2. Resource Management
▪ Memory Management: Allocates and deallocates memory for processes, ensuring efficient utilization.
▪ File System Management: Handles file operations like reading, writing, and organizing files in a hierarchical structure.
▪ Device Control: Manages interactions with hardware devices (e.g., printers, storage drives).
3. System Security
o Implements security measures to protect data and resources from unauthorized access.
The kernel is always active, working behind the scenes to ensure the system operates smoothly, even when no user programs are running. It is the foundation of all system operations, enabling communication between
hardware and software components.
2. The Shell
The shell is a command-line interpreter that serves as an interface between the user and the kernel. It translates user commands into system calls that the kernel can execute.
Key Functions
1. Interpreter
o Processes user input and translates commands into actions that the kernel performs.
o Example: When a user types ls, the shell translates it into a system call that retrieves the list of files in the directory.
2. Programming Capability
o The shell is also a powerful scripting environment, allowing users to write shell scripts.
▪ Shell Scripts: Text files containing sequences of commands and control structures (e.g., loops, conditionals).
▪ Bash (Bourne Again Shell): Enhanced version of Bourne Shell, widely used in Linux systems.
▪ C Shell (csh): Introduces syntax similar to the C programming language, with added features like command history.
▪ Korn Shell (ksh): Combines features of Bourne and C shells, offering enhanced scripting and performance.
3. Utilities
UNIX includes a wide range of utility programs that perform specialized tasks. These utilities work seamlessly with the shell to provide a powerful and flexible environment.
Categories of Utilities
1. Text Editors
▪ Examples:
2. Search Programs
o Tools for searching text within files using patterns or regular expressions.
▪ Example:
3. Sort Programs
▪ Example:
o Utilities for managing the UNIX system, such as monitoring processes, managing users, and configuring settings.
▪ Examples:
4. Applications
Applications are user-level programs designed for specific tasks. These are not part of the standard UNIX distribution but are built to extend the system’s capabilities.
Examples of Applications
1. Command-Line Tools
2. Software Suites
o Examples:
1. Kernel-Shell Interaction
o The shell translates user commands into system calls, which the kernel executes.
o Example: When a user requests to open a file, the shell communicates with the kernel using the open() system call.
o Utilities enhance the functionality of the shell by providing tools to perform specific tasks.
o Example: Combining utilities with pipes to create complex workflows (e.g., grep 'pattern' file | sort).
o Applications interact indirectly with the kernel through system calls, often mediated by the shell or utility programs.
Summary Figure
• Middle Layer (Shell): Interfaces between users and the kernel, with scripting and command interpretation capabilities.
• Outer Layer (Utilities & Applications): Tools and programs that perform specific tasks, enhancing the system’s usability.
This layered structure ensures flexibility, efficiency, and the modularity that UNIX is known for.
Flexibility of Command Usage in UNIX
UNIX provides great flexibility in using commands, allowing users to work efficiently by combining, splitting, or managing commands in various ways. Here's a detailed explanation in simple terms:
1) Combining Commands
You can run multiple commands in one line by separating them with a semicolon (;). This lets you execute several tasks sequentially without needing to re-enter each command.
Example:
• Explanation:
You can also group multiple commands using parentheses and redirect their combined output to a file.
Example:
• Explanation:
If a command is too long or needs to be spread out for better readability, UNIX allows you to split it across multiple lines. The shell provides a secondary prompt (>) to indicate the command is incomplete.
Example:
echo "This is
• Explanation:
o The command is written in two lines but treated as one logical line.
Long commands that exceed the terminal's width can also overflow naturally, continuing on the next line, while remaining one logical line.
UNIX allows you to type new commands while another long-running program is still executing. These new commands are stored in a buffer (temporary storage) by the kernel. Once the running program finishes, the new
commands are processed.
Example:
$sleep 10
date; cal
• Explanation:
3. Once the 10 seconds pass, the date command runs (showing the current date and time), followed by cal (displaying the calendar).
Conclusion
This flexibility empowers users to handle tasks efficiently, even in complex scenarios.
Internal Commands • Internal commands in Unix/Linux are commands that are built into the shell itself. They are directly executed by the shell without requiring an external executable file. This results in faster execution
because the shell doesn't need to search the file system for the command.
Internal commands are an integral part of the shell and are always available to users, even if the shell is running in minimal environments, such as a recovery or rescue mode.
1. cd (Change Directory)
• Purpose: The cd command changes the current working directory of the shell.
• How it works: The cd command changes the current directory, which is used by the shell to determine the context for file operations (like file creation, editing, etc.). The shell internally keeps track of the current
directory and updates it whenever you use cd.
• Syntax:
cd [directory]
o directory: The path to the directory you want to move to. If not provided, it moves to the home directory by default.
• Example:
$ cd /home/user
$ cd ..
• Key Points:
• Purpose: The echo command is used to display a message or the value of a variable to the terminal. It is one of the most frequently used commands for debugging or displaying output in shell scripts.
• How it works: echo writes its arguments to standard output, usually the terminal. It can print simple strings, or if variables are passed, it will print the value of the variables.
• Syntax:
o options: You can use flags like -n (to avoid newline at the end) or -e (to interpret backslash escapes).
• Example:
Hello, Unix!
$ echo $HOME
/home/user
This will print the value of the $HOME environment variable, which is the current user's home directory.
• Key Points:
o The command does not modify system state or environment; it’s just for output.
• Purpose: The pwd command displays the absolute path of the current working directory, showing where you are in the filesystem.
• How it works: The shell internally tracks the current working directory. When you invoke pwd, it simply returns the path of the directory the shell is currently working in.
• Syntax:
pwd
• Example:
$ pwd
/home/user
• Key Points:
o pwd is very useful in shell scripting or when working with complex directory structures to know exactly where you are.
• Purpose: The exit command is used to terminate the current shell session, effectively closing the terminal window or ending the script.
• How it works: When you invoke exit, the shell session is terminated. In the case of a script, it will stop executing the script at that point.
• Syntax:
exit [status]
o status: (optional) An exit status code (an integer). By convention, an exit status of 0 indicates success, and any non-zero value indicates an error.
• Example:
$ exit
This will close the terminal or logout from the current session.
$ exit 1
This will exit the shell and return the status code 1, indicating an error or abnormal termination.
• Key Points:
1. Speed: Since internal commands do not require an external process to be launched, they are typically faster than external commands.
2. No External Files: These commands do not rely on separate executables, making them available in all environments, even if only the shell is installed.
3. Simplicity: Internal commands are part of the shell, so users don't have to worry about paths, dependencies, or separate executable files.
4. Availability: These commands are always available as long as the shell is running.
Summary
Internal commands are fundamental to shell operation and provide essential functionality for navigation, output display, and session control. They are directly implemented within the shell and are critical for interacting
with the system efficiently, especially for tasks like changing directories, displaying information, and terminating sessions. Understanding these commands is key to mastering shell scripting and Unix/Linux systems.
. cd (Change Directory)
The cd (Change Directory) command is one of the most fundamental and frequently used commands in Unix/Linux shells. It allows the user to navigate between directories within the file system.
Purpose:
The primary purpose of the cd command is to change the current working directory of the shell. The shell keeps track of the current directory, and when the cd command is used, it updates the shell’s reference to that new
directory.
• Command:
cd /home/user
The command changes the current working directory to /home/user. After this, any relative paths used in the shell will be relative to /home/user. For example, using ls will list the contents of /home/user.
• Command:
cd ..
The .. represents the parent directory of the current directory. When you use cd .., it moves the current working directory up one level. For instance, if you are in /home/user/docs, executing cd .. will change the directory to
/home/user.
• Command:
cd
When you use the cd command with no arguments, it automatically takes you to your home directory. The home directory is usually represented by the environment variable $HOME. For example, if your username is
"user," and your home directory is /home/user, using cd without arguments will take you there.
Additional Notes:
• No Output on Success: The cd command does not typically produce any output if it successfully changes the directory.
• Error on Failure: If you try to cd into a directory that does not exist, you will receive an error message,
o Absolute paths begin from the root directory (/). For example, cd /var/log.
o Relative paths begin from the current directory. For example, cd docs (if there's a docs directory in the current directory).
Key Points:
• Navigation: cd - will take you back to the previous directory you were in.
The echo command is used to print text, variables, or the output of commands to the terminal. It’s one of the most commonly used commands for output in shell scripts and terminal usage.
Purpose of echo:
The echo command prints its arguments to the standard output (usually the terminal). It is often used to display text, the contents of variables, and formatted output, as well as to show results from executing other
commands.
• Command:
• Output:
Hello, Unix!
• Command:
NAME="Alice"
• Description: This prints the value of the variable NAME (Alice) to the terminal. You can use variables inside strings by referencing them with $.
• Output:
Copy code
Hello, Alice!
bash
Copy code
• Description: The -e option enables interpretation of escape sequences. Here, \n creates a newline, so the output is displayed on two lines.
• Output:
Line 1
Line 2
• \r: Carriage return (moves the cursor to the beginning of the line).
The echo command can also be used to output to files by redirecting its output using the > or >> operators.
Example:
The pwd command stands for Print Working Directory. It is used to display the full path of the current working directory in the filesystem. This is useful when you want to know where you are in the filesystem or confirm
your location after navigating with the cd command.
Purpose of pwd:
The pwd command shows the absolute path of the current directory you're working in. This path starts from the root directory (/) and includes all directories leading to the current directory.
• Command:
bash
Copy code
pwd
• Description: This prints the full path of the directory you are currently in.
• Output:
arduino
Copy code
/home/user
• Command:
bash
Copy code
cd /var/log
pwd
• Description: After changing the directory using cd, the pwd command will show the new directory path.
• Output:
bash
Copy code
/var/log
This shows that the current directory has been changed to /var/log.
Additional Notes:
• The output of pwd is always an absolute path, starting from the root (/) of the filesystem.
• It does not take any arguments for specific paths, as it only works to show the path of the current directory.
Summary of pwd Co
The exit command is used to terminate the current shell session. When you use this command, the shell will close, and you will return to the previous level (e.g., the terminal window or login session). It is often used when
you are finished with your work in a shell session.
Purpose of exit:
The exit command is used to end a shell session and close the terminal window or the current shell. Additionally, you can specify an exit code to indicate the status of the session or program (commonly used for scripting or
indicating error states).
• Command:
bash
Copy code
exit
• Description: This will close the current shell session and return to the terminal or logout.
• Command:
bash
Copy code
exit 1
• Description: This closes the shell session and returns an exit code 1, which is often used to indicate an error or abnormal termination.
• Output: (No output; the session is closed, and the exit status is set to 1.)
Exit Codes:
Exit codes are useful for scripts or commands where you want to communicate the status of the session or command. The general convention is:
For example:
• exit 1: Indicates failure or some error (commonly used for debugging or error handling).
External Commands • These commands are not built into the shell.
They exist as separate executable files in the file system.
• The shell locates these commands in directories specified by the PATH variable and then executes them.
• Slightly slower compared to internal commands because the shell needs to find and execute the corresponding file.
• If a command has entry in built-in set and has external file also, in such situation internal command takes precedence.
External commands in Unix/Linux are not built into the shell itself but exist as separate executable files stored on the system. These commands are located in directories specified by the PATH environment variable, and
when invoked, the shell searches for and executes these files. While these commands offer additional functionality, they can be slightly slower than internal commands because the shell has to search for the executable file
and launch it.
1. Separate Executable Files: Unlike internal commands (which are built into the shell), external commands are individual files found on the filesystem. These files can be programs or scripts.
2. Location in Directories: The shell searches for external commands in the directories listed in the PATH environment variable. The directories in PATH are a set of locations where executable files are stored, and
the shell looks through these directories when you enter a command.
3. Slower Execution: Since the shell needs to find and execute the corresponding file, external commands can be slightly slower compared to internal commands that are executed directly by the shell without
needing an external executable.
4. Precedence: If a command exists as both an internal and external command (i.e., a built-in shell function and an executable file with the same name), the internal command will take precedence over the external
one. The shell will execute the built-in version instead of searching for the external executable.
Basic Usage:
1. List files and directories in the current directory:
1. Basic Command
• Command: ls
• What it does: Displays the names of files and directories in the current working directory.
• Example Output:
Copy code
file1.txt file2.txt dir1 dir2
5. Recursive Listing
• Command: ls -R
• What it does: Displays all files and directories, including the contents of subdirectories.
• Example Output:
file1.txt dir1
dir1:
subfile1.txt
• Key Point: The -R option shows the contents of subdirectories, making it useful for exploring nested files.
7. Color-Coded Output
• Command: ls --color
• What it does: Adds colors to the output for better identification.
o For example:
▪ Directories might appear in blue.
▪ Executable files might appear in green.
• Key Point: This makes it easier to distinguish between different types of files.
Combining Options
You can combine multiple options for more powerful results:
• Command: ls -alh
o Lists all files, including hidden ones, in a detailed format with human-readable file sizes.
• Command: ls -ltR
o Lists files recursively, sorted by modification time.
Summary of Key Options
Option Description
-a Show all files, including hidden files.
-l Long listing format with detailed file info.
-S Sort files by size.
-R List files and subdirectories recursively.
-h Display human-readable file sizes.
--color Add color-coded output for better visualization.
-t Sort files by modification time.
Summary:
The ls command is a versatile tool for displaying files and directories in Unix/Linux systems. By using different options, you can customize the output to show detailed information, hidden files, file sizes, and even
recursively list contents of subdirectories. It is fundamental for navigating the filesystem and inspecting directory contents efficiently.
Definitions:
• Command: A program or utility executed in UNIX (e.g., ls, echo, cat, find).
• Arguments: These specify the targets or objects the command will operate on, such as filenames, directories, or text. For example, in the command ls /home/user, /home/user is an argument.
• Options: These modify or extend the behavior of a command. Options usually begin with a - (single hyphen) or -- (double hyphen). For example, in the command ls -l, -l is an option that provides a long listing of
files.
General Syntax:
The general syntax for UNIX commands follows this structure:
bash
Copy code
command [options] [arguments]
• command: The utility or program you want to execute (e.g., ls, cat).
• [options]: Flags or switches that alter how the command behaves (e.g., -a, -l).
• [arguments]: Inputs or targets the command operates on, such as file names or directories (e.g., file1.txt, /home/user).
Examples:
• Command: ls
o Lists files and directories in the current directory.
o No arguments: ls
o With arguments: ls /home/user
• Options:
o -a: List all files, including hidden ones (those starting with a dot .).
bash
Copy code
ls -a
o -l: Use long listing format, showing detailed information (permissions, owner, size, date, etc.).
bash
Copy code
ls -l
o -lh: Combine options to show file sizes in a human-readable format.
bash
Copy code
ls -lh
2. echo (Print Text to the Terminal):
• Command: echo
o Prints a text message or variable to the terminal.
o With arguments:
bash
Copy code
echo "Hello, world!"
• Options:
o -n: Prevents the newline character at the end of the output.
bash
Copy code
echo -n "Hello, world!"
3. cat (Concatenate and Display File Content):
• Command: cat
o Displays the contents of one or more files.
o With arguments:
bash
Copy code
cat file1.txt
• Options:
o -n: Displays line numbers along with the file content.
bash
Copy code
cat -n file1.txt
o -b: Displays line numbers but skips empty lines.
bash
Copy code
cat -b file1.txt
4. find (Search for Files and Directories):
• Command: find
o Searches for files and directories within a directory hierarchy.
o With arguments:
bash
Copy code
find /home/user -name "*.txt"
• Options:
o -name: Searches for files by name.
o -type: Specifies the type of file (e.g., f for regular files, d for directories).
bash
Copy code
find /home/user -type f
o -exec: Executes a command on the found files.
bash
Copy code
find /home/user -name "*.txt" -exec cat {} \;
Types of Options:
1. Short Options:
o These options usually have a single character, prefixed with a single hyphen (-).
o Example: -a, -l, -n
Example:
bash
Copy code
ls -al
o This command combines -a and -l to list all files in the long format.
2. Long Options:
o These options are more descriptive and are prefixed with two hyphens (--).
o Example: --all, --long, --help
Example:
bash
Copy code
ls --all --long
3. Combined Options:
o Some commands allow combining short options after a single hyphen.
o Example: ls -al, where -a and -l are combined together.
Conclusion:
In UNIX/Linux, commands can be customized and made more powerful by using various arguments and options. Arguments specify what the command acts upon (like files or directories), and options modify the behavior
of the command (like how output is formatted). Understanding how to structure commands with these components helps in performing complex tasks efficiently.
By combining options and arguments, UNIX commands become highly flexible and powerful.
• Use Spacebar or Enter to move forward.
• Press q to quit the manual page.
You can explore its own manual page by typing man man, which will explain the syntax and usage of the man command itself.
root Account in UNIX:
In UNIX, the root account is the superuser with full administrative privileges, capable of performing any action on the system. Similar to Linux, the root user can:
• Modify system files.
• Install and manage software.
• Manage users and groups.
• Perform system-level maintenance tasks.
Logging into the Root Account in UNIX:
• Login Name: root
• Password: The root user has a dedicated password.
The root account is commonly used for system administration and troubleshooting. However, it is recommended to avoid using root for day-to-day tasks for security reasons.