0% found this document useful (0 votes)
57 views39 pages

UNIX

Uploaded by

shuvadipmondal5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views39 pages

UNIX

Uploaded by

shuvadipmondal5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

UNIX

UNIX operating system


UNIX is a powerful Operating System initially developed by Ken Thompson, Dennis Ritchie at
AT&T Bell laboratories in 1970. It is prevalent among scientific, engineering, and academic
institutions due to its most appreciative features like multitasking, flexibility, and many
more. In UNIX, the file system is a hierarchical structure of files and directories where users
can store and retrieve information using the files.

Architecture of UNIX Operating System


The UNIX operating system is divided into 4 prominent layers that help define the
interaction between the hardware and the user. As we move from layer 1 to layer 4, we
move away from hardware towards the software. UNIX OS consists of a hardware
layer, kernel layer, shell layer, and applications layer. These layers together create a
multiuser, multitasking operating system. The following diagram shows a pictorial
representation of the layers in the UNIX operating system.

1. Hardware Layer:
It contains the hardware-related information required for the functioning of
the UNIX environment.
2. Kernel Layer:
The core (commonly known as the heart) of the operating system. It is a software
application that acts as the interface between the hardware and the user.
The kernel is responsible for handling the major functionality of the Unix Operating
System including process, memory, file, network, etc.
Functions of the Kernel Layer:
o Ensures that all the system and user tasks are executed concurrently.
o Acts as a device manager helping processes gain access to peripheral
devices connected to the computer with the help of device drivers .
o Manages memory using techniques like paging, swapping, and virtual storage.
3. Shell Layer:
The interface between the user and kernel. It is a program
that interprets/translates commands typed into the terminal into a series of
commands that can be sent to the shell. This script containing commands is called
a shell script. The shell is what keeps a history of commands typed in by the user. To
repeat a command previously typed, you can simply click the scroll-up key and you
will get access to the older commands.
UNIX uses multiple shells including Bourne Shell (sh), C shell (csh), Korn shell (ksh), etc. The
initial shell that the user logs into is defined by the system administrator. The user can
change the default shell by using the 'chsh' command.
4. Application Programs Layer:
The outermost layer that includes programs that are accessed by command on the
command line. It executes external applications like word processors and graphic
programs . Although the earlier Command line was the only way to access them,
now GUI can also be used.

Kernel -
The core of the operating system that's liable for maintaining the full functionality is named
the kernel. The kernel of UNIX runs on the particular machine hardware and interacts with
the hardware effectively.
It also works as a device manager and performs valuable functions for the processes which
require access to the peripheral devices connected to the computer. The kernel controls
these devices through device drivers.
The kernel also manages the memory. Processes are executed programs that have owner's
humans or systems who initiate their execution.
The system must provide all processes with access to an adequate amount of memory, and
a few processes require a lot of it. To make effective use of main memory and to allocate a
sufficient amount of memory to every process. It uses essential techniques like paging,
swapping, and virtual storage.

Shell -
The Shell is an interpreter that interprets the command submitted by the user at the
terminal, and calls the program you simply want.
It also keeps a history of the list of the commands you have typed in. If you need to repeat a
command you typed it, use the cursor keys to scroll up and down the list or type history for
a list of previous commands. There are various commands like cat, mv, cat, grep, id, wc, and
many more.
Types of Shell in UNIX System:
o Bourne Shell: This Shell is simply called the Shell. It was the first Shell for UNIX OS. It
is still the most widely available Shell on a UNIX system.
o C Shell: The C shell is another popular shell commonly available on a UNIX system.
The C shell was developed by the University of California at Berkeley and removed
some of the shortcomings of the Bourne shell.
o Korn Shell: This Shell was created by David Korn to address the Bourne Shell's user-
interaction issues and to deal with the shortcomings of the C shell's scripting quirks.

Linux Files & Processes -


Everything in Unix is either a file or a process.
Process
When you run a program, a process is created. Every process is identified by a number
called process ID. To check the processes you are running, execute "ps" command on the
shell. You can think of the process ID to be a sequence number given by the operating
system. It may be different at different execution of the same program.
File
A file is a sequence of data. A file could be created by users using word processors or text
editors or by the program to keep the information. A program is kept in the form of a file
and when it is run by the kernel, it loads as a process.
A file is generally written on the disk so that it exists even after the computer restarts. It is
saved in a disk - either hard disk drive (HDD - cheaper and slower) or solid state drive (SSD -
faster but costlier).
A file is identified by a name called file path. In Unix, everything is represented as file:
1. Devices such as Mouse, Keyboard
2. Programs are saved as file
3. Disk and Monitor

Unix System Calls -


System calls in Unix are used for file system control, process control, interprocess
communication etc. Access to the Unix kernel is only available through these system calls.
Generally, system calls are similar to function calls, the only difference is that they remove
the control from the user process.
There are around 80 system calls in the Unix interface currently. Details about some of the
important ones are given as follows -
System Call Description
access() This checks if a calling process has access to the required file
chdir() The chdir command changes the current directory of the system
chmod() The mode of a file can be changed using this command
chown() This changes the ownership of a particular file
kill() This system call sends kill signal to one or more processes
link() A new file name is linked to an existing file using link system call.
open() This opens a file for the reading or writing process
pause() The pause call suspends a file until a particular signal occurs.
stime() This system call sets the correct time.
times() Gets the parent and child process times
alarm() The alarm system call sets the alarm clock of a process
fork() A new process is created using this command
chroot() This changes the root directory of a file.
exit() The exit system call is used to exit a process.
Features of UNIX Operating System:
Let's discuss the features of UNIX OS one by one in detail.

