Module 5
Module 5
Module 5
The architecture of Linux is composed of kernel, shell and application programs that is
software.
5.2Types of shells
1. The Bourne Shell –
It is denoted as sh
It was written by Steve Bourne at AT&T Bell Labs. It is the original UNIX shell.
It is faster and more preferred. It lacks features for interactive use like the ability
to recall previous commands. It also lacks built-in arithmetic and logical
expression handling. It is default shell for Solaris OS. For the Bourne shell the:
It is denoted as ksh
It is denoted as csh
4. Bash Shell –
Ex:
#!/bin/bash
Shell Scripting
Basic Linux Commands
File and Directory Commands:
1. ls: List directory contents.
o Syntax: ls [options] [directory]
o Example: ls -l /home/user
2. cd: Change the current directory.
o Syntax: cd [directory]
o Example: cd /home/user
3. pwd: Print the working directory.
o Syntax: pwd
o Example: pwd
4. mkdir: Create a new directory.
o Syntax: mkdir [directory]
o Example: mkdir new_folder
5. rmdir: Remove an empty directory.
o Syntax: rmdir [directory]
o Example: rmdir old_folder
6. rm: Remove files or directories.
o Syntax: rm [options] [file/directory]
o Example: rm file.txt
o Example: rm -r directory_name
7. cp: Copy files or directories.
o Syntax: cp [options] source destination
o Example: cp file.txt /home/user/backup/
o Example: cp -r folder_name /home/user/backup/
8. mv: Move or rename files or directories.
o Syntax: mv [source] [destination]
o Example: mv file.txt /home/user/new_location/
o Example: mv old_name.txt new_name.txt
9. cat: Concatenate and display file content (including creating a file, editing a file, and
displaying a file).
o Syntax: cat [options] [file]
o Example (Creating a file): cat > newfile.txt
(Then type the content and press Ctrl+D to save)
o Example (Editing a file): cat >> existingfile.txt
(Then add the content and press Ctrl+D to save)
o Example (Displaying a file): cat file.txt
10. head: Display the beginning of a file.
o Syntax: head [options] [file]
o Example: head -n 10 file.txt
11. tail: Display the end of a file.
o Syntax: tail [options] [file] Example: tail -n 10 file.txt
File Permissions in Linux:
In Linux, each file and directory have a set of permissions that determine who can read,
write, or execute them. These permissions are divided into three categories:
1. Owner: The user who owns the file.
2. Group: A group of users who share the same permissions.
3. Others: All other users on the system.
Each category can have three types of permissions:
Read (r): Permission to read the contents of the file or list the directory.
Write (w): Permission to modify the contents of the file or create/delete files in the
directory.
Execute (x): Permission to execute the file as a program or search the directory.
Viewing File Permissions
To view the permissions of a file or directory, you can use the ls -l command. This command
lists the contents of a directory in long format, showing detailed information about each file,
including its permissions.
Syntax: ls -l [directory]
Example: ls -l /home/user
The output looks like this:
-rwxr-xr—
Control Statements
Control statements are used to alter the flow of the script based on conditions. In shell
scripting, the primary control statements are if, if-else, if-elif-else, case, and exit.
1. If Statement
The if statement is used to test a condition, and if the condition is true, the script will execute
a block of commands.
Syntax:
if [ condition ]
then
# commands to execute if condition is true
fi
Example:
#!/bin/bash
AGE=20
if [ $AGE -ge 18 ]
then
echo "You are an adult."
fi
2 If-Else Statement
The if-else statement allows you to specify what should happen if the condition is true and
also what should happen if it is false.
Syntax:
if [ condition ]
then
# commands to execute if condition is true
Else
# commands to execute if condition is false
fi
Example:
#!/bin/bash
AGE=16
if [ $AGE -ge 18 ]
then
else
3 If-Elif-Else Statement
The if-elif-else statement is used when there are multiple conditions to check. If the first
condition is false, the script checks the second condition (elif), and if none of the conditions
are true, it executes the else block.
Syntax:
if [ condition1 ]
then
then
else
Example:
#!/bin/bash
NUMBER=5
if [ $NUMBER -gt 10 ]
then
then
else
2. Looping Statements
Syntax:
for variable in list
do
# commands to execute
done
example:
#!/bin/bash
for i in 1 2 3 4 5
do
System Calls:
Basic system calls (e.g., open, read, write, close, fork, exec, wait, exit)
Process management (fork, exec, wait, kill)
exec: Executes a new program in the current process space (as mentioned
above).
wait: Waits for a child process to finish execution (as mentioned above).
kill: Sends a signal to a process or a group of processes.
Linux security involves a combination of practices, tools, and technologies to protect the
operating system and data from unauthorized access, vulnerabilities, and threats. Below are
key aspects of Linux security:
1. User and Group Management
Users and Groups: Linux uses users and groups to manage permissions and access
control. Users are individual accounts, while groups are collections of users.
o User Management Commands:
useradd, usermod, userdel for managing users.
passwd for changing passwords.
o Group Management Commands:
groupadd, groupmod, groupdel for managing groups.
gpasswd for managing group passwords.
2. File Permissions and Ownership
Permission Types: Read (r), write (w), and execute (x) permissions.
o Permission Levels: User (owner), group, and others.
Commands:
o chmod: Changes file permissions.
o chown: Changes file ownership.
o chgrp: Changes group ownership.
Special Permissions:
o Setuid: Allows users to run an executable with the file owner's privileges.
o Setgid: Allows users to run an executable with the file group's privileges.
o Sticky Bit: Used on directories to restrict file deletion.