Multitasking: A UNIX operating system is a multitasking operating system that allows you to
initiate more than one task from the same terminal so that one task is performed as a
foreground and the other task as a background process.
Multi-user: UNIX operating system supports more than one user to access computer
resources like main memory, hard disk, tape drives, etc. Multiple users can log on to the
system from different terminals and run different jobs that share the resources of a
command terminal. It deals with the principle of time-sharing. Time-sharing is done by a
scheduler that divides the CPU time into several segments also called a time slice, and each
segment is assigned to each user on a scheduled basis. This time slice is tiny. When this time
is expired, it passes control to the following user on the system. Each user executes their set
of instructions within their time slice.
Portability: This feature makes the UNIX work on different machines and platforms with the
easy transfer of code to any computer system. Since a significant portion of UNIX is written
in C language, and only a tiny portion is coded in assembly language for specific hardware.
File Security and Protection: Being a multi-user system, UNIX makes special consideration
for file and system security. UNIX has different levels of security using assigning username
and password to individual users ensuring the authentication, at the level providing file
access permission viz. read, write and execute and lastly file encryption to change the file
into an unreadable format.
Command Structure: UNIX commands are easy to understand and simple to use. Example:
"cp", mv etc. While working in the UNIX environment, the UNIX commands are case-
sensitive and are entered in lower case.
Communication: In UNIX, communication is an excellent feature that enables the user to
communicate worldwide. It supports various communication facilities provided using the
write command, mail command, talk command, etc.
Open Source: UNIX operating system is open source it means it is freely available to all and
is a community-based development project.
Accounting: UNIX keeps an account of jobs created by the user. This feature enhances the
system performance in terms of CPU monitoring and disk space checking. It allows you to
keep an account of disk space used by each user, and the disk space can be limited by each
other. You can assign every user a different disk quota. The root user can perform these
accounting tasks using various commands such as quota, df, du, etc.
ADVERTISEMENT
UNIX Tools and Utilities: UNIX system provides various types of tools and utilities facilities
such as UNIX grep, sed and awk, etc. Some of the general-purpose tools are compilers,
interpreters, network applications, etc. It also includes various server programs which
provide remote and administration services.

Posix -
POSIX (Portable Operating System Interface) is a family of standards specified by the IEEE for
maintaining compatibility between operating systems. POSIX defines the application
programming interface (API), along with command line shells and utility interfaces, for
software compatibility with variants of Unix and other operating systems.
Key Points about POSIX:
1. Standardization: POSIX is designed to ensure that software developed for one POSIX-
compliant operating system can be compiled and run on another POSIX-compliant
system with minimal modifications.
2. API Specification: POSIX provides a set of standardized APIs that applications can use
to interact with the operating system. These APIs include system calls for file
operations, process control, and inter-process communication.
3. Shell and Utilities: POSIX defines a standard command-line interface and utility
programs. This includes standard utilities like ls, grep, awk, and a standard shell
(usually the Bourne shell or a compatible shell).
4. Portability: The primary goal of POSIX is to facilitate the portability of applications
across different UNIX-like operating systems. This is crucial for developers who aim to
write software that can run on various platforms without needing to be rewritten for
each one.
5. Versions and Compliance: POSIX standards are published in various versions.
Compliance with POSIX can vary, with some operating systems being fully compliant
and others only partially so. Some notable POSIX-compliant operating systems include
Linux, macOS, and many versions of UNIX.
6. Modular Approach: The POSIX standards are modular, allowing different parts of the
specification to be implemented independently. This modularity enables operating
systems to support only the necessary parts of the standard relevant to their use
case.

Single-User Mode -
1. Purpose:
• Single-user mode is used primarily for system maintenance tasks, such as
repairing file systems, resetting passwords, or performing backup and restore
operations. It provides an environment where the system administrator has
direct access to the system without interference from other users or services.
2. Characteristics:
• Minimal Environment: The system boots into a minimal environment with only
essential services running. Typically, it mounts the root filesystem in read-only
mode to ensure system stability.
• No Networking: Network services are usually disabled, reducing the risk of
remote access during sensitive operations.
• Single User: Only the root user has access to the system in this mode. Other
user logins are disabled.
• Direct Access: The root user has direct access to the command line interface
(CLI), allowing for administrative tasks without the normal multi-user
environment overhead.
3. Booting into Single-User Mode:
• Manual Intervention: Typically, entering single-user mode requires manual
intervention during the boot process. This can be done by modifying the boot
parameters at the bootloader stage (e.g., GRUB) to specify the runlevel for
single-user mode.
• Runlevel: Traditionally, Unix-like systems use runlevels to define modes of
operation. Single-user mode corresponds to runlevel 1 in many Unix System V-
based systems. Modern systems using systemd refer to this as "rescue.target"
or "emergency.target."
Internal Commands : Commands which are built into the shell. For all the shell built-in
commands, execution of the same is fast in the sense that the shell doesn’t have to search
the given path for them in the PATH variable, and also no process needs to be spawned
for executing it.
Examples: source, cd, fg, etc.

External Commands : Commands which aren’t built into the shell. When an external
command has to be executed, the shell looks for its path given in the PATH variable, and
also a new process has to be spawned and the command gets executed. They are usually
located in /bin or /usr/bin. For example, when you execute the “cat” command, which
usually is at /usr/bin, the executable /usr/bin/cat gets executed.
Examples: ls, cat etc.

Basic Commands -
Calendar (cal) - The cal command is used to display a calendar in the terminal. By default,
it shows the current month's calendar.
Example:
To display the current month's calendar:
cal
To display a specific year's calendar:
cal 2024
To display a specific month's calendar of a specific year:
cal 5 2024

Display system date (date) - The date command is used to display or set the system
date and time.
Example:
To display the current date and time:
date

Message display (echo) - The echo command is used to display a line of text or a
variable value.
Example:
To display a simple message:
echo "Hello, World!"

Calculator (bc) - The bc command is a calculator that supports arbitrary precision


arithmetic.
Example:
To start bc in interactive mode:
bc
5+3

Password changing (passwd) - The passwd command is used to change the user's
password.
Example:
To change your own password:
passwd
You will be prompted to enter your current password and then the new password.

Knowing who are logged in (who) - The who command displays a list of users who are
currently logged into the system.
Example:
To see who is logged in:
who

System information using uname - The uname command prints system information,
such as the operating system name, kernel version, and more.
Example:
To display the system's kernel name:
uname
To display detailed system information:
uname -a
File name of terminal connected to the standard input (tty) - The tty command
displays the file name of the terminal connected to the standard input.
Example:
To find out the terminal file name:
Tty

File System - A file system is a method and data structure that the operating system uses
to manage files on a disk or partition. It determines how data is stored and retrieved.
Without a file system, data placed in a storage medium would be one large body of data
with no way to tell where one piece of information stops and the next begins.

Types of Files
Types of Unix Files
The UNIX files system contains several different types of files

Ordinary Files
An ordinary file is a file on the system that contains data, text, or program
instructions.
• Used to store your information, such as some text you have written or an image you
have drawn. This is the type of file that you usually work with.
• Always located within/under a directory file.
• Do not contain other files.
• In long-format output of ls -l, this type of file is specified by the “-” symbol.
Directories
Directories store both special and ordinary files. For users familiar with Windows or
Mac OS, UNIX directories are equivalent to folders. A directory file contains an entry
for every file and subdirectory that it houses. If you have 10 files in a directory, there
will be 10 entries in the directory. Each entry has two components. (1) The Filename
(2) A unique identification number for the file or directory (called the inode number)
• Branching points in the hierarchical tree.
• Used to organize groups of files.
• May contain ordinary files, special files or other directories.
• Never contain “real” information which you would work with (such as text). Basically,
just used for organizing files.
• All files are descendants of the root directory, ( named / ) located at the top of the
tree.
In long-format output of ls –l , this type of file is specified by the “d” symbol.

Special Files
Used to represent a real physical device such as a printer, tape drive or terminal, used
for Input/Output (I/O) operations. Device or special files are used for device
Input/Output(I/O) on UNIX and Linux systems. They appear in a file system just like an
ordinary file or a directory. On UNIX systems there are two flavors of special files for
each device, character special files and block special files :
• When a character special file is used for device Input/Output(I/O), data is transferred
one character at a time. This type of access is called raw device access.
• When a block special file is used for device Input/Output(I/O), data is transferred in
large fixed-size blocks. This type of access is called block device access.
For terminal devices, it’s one character at a time. For disk devices though, raw access
means reading or writing in whole chunks of data – blocks, which are native to your
disk.
• In long-format output of ls -l, character special files are marked by the “c” symbol.
• In long-format output of ls -l, block special files are marked by the “b” symbol.

Pipes
UNIX allows you to link commands together using a pipe. The pipe acts a temporary
file which only exists to hold data from one command until it is read by another.A
Unix pipe provides a one-way flow of data.The output or result of the first command
sequence is used as the input to the second command sequence. To make a pipe, put
a vertical bar (|) on the command line between two commands.For example: who |
wc -l In long-format output of ls –l , named pipes are marked by the “p” symbol.

Sockets
A Unix socket (or Inter-process communication socket) is a special file which allows
for advanced inter-process communication. A Unix Socket is used in a client-server
application framework. In essence, it is a stream of data, very similar to network
stream (and network sockets), but all the transactions are local to the filesystem. In
long-format output of ls -l, Unix sockets are marked by “s” symbol.

Symbolic Link
Symbolic link is used for referencing some other file of the file system.Symbolic link is
also known as Soft link. It contains a text form of the path to the file it references. To
an end user, symbolic link will appear to have its own name, but when you try reading
or writing data to this file, it will instead reference these operations to the file it
points to. If we delete the soft link itself , the data file would still be there.If we delete
the source file or move it to a different location, symbolic file will not function
properly. In long-format output of ls –l , Symbolic link are marked by the “l” symbol
(that’s a lower case L).

Advantages of the Unix file System


• Hierarchical organization: The hierarchical structure of the Unix file system makes it
easy to organize and navigate files and directories.
• Robustness: The Unix file system is known for its stability and reliability. It can handle
large amounts of data without becoming unstable or crashing.
• Security: The Unix file system uses a set of permissions that allows administrators to
control who has access to files and directories.
• Compatibility: The Unix file system is widely used and supported, which means that
files can be easily transferred between different Unix-based systems.

Disadvantages of the Unix file System


• Complexity: The Unix file system can be complex to understand and manage,
especially for users who are not familiar with the command line interface.
• Steep Learning Curve: Users who are not familiar with Unix-based systems may find it
difficult to learn how to use the Unix file system.
• Lack of User-Friendly Interface: The Unix file system is primarily managed through
the command line interface, which may not be as user-friendly as a graphical user
interface.
• Limited Support for Certain File Systems: While the Unix file system is compatible
with many file systems, there are some file systems that are not fully supported.

File Naming Convention - File naming conventions vary between different operating
systems, but there are common guidelines:
• Length: Most modern systems allow long filenames, but historically they were limited
to 8.3 format (eight characters, a period, and a three-character extension).
• Case Sensitivity: Some systems (e.g., UNIX, Linux) are case-sensitive, meaning file.txt
and File.txt are different files. Others (e.g., Windows) are not.
• Allowed Characters: Avoid special characters like \, /, :, *, ?, ", <, >, | to maintain
compatibility across systems.
• Extensions: Used to indicate the file type (e.g., .txt for text files, .jpg for image files).

Parent-Child Relationship - In a hierarchical file system, directories can contain files and
other directories. The directory that contains another directory is called the parent
directory, and the contained directory is called the child directory. This relationship creates
a tree structure, where the root directory is the topmost parent.

HOME Variable - The HOME variable in UNIX-like operating systems is an environment


variable that represents the current user's home directory. It can be used to reference the
user's home directory without having to type out the full path. For example, echo $HOME
prints the path to the user's home directory.

Inode Number - An inode (index node) is a data structure on a filesystem on UNIX-like


operating systems that stores information about a file or a directory except its name or its
actual data. Information stored in an inode includes:
• File type
• Permissions
• Owner
• Group
• Size
• Timestamps (creation, modification, access)
• Link count
• Pointers to data blocks where the file’s data is stored
Each file is identified by an inode number.

Absolute Pathname - An absolute pathname is a file or directory location in relation to


the root directory, denoted by /. It provides the complete path from the root to the desired
file or directory. For example, /home/user/documents/file.txt is an absolute path.

Relative Pathname - A relative pathname describes the location of a file or directory in


relation to the current working directory. It does not start with /. For example, if the current
directory is /home/user, the relative path to file.txt in the documents directory would be
documents/file.txt.

UNIX File System


Significance of dot (.) and dotdot (..) - In Unix, the dot (.) and dotdot (..) are special
directory entries:
• . (dot): Represents the current directory. It is used to execute commands or scripts in
the current directory. For example, if you have a script named script.sh in the current
directory, you can run it using ./script.sh.
• .. (dotdot): Represents the parent directory. It is used to navigate up one level in the
directory structure. For example, to move to the parent directory, you use the
command cd ...

Displaying Pathname of the Current Directory (pwd) - The pwd command stands
for "print working directory." It displays the full pathname of the current directory.
Example:
$ pwd /home/user

Changing the Current Directory (cd) - The cd command is used to change the current
directory.
Examples:
$ cd /home/user/Documents $ cd .. $ cd ~ # Changes to the home directory

Make Directory (mkdir) - The mkdir command creates a new directory.


Example:
$ mkdir new_directory

Remove Directories (rmdir) - The rmdir command removes an empty directory.


Example:
$ rmdir new_directory
To remove a non-empty directory, you would use the rm -r command instead.

Listing Contents of Directory (ls) - The ls command lists the contents of a directory.
Examples:
$ ls
file1.txt file2.txt directory
$ ls -l # Detailed listing
total 4
-rw-r--r-- 1 user user 0 May 31 12:00 file1.txt
-rw-r--r-- 1 user user 0 May 31 12:00 file2.txt
drwxr-xr-x 2 user user 4096 May 31 12:00 directory

Important File Systems of Unix


• /bin: Contains essential command binaries (executables) like ls, cp, and mv. These
commands are needed for the system to operate in single-user mode and for all
users.
• /usr/bin: Contains the majority of user commands. It is typically where user-installed
software goes.
• /sbin: Contains system binaries. These are essential for system administration, such
as fdisk and ifconfig.
• /usr/sbin: Contains non-essential system binaries used by the system administrator,
like httpd and sshd.
• /etc: Contains configuration files for the system. This includes system-wide
configuration files and shell scripts that start at boot time.
• /dev: Contains device files. These are special files that represent hardware devices,
such as /dev/sda for a hard disk.
• /lib: Contains shared library files necessary for the binaries in /bin and /sbin.
• /usr/lib: Contains libraries for /usr/bin and /usr/sbin binaries.
• /usr/include: Contains standard include files used in C programming, such as headers
for the standard C library.
• /usr/share/man: Contains manual pages (documentation) for commands and system
calls.
• /tmp: Used for temporary files. Files in this directory may be deleted when the
system is rebooted.
• /var: Contains variable data files, such as logs, mail spools, and databases.
• /home: Contains home directories for all users. Each user has a subdirectory within
/home for personal files.

Ordinary File Handling


Displaying and Creating Files (cat) - Displays the contents of a file and can also be used
to concatenate multiple files.
Example:
cat file1.txt cat file1.txt file2.txt > combined.txt

Copying a File (cp) - Copies files or directories.


Example:
cp source.txt destination.txt

Deleting a File (rm) - Deletes files or directories.


Example:
rm unwanted.txt
Renaming/Moving a File (mv) - Moves or renames files or directories.
Example:
mv oldname.txt newname.txt mv file.txt /new/location/file.txt

Paging Output (more) - Displays file contents one screen at a time.


Example:
more largefile.txt

Printing a File (lp) - Sends a file to the printer.


Example:
lp document.txt

Knowing File Type (file) - Determines the type of a file.


Example:
file example.txt

Line, Word, and Character Counting (wc) - Counts lines, words, and characters in a
file.
Example:
wc file.txt wc -l file.txt # counts lines
wc -w file.txt # counts words
wc -c file.txt # counts characters

Comparing Files (cmp) - Compares two files byte by byte.


Example:
cmp file1.txt file2.txt
Finding Common Between Two Files (comm) - Compares two sorted files line by line
and outputs the lines that are common.
Example:
comm file1.txt file2.txt

Displaying File Differences (diff) - Displays the differences between two files line by
line.
Example:
diff file1.txt file2.txt

Creating Archive File (tar) - Archives multiple files into a single file.
Example:
tar -cvf archive.tar file1.txt file2.txt

Compress File (gzip) - Compresses files.


Example:
gzip file.txt

Uncompress File (gunzip) - Uncompresses files compressed by gzip.


Example:
gunzip file.txt.gz

Archive File (zip) - Compresses and archives files.


Example:
zip archive.zip file1.txt file2.txt

Extract Compressed File (unzip) - Extracts files from a zip archive.


Example:
unzip archive.zip
Effects of cp, rm, and mv on Directories
Copying Directories (cp -r) - Recursively copies directories.
Example:
cp -r sourcedir/ destinationdir/
Removing Directories (rm -r) - Recursively deletes directories and their contents.
Example:
rm -r directory/
Moving/Renaming Directories (mv) - Moves or renames directories.
Example:
mv olddir/ newdir/ mv directory/ /new/location/directory/

File Attributes
File and Directory Attributes Listing - To list file and directory attributes in Linux, you
typically use the ls command with various options. The most commonly used options are:
• ls -l: Lists files and directories with detailed attributes including permissions, number
of links, owner, group, size, and modification date.
• ls -a: Lists all files, including hidden files.
• ls -lh: Lists files with human-readable file sizes.
Example :
ls -l
-rw-r--r-- 1 user group 4096 May 1 12:34 example.txt
Brief Idea About the Attributes

1. File Type and Permissions: The first column shows file type and permissions.
❖ -rw-r--r--: A regular file with read and write permissions for the owner, read
permissions for the group, and read permissions for others.
2. Number of Links: The second column shows the number of hard links to the file.
3. File Ownership: The third and fourth columns show the owner and group of the file.
4. File Size: The fifth column shows the size of the file in bytes.
5. Modification Date: The sixth column shows the last modification date and time of the
file.
6. File Name: The last column shows the name of the file.

File Ownership - Each file in Linux has an owner and an associated group. The
ownership is set when the file is created. The owner is usually the user who created the file,
and the group is typically the primary group of that user.

File Permissions - File permissions in Linux are divided into three categories:
1. Owner Permissions: Permissions for the user who owns the file.
2. Group Permissions: Permissions for the group that owns the file.
3. Other Permissions: Permissions for all other users.

Permissions are represented by:


• r: Read permission
• w: Write permission
• x: Execute permission

Changing File Permissions


Relative Permissions - Relative permissions use symbolic notation to modify
permissions. The chmod command is used for this purpose.
• chmod u+x file: Adds execute permission for the owner.
• chmod g-w file: Removes write permission for the group.
• chmod o=r file: Sets read-only permission for others.

Absolute Permissions - Absolute permissions use octal notation to set permissions. Each
permission category (owner, group, others) is represented by a digit from 0 to 7.
• chmod 755 file: Sets permissions to rwxr-xr-x (read, write, execute for owner; read,
execute for group and others).
• chmod 644 file: Sets permissions to rw-r--r-- (read, write for owner; read for group
and others).

Changing File Ownership - The chown command is used to change the ownership of a
file.
• chown user file: Changes the owner of the file to user.
• chown user:group file: Changes the owner and group of the file to user and group.
Changing Group Ownership
The chgrp command is used to change the group ownership of a file.
• chgrp group file: Changes the group of the file to group.

Examples :
1. Listing file attributes:
ls -l
2. Changing file permissions (relative):
chmod u+x file.txt chmod g-w file.txt
3. Changing file permissions (absolute):
chmod 755 file.txt chmod 644 file.txt
4. Changing file ownership:
chown newuser file.txt chown newuser:newgroup file.txt
5. Changing group ownership:
chgrp newgroup file.txt

File System and Inodes:


• File System: A file system organizes and stores files on storage devices. It provides a
way to manage how data is stored and retrieved.
• Inodes: An inode (index node) is a data structure on a filesystem that stores
information about a file or a directory (except its name or its actual data). It includes:
• File type (regular file, directory, etc.)
• Permissions (read, write, execute)
• Owner and group IDs
• File size
• Timestamps (creation, modification, access)
• Link count (number of hard links)
• Pointers to the data blocks where the actual data is stored.

Hard Link and Soft Link :


❖ Hard Link:
• A hard link is a directory entry that associates a name with an inode. Multiple
hard links can associate with the same inode, meaning multiple filenames can
point to the same file.
• If the original file is deleted, the data remains accessible through the hard link.
• Cannot link directories and cannot link across filesystems.
• Command: ln existingfile hardlinkname

❖ Soft Link (Symbolic Link):


• A soft link is a file that points to another file or directory by name. It acts as a
shortcut or alias.
• If the original file is deleted, the soft link becomes a "dangling" link and is
broken.
• Can link directories and can link across filesystems.
• Command: ln -s targetfile symlinkname

Significance of File Attribute for Directory - Directories have special attributes that
control access:
• r (read): Allows listing the contents of the directory.
• w (write): Allows creating, deleting, or renaming files in the directory.
• x (execute): Allows accessing the directory and its content (needed for cd).

Default Permissions of File:


❖ Typically, the default permission for new files is rw-r--r-- (644).
❖ The default permission for new directories is rwxr-xr-x (755).

Directory and Using umask :


❖ umask sets the default permission for newly created files and directories by masking
out permission bits.
❖ Command: umask 022 (removes write permissions for group and others, resulting in
files with 644 and directories with 755 permissions).

Listing of Modification and Access Time:


❖ ls -l: Lists files with modification time.
❖ ls -lu: Lists files with access time.
❖ ls -lc: Lists files with status change time.

Time Stamp Changing (touch):


❖ The touch command is used to update the access and modification times of a file to
the current time. If the file does not exist, it creates an empty file.
❖ Command: touch filename

File Locating (find) - The find command searches for files in a directory hierarchy based
on criteria such as name, type, size, and modification time.
Examples:
❖ Find by name: find /path -name filename
❖ Find by type: find /path -type d (directories), find /path -type f (files)
❖ Find by size: find /path -size +100M (files larger than 100MB)
❖ Find by modification time: find /path -mtime -7 (files modified within the last 7 days)

Interpretive Cycle of Shell


The shell in UNIX is an interpreter that executes commands typed by the user. The
interpretive cycle involves the shell reading a command, interpreting its meaning, executing
it, and then returning to the prompt for the next command. This cycle is fundamental to the
interactive nature of UNIX.

Types of Shell
There are several types of shells in UNIX, each with its own features and syntax:
• Bourne Shell (sh): The original UNIX shell.
• C Shell (csh): Introduces a syntax similar to the C programming language.
• Korn Shell (ksh): Combines features of both the Bourne and C shells.
• Bash (Bourne Again Shell): An improved version of the Bourne shell, widely used in
Linux.

Pattern Matching - Pattern matching in UNIX is used to find files or text that match a
certain pattern. Wildcards like * (matches any number of characters) and ? (matches a
single character) are commonly used.

ls *.txt

Escaping - Escaping is used to give special characters their literal meaning. This is done
using the backslash (\).
echo "This is a dollar sign: \$"

Quoting - Quoting prevents the shell from interpreting special characters. Single quotes (')
preserve the literal value of each character within the quotes. Double quotes (") allow
variable and command substitution but prevent wildcard expansion.
echo 'This is a quoted string with $dollar signs' echo "This is a quoted string with a
variable: $HOME"

Redirection - Redirection allows you to change the input and output sources for
commands. The symbols > and >> are used to redirect output to a file, and < is used to
redirect input from a file.
echo "Hello, World!" > output.txt cat < input.txt

Standard Input, Output, and Error - UNIX programs use three standard streams:
• Standard Input (stdin): File descriptor 0.
• Standard Output (stdout): File descriptor 1.
• Standard Error (stderr): File descriptor 2.
command > output.txt 2> error.txt
/dev/null and /dev/tty
• /dev/null: A special file that discards all data written to it.
• /dev/tty: Represents the terminal for the current process.
command > /dev/null 2>&1 echo "Enter input:" > /dev/tty

Pipe - Pipes (|) are used to pass the output of one command as input to another.
ls -l | grep "Jan"

Tee - tee reads from standard input and writes to both standard output and one or more
files.
ls -l | tee filelist.txt

Command Substitution - Command substitution allows the output of a command to be


used as an argument in another command. This is done using backticks (`) or $(...).
echo "Today is $(date)"

Shell Variables - Shell variables store data that can be used by the shell and other
programs.
MY_VAR="Hello, World!" echo $MY_VAR

Processes
Basic Idea about UNIX Process - A process in UNIX is an instance of a running program.
Each process has a unique process ID (PID).

Display Process Attributes (ps) - The ps command displays information about active
processes.
ps -ef

Display System Processes - The top command displays real-time system processes.
top
Process Creation Cycle - Processes are created using the fork system call, which creates
a child process. The exec family of functions is then used to replace the child's memory
space with a new program.
Shell Creation Steps
The shell is started in the following steps:
1. init: The first process started by the kernel.
2. getty: Prepares for login.
3. login: Authenticates the user.
4. shell: Starts the user’s shell.
Process State and Zombie State - Processes can be in various states, such as running,
sleeping, stopped, or zombie. A zombie process is one that has completed execution but still
has an entry in the process table.

Reduce Priority (nice) - The nice command runs a command with a modified scheduling
priority.
nice -n 10 command

Using Signals to Kill Process - The kill command sends a signal to a process, usually to
terminate it.
kill -9 PID

Background and Foreground Jobs - bg sends a job to the background, and fg brings it
to the foreground.
bg %1 fg %1

Listing Jobs - The jobs command lists all jobs in the current session.
jobs

Suspend Job
Press Ctrl+Z to suspend a job.

Kill a Job - The kill command terminates a job.


kill %1
Execute at Specified Time (at and batch) - The at command schedules commands to
be run at a specific time, and batch schedules them based on system load.
echo "command" | at 5pm echo "command" | batch

Customization
Environment Variables - Environment variables are dynamic values that can affect the
behavior of running processes on a computer. They are part of the environment in which a
process runs and can be used to pass configuration settings to applications and scripts.

Common Environment Variables


1. HOME:
• Description: The current user's home directory.
• Example: /home/username
• Usage: cd $HOME changes the current directory to the user's home directory.
2. PATH:
• Description: A colon-separated list of directories that the shell searches
through to find executable files for commands.
• Example: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
• Usage: Adding a directory to PATH: export PATH=$PATH:/new/directory
3. LOGNAME:
• Description: The login name of the user.
• Example: username
• Usage: Echoing the logname: echo $LOGNAME
4. USER:
• Description: The current logged-in user's name.
• Example: username
• Usage: echo $USER prints the username.
5. TERM:
• Description: The type of terminal to emulate when running the shell.
• Example: xterm-256color
• Usage: Useful for terminal-based applications to determine terminal
capabilities.
6. PWD:
• Description: The current working directory.
• Example: /home/username/projects
• Usage: echo $PWD displays the present working directory.
7. PS1:
• Description: The primary prompt string (the default command prompt).
• Example: \u@\h:\w\$ (shows username@hostname$)
• Usage: Customizing the prompt: PS1="\[\e[32m\]\u@\h \[\e[34m\]\w
\$\[\e[m\] "
8. PS2:
• Description: The secondary prompt string (used for multiline commands).
• Example: >
• Usage: Customizing the secondary prompt: PS2="> "
Aliases
Aliases are shortcuts or abbreviations for longer commands. They can simplify the use of
the command line by allowing you to create custom commands.
• Creating an alias:
alias ll='ls -la'
This command creates an alias ll for the ls -la command.
• Removing an alias:
unalias ll
This command removes the alias ll.
• Listing all aliases:
alias
This command lists all currently defined aliases.
Command History
Command history is a feature that records commands entered by the user in the shell,
allowing for easy reuse and reference.
• Viewing command history:
history
This command displays the list of previously executed commands.
• Executing a command from history:
!n
This command re-executes the command at position n in the history list.
• Searching command history:
• Incremental search: Press Ctrl+R and type part of the command to search
backwards through history.
• Clearing command history:
history -c
This command clears the command history.
• Saving command history: By default, command history is saved in a file named
.bash_history in the user's home directory.
• Configuring history:
• HISTSIZE: Number of commands to remember in the history list.
• HISTFILESIZE: Number of lines to keep in the history file.
export HISTSIZE=1000 export HISTFILESIZE=2000

Filters
Prepare File for Printing (pr) - The pr command is used to format a file for printing. It
adds headers, footers, and paginates the text.
Example:
pr myfile.txt
This command formats myfile.txt with headers, footers, and page numbers.

Custom Display of File Using head and tail


• head: Displays the first few lines of a file.
• tail: Displays the last few lines of a file.
Examples:
head -n 5 myfile.txt
This command shows the first 5 lines of myfile.txt.
tail -n 5 myfile.txt
This command shows the last 5 lines of myfile.txt.

Vertical Division of File (cut) - The cut command is used to extract sections from each
line of a file.
Example:
cut -d ',' -f 1,3 myfile.csv
This command extracts the 1st and 3rd fields from myfile.csv, assuming fields are separated
by commas.

Paste Files (paste) - The paste command merges lines of files side-by-side.
Example:
paste file1.txt file2.txt
This command merges lines of file1.txt and file2.txt horizontally.

Sort File (sort) - The sort command sorts the lines of a file.
Example:
sort myfile.txt
This command sorts the lines in myfile.txt in alphabetical order.

Finding Repetition and Non-repetition (uniq) - The uniq command reports or filters
out repeated lines in a file. It requires sorted input.
Example:
sort myfile.txt | uniq
This command sorts myfile.txt and then removes duplicate lines.
Manipulating Characters Using (tr) - The tr command translates or deletes characters.
Example:
tr 'a-z' 'A-Z' < myfile.txt
This command translates all lowercase letters to uppercase in myfile.txt.

Searching Pattern Using grep - The grep command searches for patterns in files using
basic regular expressions.
Example:
grep "pattern" myfile.txt
This command searches for "pattern" in myfile.txt.

Basic Regular Expression (BRE) - BRE uses simple regex patterns for searching.
Example:
grep "^[a-z]" myfile.txt
This command searches for lines starting with a lowercase letter in myfile.txt.

Extended Regular Expression (ERE) and egrep - ERE supports more complex patterns
and can be used with grep -E or egrep.
Examples:
grep -E "a{2,3}" myfile.txt
This command searches for occurrences of 'a' repeated 2 or 3 times in myfile.txt.
egrep "a{2,3}" myfile.txt
This command does the same using egrep.

Shell Script
Simple Shell Scripts - A simple shell script is a sequence of commands written in a file.
Example:
#!/bin/bash
echo "Hello, World!"
Save the above script in a file named hello.sh and make it executable using chmod +x
hello.sh. Run it with ./hello.sh.

Interactive Shell Scripts - Interactive shell scripts require user input.


Example:
#!/bin/bash
echo "Enter your name:"
read name
echo "Hello, $name!"
Save it as interactive.sh, make it executable, and run with ./interactive.sh. It will prompt
you to enter your name and then greet you.

Using Command Line Arguments - You can pass arguments to a script.


Example:
#!/bin/bash
echo "First argument: $1"
echo "Second argument: $2"
Save it as args.sh, make it executable, and run with ./args.sh arg1 arg2.

Logical Operators (&&, ||) - Logical operators help in combining commands.


Example:
#!/bin/bash mkdir
mydir && cd mydir || echo "Failed to create and navigate to mydir"
This script creates a directory and navigates into it if successful; otherwise, it prints an error
message.

Condition Checking (if, case) - You can use if and case for condition checking.
Example:
#!/bin/bash
if [ $1 -gt 10 ]; then
echo "The first argument is greater than 10"
else
echo "The first argument is 10 or less"
fi
case $2 in
start)
echo "Starting..."
;;
stop)
echo "Stopping..."
;;
*)
echo "Unknown command"
;;
esac
Save it as condition.sh, make it executable, and run with ./condition.sh 15 start.

Expression Evaluation (test, []) - The test command or [] is used for evaluating
expressions.
Example:
#!/bin/bash
if [ -e /etc/passwd ];
then
echo "File exists"
else
echo "File does not exist"
fi
Save it as test.sh, make it executable, and run with ./test.sh.
Computation (expr) - The expr command is used for arithmetic computations.
Example:
#!/bin/bash
result=$(expr 5 + 3)
echo "5 + 3 = $result"
Save it as computation.sh, make it executable, and run with ./computation.sh.

Using expr for Strings - The expr command can also manipulate strings.
Example:
#!/bin/bash
length=$(expr length "hello")
substring=$(expr substr "hello world" 1 5)
echo "Length of 'hello' is $length"
echo "Substring of 'hello world' is $substring"
Save it as strings.sh, make it executable, and run with ./strings.sh.

Loop (while, for) - Loops help in repeating commands.


Example:
#!/bin/bash
count=1
while [ $count -le 5 ];
do
echo "Count: $count"
count=$(expr $count + 1)
done
for i in 1 2 3 4 5;
do
echo "Number: $i"
done
Save it as loops.sh, make it executable, and run with ./loops.sh.

Use of Positional Parameters - Positional parameters are used to handle command line
arguments.
Example:
#!/bin/bash
echo "Script name: $0"
echo "First argument: $1"
echo "Second argument: $2"
echo "All arguments: $@"
Save it as positional.sh, make it executable, and run with ./positional.sh arg1 arg2.

System Administration
Essential Duties of a UNIX System Administrator - A UNIX System Administrator is
responsible for maintaining the operation and security of UNIX systems.
Their duties include:
1. System Installation and Configuration: Install and configure UNIX operating systems
and software.
2. User Account Management: Create, manage, and delete user accounts.
3. System Monitoring: Monitor system performance and resource usage.
4. Backup and Recovery: Plan and execute regular backups and restore systems when
necessary.
5. Security Management: Implement security policies, manage access controls, and
apply patches and updates.
6. Troubleshooting: Diagnose and resolve hardware, software, and network issues.
7. Scripting and Automation: Develop and maintain scripts for automating
administrative tasks.
8. Documentation: Maintain documentation related to system configurations,
procedures, and policies.
9. Support: Provide technical support to users and address their issues.
Starting and Shutdown of UNIX Systems
1. Starting (Booting) a UNIX System:
• Power On: Turn on the physical or virtual machine.
• BIOS/UEFI Initialization: The BIOS/UEFI firmware initializes hardware
components.
• Bootloader Execution: The bootloader (GRUB, LILO) is loaded from the boot
sector and allows the selection of the operating system or kernel version to
load.
• Kernel Loading: The selected kernel is loaded into memory and initializes
system components.
• init/Systemd Initialization: The init or systemd process (the first process with
PID 1) is started, which sets up user-space services and mounts file systems.
• Runlevel/Target Execution: Services corresponding to the specified runlevel
(SysV init) or target (systemd) are started.
2. Shutting Down a UNIX System:
• Command Execution: Use commands like shutdown, poweroff, halt, or reboot
to initiate shutdown.
• Example: shutdown -h now (halts the system immediately)
• Service Termination: The system terminates running services and processes.
• File System Unmounting: Filesystems are synced and unmounted to prevent
data corruption.
• Kernel Shutdown: The kernel halts the system or reboots, depending on the
command issued.

User Account Management


1. Username: A unique identifier for a user.
• Stored in /etc/passwd.
• Example entry: john:x:1001:1001:John Doe:/home/john:/bin/bash
• Username: john
2. Password: Authentication credential for a user.
• Encrypted and stored in /etc/shadow.
• Example entry: john:$6$saltsalt$hashhashhash:18295:0:99999:7:::
• passwd command is used to change passwords.
3. Home Directory: The directory where a user’s personal files and configuration
settings are stored.
• Specified in /etc/passwd.
• Example: /home/john
4. Group ID (GID): Identifies the primary group of a user.
• Stored in /etc/passwd and /etc/group.
• Example entry in /etc/group: users:x:1001:john,jane
• groupadd, groupmod, groupdel commands manage groups.
5. Disk Quota: Limits on the amount of disk space a user or group can use.
• Managed using quota and edquota commands.
• Example commands:
• quota -u john (checks disk usage and limits for user john)
• edquota -u john (edits disk quota for user john)
6. Terminal: The interface through which a user interacts with the system.
• Users can connect via local terminals or remote sessions (SSH).
• Terminal settings can be configured in .bashrc or .profile in the user’s home
directory.

Example Commands
• Adding a User: useradd john
• Setting a Password: passwd john
• Deleting a User: userdel john
• Modifying a User: usermod -d /new/home/john john (changes home directory)
• Checking Disk Quota: quota -u john

Configuration Files
1. /etc/passwd: Contains user account information.
• Format: username:password:UID:GID:GECOS:home_directory:shell
2. /etc/shadow: Contains encrypted passwords and password expiration information.
• Format:
username:encrypted_password:last_changed:min:max:warn:inactive:expire
3. /etc/group: Contains group information.
• Format: group_name:password:GID:user_list

You might also like