2. Syllabus
Unit 1 : Introduction
Unit 2 : Simple Unix Commands
Unit 3 : Files and Directories
Unit 4 : Processes and Process Management
Unit 5 : Shell Programming Basics
3. Definition of an Operating System
(OS)
An operating system is a control program for a
computer that performs the following
operations:
Allocates computer resources
Schedules routine tasks
Provides a platform to run application software for
users to accomplish tasks
Provides an interface between the user & the
computer
5. Built-in task scheduling
Whenever using a UNIX-based operating
system, certain tasks are to be performed
repeatedly. Running them manually every
single time is time-consuming and overall
inefficient. To solve this issue, UNIX comes
with its built-in task schedulers. These task
schedulers act like a smart alarm clock. When
the alarm goes off, the operating system will
run the predefined task.
6. History of Unix OS
The Unix OS was developed by Ken Thompson
at the AT&T Bell Laboratories in 1969. He
wanted to create a multi-user operating system
to run “space wars” game.
Bell Labs researchers who had worked on
Multics (Ken Thompson, Dennis Ritchie,
Douglas McIlroy, Joseph Ossanna, and others)
still wanted to develop an operating system for
their own and Bell Labs’ programming, job
control, and resource usage needs.
7. History of Unix OS
The first versions of UNIX were written in
“machine-dependent” program
Ken Thompson approached Dennis Ritchie,
developer of C language, and in 1973 they
compiled UNIX in C to make operating system
“portable” to other computers systems.
8. History of Unix
Ken Thompson
(recently retired
from Bell Labs)
is on left, and
Dennis Ritchie is
in the middle.
9. Development of Unix OS
Unix became a popular OS among institutions
such as colleges & universities through a 4-year
“try before you buy” deal.
Efficient and inexpensive way of networking
promotes Internet use and file-sharing
Open system allows for source code to be shared
among many programmers - allows for better
coordination among programmers
10. Development of Unix OS
Students at University of California (in Berkley)
further developed the UNIX operating system
and introduced the BSD version of Unix
Unix
Bell Labs
UNIX System V (5)
Proprietary
Berkley Software Distribution (BSD)
Free
11. Development of Unix OS
There were versions of UNIX for the Personal
Computer (PC), such as XENIX, etc., but they
didn’t catch on in popularity until Linux was
developed in the early 90’s.
12. Development of Unix OS
Unix V6, released in 1975 became very
popular. Unix V6 was free and was distributed
with its source code.
In 1983, AT&T released Unix System V which
was a commercial version.
Meanwhile, the University of California at
Berkeley started the development of its own
version of Unix. Berkeley was also involved in
the inclusion of Transmission Control
Protocol/Internet Protocol (TCP/IP) networking
protocol.
13. Major milestones in UNIX history
in early 1980's
AT&T was developing its System V Unix.
Berkeley took initiative on its own Unix BSD (Berkeley
Software Distribution) Unix.
Sun Microsystems developed its own BSD-based
Unix called SunOS and later was renamed to Sun
Solaris.
Microsoft and the Santa Cruz operation (SCO) were
involved in another version of UNIX called XENIX.
Hewlett-Packard developed HP-UX for its
workstations.
DEC (Digital Equipment Corporation) released
ULTRIX.
In 1986, IBM developed AIX (Advanced Interactive
14. History of Linux
Linux operating system
developed by
programming student
Linus Torvalds
Linus wanted to develop
Unix-like OS just to
experiment with new
386 computer at the
time...
15. Why Has Linux Become so
Popular?
Linus decided to make Linux OS
source-code for Linux Kernel open to all:
Unlike traditional Operating Systems, anyone can
modify and distribute Linux OS (as long as they
distribute source code of Linux Kernel)
“Competition among Hackers” allow code to be
improved and distributed often
Many users can spot bugs in the operating system or
application if source code is “open”
16. Why Has Linux Become so
Popular?
Other Factors:
PC’s have increased processing power and a there
has been a noted shift from mainframes and
minicomputers to PCs.
Since Linux is a “Unix Work-alike”, this OS has a
reputation to be a very stable platform for networking
(creating at-home servers) and running / maintaining
applications.
Agencies such as Free Software Foundation created
GNU project to provide free software.
17. Concerns
Some people claim that “there are as many
version of Linux as there are users…”
POSIX (Portable Operating System Interface for
Computer Environments) is a government
standard to ensure consistency among different
UNIX and Linux versions.
Many versions of Linux are approaching POSIX
standard.
18. UNIX History
The UNIX operating system was born in the late 1960s.
It originally began as a one man project led by Ken
Thompson of Bell Labs, and has since grown to become
the most widely used operating system.
In the time since UNIX was first developed, it has gone
through many different generations and even mutations.
Some differ substantially from the original version, like Berkeley
Software Distribution (BSD) or Linux.
Others, still contain major portions that are based on the original
source code.
An interesting and rather up-to-date timeline of these
variations of UNIX can be found at
http://www.levenez.com/unix/history.html.
19. General Characteristics of UNIX as an
Operating System (OS)
Multi-user & Multi-tasking - most versions of UNIX are
capable of allowing multiple users to log onto the
system, and have each run multiple tasks. This is
standard for most modern OSs.
Over 30 Years Old - UNIX is over 30 years old and it's
popularity and use is still high. Over these years, many
variations have spawned off and many have died off, but
most modern UNIX systems can be traced back to the
original versions. It has endured the test of time. For
reference, Windows at best is half as old (Windows 1.0
was released in the mid 80s, but it was not stable or
very complete until the 3.x family, which was released in
the early 90s).
Large Number of Applications – there are an
enormous amount of applications available for UNIX
operating systems. They range from commercial
applications such as CAD, Maya, WordPerfect, to many
free applications.
20. General Characteristics of UNIX as
an Operating System (OS)
Free Applications and Even a Free Operating
System - of all of the applications available under UNIX,
many of them are free. The compilers and interpreters
that we use in most of the programming courses here at
UMBC can be downloaded free of charge. Most of the
development that we do in programming courses is
done under the Linux OS.
Less Resource Intensive - in general, most UNIX
installations tend to be much less demanding on system
resources. In many cases, the old family computer that
can barely run Windows is more than sufficient to run
the latest version of Linux.
Internet Development - Much of the backbone of the
Internet is run by UNIX servers. Many of the more
general web servers run UNIX with the Apache web
server - another free application.
21. Parts of the UNIX OS
The Kernel - handles memory management, input and output
requests, and program scheduling. Technically speaking, the kernel
is the OS. It provides the basic software connection to the
hardware. The kernel is very complex and deals with the inner
workings of these things, and is beyond the scope of this course.
The kernel is covered in detail in “Operating Systems.”
The Shell and Graphical User Interfaces (GUIs) - basic UNIX
shells provides a “command line” interface which allows the user to
type in commands. These commands are translated by the shell
into something the kernel can comprehend, and then executed by
the kernel.
The Built-in System Utilities - are programs that allow a user to
perform tasks which involve complex actions. Utilities provide user
interface functions that are basic to an operating system, but which
are too complex to be built into the shell. Examples of utilities are
programs that let us see the contents of a directory, move & copy
files, remove files, etc...
Application Software & Utilities – these are not part of the
operating system, per se. They are additional programs that are
bundled with the OS distribution, or available separately. These can
range from additional or different versions of basic utilities, to full
scale commercial applications.
22. Flavors of UNIX
These can be grouped into two categories: Open Source and Proprietary
Proprietary: (redistribution and modification prohibited or restricted; not free)
Solaris - Access to a Solaris UNIX server (solaris.gl.umbc.edu) via SSH access.
IRIX - There are a couple of IRIX machines in the basement of the library, as well as
server access (irix.gl.umbc.edu) via SSH access.
Mac OS X - no access currently available at UMBC.
and many others...
Open Source: (source code is readily available and free to modify)
FreeBSD - no access currently available at UMBC.
Linux Distributions - access is available in the form of dual-boot PCs scattered
throughout the Engineering building. There are also several Linux servers
(linux.gl.umbc.edu) through which access is available.
RedHat (used by UMBC) and the Fedora Project (maintained by RedHat)
Mandrake
Debian
SuSE
Slackware
and many others...
As a side note, Linux is a open source UNIX-based OS that was originally developed in
1991 by Linus Torvalds, a Finnish undergraduate student.
23. The birth of LINUX
In 1991 Linus Torvalds a graduate student in
Denmark developed a system as part of his
PhD work which he called LINUX.
It was a simpler variant of UNIX.
It is now the third most popular operating
systems in the world after Windows and Mac.
24. The birth of LINUX
LINUX has been implemented on many
platforms and or different tasks from standard
desktop use to high-level servers.
LINUX is more suited to desktop operations
than UNIX which is mainly server level.
The main reason is in the utilities that have
been developed.
25. Architecture/Structure of Unix
OS
Layer-1: Hardware –
It consists of all hardware related information.
Layer-2: Kernel –
The kernel is the heart of the operating system. It interacts
with the hardware and most of the tasks like memory
management, task scheduling and file management.
Layer-3: Shell commands –
The shell is the utility that processes your requests. When you
type in a command at your terminal, the shell interprets the
command and calls the program that you want. The shell uses
standard syntax for all commands. C Shell, Bourne Shell and
Korn Shell are the most famous shells which are available
with most of the Unix variants.
Layer-4: Application Layer –
It is the outermost layer that executes the given external
applications.
26.
27. Salient Features of Unix
Multiuser:It is a multi-user system where the same
resources can be shared by different users.
Multitasking:It provides multi-tasking, wherein each user can
execute many processes at the same time.
Portability:It was the first operating system that was written
in a high-level language (C Language). This made it easy to
port to other machines with minimum adaptations.
It provides a hierarchical file structure which allows easier
access and maintenance of data.
Unix has built-in networking functions so that different
users can easily exchange information.
Machine independent, Security, Pipes and filters, Unix
shell programming
28. Difference between Linux &Unix
Linux OS Unix OS
Linux is development by an active Linux Community
worldwide.
Unix refers to the original operating system
developed by AT&T.
Original code developed by Linus and the GNU
Foundation
Original code developed by AT & T
The source code of Linux is freely available to its
users
The source code of Unix is not freely available
general public
It has graphical user interface along with command
line interface
It only has command line interface
Linux OS can be installed on various types of devices
like mobile, tablet computers.
The UNIX operating system is used for internet
servers, workstations & PCs.
Different version of Linux are Ubuntu, Linux Mint,
RedHat, Solaris, etc.
Different version of Unix are AIS, HP-UX, BSD, Iris,
etc.
The file systems supported by Linux are as follows:
xfs, ramfs, vfat, cramfsm, ext3, ext4, ext2, ext1, ufs,
autofs, devpts, ntfs
The file systems supported by Unix are as follows:
zfs, js, hfx, gps, xfs, vxfs
29. Few more Differences
Unix is expensive, Linux is free
Linux default shell is BASH while the Unix shell is
Bourne Shell.
Linux threat detection and solution are very fast
while Unix users require longer wait times to get the
proper bug fixing patch.
30. What is Shell?
A Shell provides you with an interface to the
Unix system. It gathers input from you and
executes programs based on that input. When
a program finishes execution, it displays that
program's output.
Shell is an environment in which we can run
our commands, programs, and shell scripts.
There are different flavors of a shell, just as
there are different flavors of operating
systems. Each flavor of shell has its own set of
recognized commands and functions.
31. Shell Prompt
The prompt, $, which is called the command
prompt, is issued by the shell. While the prompt
is displayed, you can type a command.
Shell reads your input after you press Enter. It
determines the command you want executed by
looking at the first word of your input. A word is an
unbroken set of characters. Spaces and tabs
separate words.
Following is a simple example of
the date command, which displays the current
date and time −
$date
Thu Jun 25 08:30:19 MST 2009
32. Shell Types
The following sections describe OS shells mostly available on
UNIX/Linux Operating system. Shell features and their default
prompts are also described.
1. The Bourne Shell
The Bourne shell (sh), written by Steve Bourne at AT&T Bell
Labs, is the original UNIX shell. It is the preferred shell for
shell programming because of its compactness and speed. A
Bourne shell drawback is that it lacks features for interactive
use, such as the ability to recall previous commands (history).
The Bourne shell also lacks built-in arithmetic and logical
expression handling.
The Bourne shell is the Solaris OS default shell. It is the
standard shell for Solaris system administration scripts. For
the Bourne shell the:
Command full-path name is /bin/sh
Non-root user default prompt is $.
33. 2. The C Shell
The C shell (csh):
Is a UNIX enhancement written by Bill Joy at the
University of California at Berkeley.
Incorporated features for interactive use, such
as aliases and command history.
Includes convenient programming features, such
as built-in arithmetic and a C-like expression
syntax.
For the C shell the:
Command full-path name is /bin/csh.
Non-root user default prompt is hostname %.
34. 3. The Korn Shell
Was written by David Korn at AT&T Bell Labs
Is a superset of the Bourne shell.
Supports everything in the Bourne shell.
Has interactive features comparable to those in the C
shell.
Includes convenient programming features like built-in
arithmetic and C-like arrays, functions, and string-
manipulation facilities.
Is faster than the C shell.
Runs scripts written for the Bourne shell.
For the Korn shell the:
Command full-path name is /bin/ksh.
35. 4. The Bourne-Again Shell
The Bourne-Again shell (bash)
Is compatible to the Bourne shell.
Incorporates useful features from the Korn and
C shells.
Has arrow keys that are automatically mapped
for command recall and editing.
For the Bourne-Again shell the:
Command full-path name is /bin/bash.
36. The UNIX Environment
The UNIX operating system is made up of
three parts; the kernel, the shell and the file
system
37. The kernel
It is the hub of the operating system: it allocates time and
memory to programs and handles the file store and
communications in response to system calls.
Kernel is loaded whenever the system is started - referred to
as a boot of the system.
It manages the entire resources of the system, presenting
them to you and every other user as a coherent system.
Amongst the functions performed by the kernel are:
Managing the machine's memory and allocating it to each
process.
Scheduling the work done by the CPU so that the work of each
user is carried out as efficiently as is possible.
Organizing the transfer of data from one part of the machine to
another.
Accepting instructions from the shell and carrying them out.
Enforcing the access permissions that are enforced on the file
system.
38. The Shell
The shell acts as an interface between the user and the
kernel.
Whenever you login to a Unix system you are placed in a
program called the shell.
To get your work done, you enter commands at this prompt.
The shell acts as a command line interpreter (CLI); it takes
each command and passes it to the operating system kernel
to be acted upon.
It then displays the results of this operation on your screen.
The shell provides you with one or more of the following
features. You can:
Create an environment that meets your needs
Write shell scripts
Automatically complete the command line
Edit the command line
39. File System
It is a logical method for organizing and storing large amounts
of information in a way which makes it easy to manage.
The file system is the smallest unit in which information is
stored.
The UNIX file system has several important features:
Different types of files
In fact, the UNIX file system contains several types of files.
Ordinary File: This type of file is 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.
Directory File: A directory is a file that holds other files and
other directories. You can create directories in your home
directory to hold files and other sub-directories.
Having your own directory structure gives you a definable place
to work from and allows you to structure your information in a way
40. File system
File is a container for storing content, records
or information
Everything in Unix is considered as a file,
including physical devices DVD, USB devices,
etc
Input and output devices are also considered
to be file in unix system
Every file in Unix has a unique name and
unique inode number.
In Unix everything is a file, if something is not
a file then it is process
41. File System
File attributes are kept in a separate location in
the disk. For example programs, services,
texts, images
File contains in time iterator i.e. size, when it
was created, version etc
42. Directories and sub-direcories
Directory within a directory is called a sub-
directory.
Each directory can contain multiple directories
and or files.
The structure of directories having sub-
directories along with files are called as tree or
recursive structure.
All types of operations possible for directories
and files such as create, delete, copy, move,
print
43. Directories and sub-directories
A directory can be a file but a file cannot be
directory as file contains information or records
but directory contains files and sub-directories
44. Different types of files
Directories which you create belong to you - you are said to "own" them
- and you can set access permissions to control which other users can
have access to the information they contain.
Device files: This type of file is used to represent a real physical
device such as a printer, tape drive or terminal.
It may seem unusual to think of a physical device as a file, but it allows
you to send the output of a command to a device in the same way that
you send it to a file. For example:
cat scream.au > /dev/audio
This sends the contents of the sound file scream.au to the file /dev/audio
which represents the audio device attached to the system.
The directory /dev contains the special files which are used to represent
devices on a UNIX system.
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.
45. Structure of the File System
Your home directory
Your current directory
Pathnames
Access permissions
46.
47. UNIX File System
Unix file system is a logical method of organizing
and storing large amounts of information in a way
that makes it easy to manage. A file is a smallest
unit in which the information is stored. Unix file
system has several important features. All data in
Unix is organized into files. All files are organized
into directories. These directories are organized
into a tree-like structure called the file system.
Files in Unix System are organized into multi-level
hierarchy structure known as a directory tree. At
the very top of the file system is a directory
called “root” which is represented by a “/”. All
other files are “descendants” of root.
48. UNIX File System
The Unix file system has a hierarchical (or tree-like) structure
with its highest level directory called root (denoted by /,
pronounced slash). Immediately below the root level directory
are several subdirectories, most of which contain system
files. Below this can exist system files, application files,
and/or user data files. Similar to the concept of the process
parent-child relationship, all files on a Unix system are related
to one another. That is, files also have a parent-child
existence. Thus, all files (except one) share a common
parental link, the top-most file (i.e. /) being the exception.
Below is a diagram (slice) of a "typical" Unix file system. As
you can see, the top-most directory is / (slash), with the
directories directly beneath being system directories. Note
that as Unix implementations and vendors vary, so will this
file system hierarchy. However, the organization of most file
systems is similar.
50. UNIX File System
While this diagram is not all-inclusive, the following system files (i.e.
directories) are present in most Unix file systems:
bin - short for binaries, this is the directory where many commonly
used executable commands reside e.g. ls, cp
dev - contains device specific files
etc - contains system configuration files
home - contains user directories and files
lib - contains all library files
mnt - contains device files related to mounted devices (USB
storage, DVD or removable devices
51. UNIX File System
proc - contains files related to system
processes
root - the root users' home directory (note
this is different than /)
sbin - system binary files reside here. If
there is no sbin directory on your system,
these files most likely reside in etc
tmp–storage for temporary files which are
periodically removed from the file system
usr - also contains executable commands
52. Structure of Unix Commands
A command is a program that tells the Unix system to do something. It has
the form:command [options] [arguments]
Here the argument indicates on what the command is to perform its
action, usually a file or series of files. An option modifies the command,
changing the way it performs.
Commands are case sensitive. command and Command are not the
same.
Options are generally preceded by a hyphen (-), and for most commands,
more than one option can be strung together, in the form:
command -[option][option][option]
e.g.:
ls -alR
will perform a long list on all files in the current directory and recursively
perform the list through all sub-directories.
For most commands you can separate the options, preceding each with a
hyphen, e.g.:
command -option1 -option2 -option3
as in:
ls -a -l -R
53. Types of Unix Commands
UNIX commands are classified into two types
Internal Commands – Ex: cd, pwd, echo etc
External Commands - Ex: ls, cat, mv etc
type utility can be used to check whether a
command is internal or external.
If the command is internal, the output will say that
the command is shell built-in.
If the command is external, the output will give
you the path to the command.
54. Internal Commands
These commands are built into the shell
Execution of these commands is fast as the
shell does not have to search for the given
path for them in PATH variable.
No process needs to be spawned for
executing it.
55. Spawned
Spawn in computing refers to a function that
loads and executes a new child process.
The current process may wait for the child to
terminate or may continue to execute
concurrent computing.
Creating a new subprocess requires enough
memory in which both the child process and
the current program can execute.
56. External Commands
They are not built into the shell
When user fires external command then the
shell looks for its path given in the PATH
variable
A new process has to be spawned and the
command gets executed
They are usually located in /bin or /usr/bin
The executable /usr/bin/cat gets executed
External commands are stored as separate
files
57. Structure of Unix Command
Command –(options) argument1,
argument2, …
The first part, the command, tells the
computer what sort of operation is being done.
The second part- The list of options
changes the way commands operate. Some
options might change the format of the
output or allow a command to work on an
entire directory of files instead of individual
files.
58. Pwd, touch, file
Pwd- – print working directory- It displays the
directory in which we are currently
Touch- The ‘$ touch’ command creates a
file(not directory) and you can simple add an
extension such as .txt after it to make it a Text
File.
File- The ‘$ file’ command displays the type of
file.
Ex. $ file helloworld
helloworld: ASCII text
Ex. $ file vk
vk: directory
59. Structure of Unix Command
The third part- An argument is any piece of
additional information that might be necessary
to execute the command. This is often the path
to a file or folder, but it can be any piece of
information (such as a Unity ID or perhaps a
date). In many cases, arguments are required.
There can be more than one argument for a
command, and arguments must always be
supplied in the order they are expected.
60. Echo
It is an internal command.
Shell won’t look in its PATH to locate it.
Shell will execute it from its own set of built-in
commands.
Echo by default prints data on new line
61. Printf
It is an internal command
It requires new line character when data needs
to be printed on new line
It works the same way as in c language
62. How to check whether the
command is internal or external?
$ type cd
Cd is shell built-in
$ type cat
Cat is /usr/bin/cat
63. Tee command
tee command reads the standard input and
writes it to both the standard output and one or
more files.
It does both the tasks simultaneously, copies
the result into the specified files or variables
and also display the result.
65. Cmp
cmp command in Linux/UNIX is used to
compare the two files byte by byte and helps
you to find out whether the two files are
identical or not.
When cmp is used for comparison between
two files, it reports the location of the first
mismatch to the screen if difference is found
and if no difference is found i.e the files
compared are identical.
cmp displays no message and simply returns
the prompt if the files compared are identical.
66. Cmp
vrush@VRUSHALI-PC ~/vk
$ cat data.txt dt.txt
MIT
Garware
Symbiosys
MIT
Garware
Symbiosys
Bytco College
SNDT College
vrush@VRUSHALI-PC ~/vk
$ cmp data.txt dt.txt
cmp: EOF on data.txt after byte 22, line 3
67. Comm
comm compare two sorted files line by line
and write to standard output; the lines that are
common and the lines that are unique.
It requires two sorted files which it compares
line by line.
69. uniq
The uniq command is used to remove
duplicate lines from a text file in Linux. By
default, this command discards all but the first
of adjacent repeated lines, so that no output
lines are repeated.
Optionally, it can instead only print duplicate
lines. For uniq to work, you must first sort the
output.
71. Sort
The Unix sort command is a simple command that
can be used to rearrange the contents of text files
line by line.
The command is a filter command that sorts the
input text and prints the result to stdout. By
default, sorting is done line by line, starting from
the first character.
Numbers are sorted to be ahead of letters.
Lowercase letters are sorted to be ahead of
uppercase letters.
72. Sort
sort -b: Ignore blanks at the start of the line.
sort -r: Reverse the sorting order.
sort -o: Specify the output file.
sort -n: Use the numerical value to sort.
sort -M: Sort as per the calendar month
specified.
75. Internal and External command
Internal Command: Internal commands are something
which is built into the shell. For the shell built in
commands, the execution speed is really high. It is
because no process needs to be spawned for executing
it. For example, when using the "cd" command, no
process is created. The current directory simply gets
changed on executing it.
External Command: External commands are not built
into the shell. These are executables present in a
separate file. When an external command has to be
executed, a new process has to be spawned and the
command gets executed. For example, when you
execute the "cat" command, which usually is at /usr/bin,
the executable /usr/bin/cat gets executed.
76. Few Basic Unix Commands
ls
mkdir
cd
pwd
cp
mv
rm
rmdir
cat
less
more
head
tail
grep
wc
wild cards
man
echo
who
cal
printf
date
77. ls command
The ls is the list command in Linux. It will show
the full list or content of your directory.
But if you want your files to be displayed in a
long list format, then you can use ls -l
command.
If you don't want to display the owner
information in your list, then you can exclude
this column with the help of ls -g command.
Linux ls ~ command shows the contents of the
home directory. Let us see the example of ls ~
command.
To list invisible files, specify the -a option to ls.
78. ls -l
Columns above indicate specific things:
• Column 1 indicates information regarding
file permission.
• Column 2 indicates the number of links to
the file.
• Column 3 & 4 indicates the owner and
group information.
• Column 5 indicates size of the file in bytes.
• Column 6 shows the date and time on
which the file was recently modified.
• Column 7 shows the file or directory name.
79. wc command
Counting Words in a File: You can use the wc command to get a
count of the total number of lines, words, and characters contained
in a file.
Following is the simple example to see the information about above
created file:
$ wc filename
2 19 103 filename
Here is the detail of all the four columns:
1. First Column: represents total number of lines in the file.
2. Second Column: represents total number of words in the file.
3. Third Column: represents total number of bytes in the file. This is
actual size of the file
4. Fourth Column: represents file name
81. Home Directory
The directory in which you find yourself when you first login is
called your home directory.
You will be doing much of your work in your home directory
and subdirectories that you'll be creating to organize your
files.
You can go in your home directory anytime using the
following command −
$cd ~
$
Here ~ indicates the home directory. Suppose you have to go
in any other user's home directory, use the following
command −
$cd ~username
$
To go in your last directory, you can use the following
command −
$cd –
82. Absolute/Relative Pathnames
Directories are arranged in a hierarchy with root
(/) at the top. The position of any file within the
hierarchy is described by its pathname.
Elements of a pathname are separated by a /. A
pathname is absolute, if it is described in relation
to root, thus absolute pathnames always begin
with a /.
Following are some examples of absolute
filenames.
/etc/passwd
/users/sjones/chem/notes
/dev/rdsk/Os3
83. A pathname can also be relative to your current
working directory. Relative pathnames never begin
with /. Relative to user amrood's home directory, some
pathnames might look like this −
chem/notes
personal/res
• To determine where you are within the
filesystem hierarchy at any time, enter the
command pwd to print the current working
directory −
$pwd
/user0/home/amrood
$
84. The directories . (dot) and .. (dot
dot)
The filename . (dot) represents the current
working directory; and the filename .. (dot dot)
represents the directory one level above the
current working directory, often referred to as
the parent directory.
If we enter the command to show a listing of
the current working directories/files and use
the -a option to list all the files and the -l
option to provide the long listing, we will
receive the following result.
85.
86. Copying Files
To make a copy of a file use the cp command.
The basic syntax of the command is:
$ cp source_file destination_file
Following is the example to create a copy of
existing file filename.
$ cp filename copyfile
Now you would find one more file copyfile in
your current directory.
This file would be exactly same as original file
filename.
87. Renaming Files
To change the name of a file use the mv
command.
Its basic syntax is:
$ mv old_file new_file
Following is the example which would rename
existing file filename to newfile:
$ mv filename newfile
The mv command would move existing file
completely into new file.
So in this case you would find only newfile in your
current directory
88. Deleting Files
To delete an existing file use the rm command.
Its basic syntax is:
$ rm filename
Following is the example which would
completely remove existing file filename:
$ rm filename
You can remove multiple files at a tile as
follows: $ rm filename1 filename2 filename3
89. Head command
The head command, as the name implies,
print the top N number of data of the given
input. By default, it prints the first 10 lines of
the specified files. If more than one file name
is provided then data from each file is
preceded by its file name.
Syntax:
head [OPTION]... [FILE]...
90. Head command
-n num: Prints the first ‘num’ lines instead of first
10 lines. num is mandatory to be specified in
command otherwise it displays an error.
-c num: Prints the first ‘num’ bytes from the file
specified. Newline count as a single character, so
if head prints out a newline, it will count it as a
byte. num is mandatory to be specified in
command otherwise displays an error.
-v: By using this option, data from the specified
file is always preceded by its file name.
91. Tail command
The tail command, as the name implies, print
the last N number of data of the given input.
By default it prints the last 10 lines of the
specified files. If more than one file name is
provided then data from each file is precedes
by its file name.
Syntax:
tail [OPTION]... [FILE]...
92. Tail command
-n num: Prints the last ‘num’ lines instead of last 10 lines. num is
mandatory to be specified in command otherwise it displays an error.
This command can also be written as without symbolizing ‘n’
character but ‘-‘ sign is mandatory.
Tail command also comes with an ‘+’ option which is not present in
the head command.
With this option tail command prints the data starting from specified
line number of the file instead of end.
For command: tail +n file_name, data will start printing from line
number ‘n’ till the end of the file specified.
-c num: Prints the last ‘num’ bytes from the file specified. Newline
count as a single character, so if tail prints out a newline, it will count
it as a byte. In this option it is mandatory to write -c followed by
positive or negative num depends upon the requirement.
-v: By using this option, data from the specified file is always
preceded by its file name.
93. More command
As 'cat' command displays the file content.
Same way 'more' command also displays the
content of a file. Only difference is that, in case
of larger files, 'cat' command output will scroll
off your screen while 'more' command displays
output one screenful at a time.
Following keys are used in 'more' command to
scroll the page:
• Enter key: To scroll down page line by line.
• Space bar: To go to next page.
• b key: To go to the backward page.
more <file name>
94. Less command
Less command is linux utility which can be used
to read contents of text file one page(one screen)
per time. It has faster access because if file is
large, it don’t access complete file, but access it
page by page.
For example, if it’s a large file and you are reading
it using any text editor, then the complete file will
be loaded to main memory, but less command
don’t load entire file, but load it part by part, which
makes it faster.
less filename
95. Printf command
“printf” command in Linux is used to display the
given string, number or any other format specifier
on the terminal window. It works the same way as
“printf” works in programming languages like C.
Syntax:
$printf [-v var] format [arguments]
Note: printf can have format specifiers, escape
sequences or ordinary characters.
96. Grep command
There are three utilities in the grep family: grep, egrep, and
fgrep.
All these search one or more files and output lines that
contain the text that matches criteria specified as a regular
expression.
The whole line does not have to match the criteria; any
matching text in the line is sufficient for it to be output.
It examines each line in the file, one by one.
When a line contains matching pattern, the line is output.
Although this is a powerful capability that quickly reduces a
large amount of data to a meaningful set of information, it
cannot be used to process only a portion of the data.
The grep family appears in the figure given below: The grep
family: grep, fgrep, and egrep
97. Grep Family
The command grep stands for global regular
expression print.
It is the family of programs that is used to
search the input file for all lines that match a
specified regular expression and write them to
the standard output file (monitor).
The format of grep is shown below:
grep options regexp filelist
98.
99. Think of the pipe as a combination of a monitor and a keyboard. The
input to the pipe operator must come from standard output. This
means that the command on the left that sends output to the pipe
must write its output to standard output.
A pipe operator receives its input from standard output and sends it
to the next command through standard input. This means that the
left command must be able to send data to standard output and the
right command must be able to receive data from standard input.
The token for a pipe is the vertical bar (|). There is no standard
location on the keyboard for the bar. Usually you will find it
somewhere on the right side, often above the return key.
Pipe is an operator not a command. It tells the shell to immediately
take the output of the first command, which must be sent to the
standard output, and turn it into input for the second command,
which must get its input from standard input.
100. Wildcards
Wildcards (also referred to as meta
characters) are symbols or special characters
that represent other characters.
You can use them with any command such
as ls command or rm command to list or
remove files matching a given criteria,
respectively.
101. These wildcards are interpreted by the shell and
the results are returned to the command you run.
There are three main wildcards in Linux:
An asterisk (*) – matches one or more occurrences of
any character, including no character.
Question mark (?) – represents or matches a single
occurrence of any character.
Bracketed characters ([ ]) – matches any occurrence
of character enclosed in the square brackets. It is
possible to use different types of characters
(alphanumeric characters): numbers, letters, other
special characters etc.
102. The Star Wildcard also Known as
ASTERIX ” * “
This is the most frequently employed and
usually the most useful wildcard in linux. The
star wildcard has the broadest meaning of any
of the wildcards. The asterix matches zero or
more characters. It is used in combination of
files and directory names.
Examples: Used *.txt to list all files ending in
txt
Use a* to list all files starting with a
use a *.txt to list all files starting with a and
ending in txt
103. The Question Mark Wildcard “?”
The question mark (?) is used as a wildcard
character in shell commands to represent exactly
one character.
Example. To find all files with one character after
txt, use ?.txt
To match all two lettered files that start with a,
use a?
To match all files starting with a, followed by
exactly 1 character and ends in txt, use a?.txt
104. Square Brackets Wildcard or
Character Class
This represents any of the characters enclosed in the
brackets. Here very specific search patterns are given.
For example, Within the bracket, list the characters you want
to match.
Example :1: da[nt]*
will give you results like dan, daddy etc
Example 2: If you want characters to be excluded, say, you
want all characters that dont start with a vowel, use
[!aeiou]*
results like biscuits, zebra, yellow will be generated.
Example 3: If you want to group or match all characters from
c-m, use [c-m]*
105. Regular Expressions (regex)
Regular Expressions are special characters
which help search data and matching
complex patterns. Regular expressions are
shortened as 'regexp' or 'regex'. They are
used in many Linux programs like grep,
bash, rename, sed, etc.
106.
107. Execute cat sample to see contents of an
existing file
Search for content containing letter 'a’.
Let's search for content that STARTS with a
using ^.
Search for content containing letter ‘t’.
Select only those lines that ENDS with t
using $.
109. Unix - File Permission / Access
Modes
In this chapter, we will discuss in detail about file
permission and access modes in Unix. File ownership
is an important component of Unix that provides a
secure method for storing files. Every file in Unix has
the following attributes −
Owner permissions − The owner's permissions
determine what actions the owner of the file can
perform on the file.
Group permissions − The group's permissions
determine what actions a user, who is a member of
the group that a file belongs to, can perform on the
file.
Other (world) permissions − The permissions for
others indicate what action all other users can perform
112. The Permission Indicators
While using ls -l command, it displays various information related to
file permission as follows −
$ ls -l /home/amrood
-rwxr-xr-- 1 amrood users 1024 Nov 2 00:10 myfile
drwxr-xr--- 1 amrood users 1024 Nov 2 00:10 mydir
Here, the first column represents different access modes, i.e., the
permission associated with a file or a directory.
The permissions are broken into groups of threes, and each position
in the group denotes a specific permission, in this order: read (r),
write (w), execute (x) −
The first three characters (2-4) represent the permissions for the
file's owner. For example, -rwxr-xr-- represents that the owner has
read (r), write (w) and execute (x) permission.
The second group of three characters (5-7) consists of the
permissions for the group to which the file belongs. For example, -
rwxr-xr--represents that the group has read (r) and execute (x)
permission, but no write permission.
The last group of three characters (8-10) represents the
permissions for everyone else. For example, -rwxr-xr-- represents
that there is read (r) only permission.
113. Unix follows a three tiered file protection system that
determines a file’s access rights.
Each group represents a category and contains three
slots, representing the read, write and execute
permissions of the file.
r indicates read permission, which means cat can
display the file.
w indicates write permission, you can edit such file
with an editor.
x indicates execute permission, the file can be
executed as a program.
- shows the absence of the corresponding
permissions.
114. ls –l : listing file atributes
This (-l) option displays most attributes of a file
like – permissions, size and ownership details.
The output in Unix is often referred to as
listing.
Ls –l lists the seven attributes of all files in the
current directory.
We will now briefly describe the significance of
each field in the output.
115. 1. File Type and permissions
2. Links
3. Ownership
4. Group ownership
5. File size
6. Last Modification time
7. File name
116. -rw-rw-r– 1 maverick maverick 1176 Feb 16 00:19 1.c
• Field 1 – File Permissions: Next 9 character specifies the files permission. The
every 3 characters specifies read, write, execute permissions for user(root), group
and others respectively in order. Taking above example, -rw-rw-r– indicates read-write
permission for user(root) , read permission for group, and no permission for others
respectively. If all three permissions are given to user(root), group and others, the
format looks like -rwxrwxrwx
• Field 2 – Number of links: Second field specifies the number of links for that file. In
this example, 1 indicates only one link to this file.
• Field 3 – Owner: Third field specifies owner of the file. In this example, this file is
owned by username ‘maverick’.
• Field 4 – Group: Fourth field specifies the group of the file. In this example, this file
belongs to ”maverick’ group.
• Field 5 – Size: Fifth field specifies the size of file in bytes. In this example, ‘1176’
indicates the file size in bytes.
• Field 6 – Last modified date and time: Sixth field specifies the date and time of the
last modification of the file. In this example, ‘Feb 16 00:19’ specifies the last
modification time of the file.
• Field 7 – File name: The last field is the name of the file. In this example, the file
name is 1.c.
117. Ls –d: listing directory attributes
To force ls to list the attributes of a directory,
rather than its contents, you need to use the –
d(directory) option.
To see the attributes of a directory use ls –ld
with the directory name.
Simply, ls –d will not list all the subdirectories
in the current directory.
118. File Access Modes
The permissions of a file are the first line of defense in
the security of a Unix system. The basic building
blocks of Unix permissions are the read, write, and
execute permissions, which have been described
below −
Read
Grants the capability to read, i.e., view the contents of the
file.
Write
Grants the capability to modify, or remove the content of
the file.
Execute
User with execute permissions can run a file as a program.
119. Directory Access Modes
Directory access modes are listed and organized in
the same manner as any other file. There are a few
differences that need to be mentioned −
Read
Access to a directory means that the user can read the
contents. The user can look at the filenames inside the
directory.
Write
Access means that the user can add or delete files from the
directory.
Execute
Executing a directory doesn't really make sense, so think of
this as a traverse permission.
A user must have execute access to the bin directory
in order to execute the ls or the cd command.
120. Chmod : Changing File
Permissions
The chmod(change mode) commands is used
to set the permissions of one or more files for
all three categories of users(user,
group,others).
It can be done only by the user (the owner)
and the superuser.
The command can be used in two ways:
In a relative manner by specifying the changes
to the current permissions.
In an absolute manner by psecifying the final
permissions.
121. Relative Permissions
When changing permissions in a relative manner,
chmod only changes the permission specified in
the command line and leaves the other permission
unchanged.
It uses the following syntax
Chmod category operation permission
filename(s)
User category ( user, group, others)
The operations to be performed(assign or remove a
permission)
The type of permission (read, write, execute)
By using suitable abbreviations for each of these
components, you can frame a compact expression
and then use it as an argument to chmod.
122. To assign execute permission to the user of the file, we need to frame a
suitable expression by using appropriate characters from each of the three
columns of above table:
$ chmod u+x test
$ ls –l test
-rwxr—r– 1 207………………………….
To enable all of them to execute this file, you have to use multiple
characters to represent the user category (ugo):
$ chmod ugo+x test; ls –l test
-rwxr-xr-x 1 207…………………………………………………………………..
123. Absolute Permissions
Sometimes you don’t need to know what a file’s current permissions
are, but want to set all nine permission bits explicitly.
The expression used by chmod here is a string of three octal
numbers(base 8).
Octal numbers use base 8 and octal digits have the value 0 to 7.
This means that a set of three bits can represent one octal digit.
We represent the permissions of each category by one octal digit.
Read permission – 4 (octal 100)
Write permissions – 2 (octal 010)
Execute Permission – 1 (octal 001)
For each category we add up the numbers. For example, 6
represents read and write permissions and 7 represents all
permissions.
124. Absolute permissions
Binary Octal Permissions Significance
000 0 --- No permissions
001 1 --x Executable only
010 2 -w- Writable only
011 3 -wx Writable and
executable
100 4 r-- Readable only
101 5 r-x Readable and
executable
110 6 rw- Readable and
writable
111 7 rwx Readable,
writable and
executable
125. Example,
$ chmod a+rw test
But in case of absolute permissions :
$chmod 666 test ; ls –l test
-rw-rw-rw- 1 kumar metal 1906 May 10 20:30 test
6 indicates read and write permission(4+2)
To restore original permission to the file, you need to
remove the write permission(2) from group and
others :
126. Using chmod Recursively(-R)
It’s possible to make chmod descend a
directory hierarchy and apply the expression to
every file and subdirectory it finds.
This is done with (-R) option:
chmod –R a+x shell_scripts
This makes all files and subdirectories found in
the tree-walk(that commences from the
shell_scripts directory)executable by all users.
127. Directory Permissions
The directory had its own permissions and links.
Significance of the file attributes change a great
deal when applied to a directory.
Permissions too accquire a different meaning
when applied to directories.
Lets see default permissions
$ ls –l –d progs
drwxr-xr-x 2 kumar metal 320 May 9 09:57 progs
The user has all permissions, group and others
have read and execute permisiions only.
128. Directory : Read Permission
Read permission for a directory means that the list
of filenames stored in that directory is accessible.
Since ls reads the directory to display filenames, if
a directory’s read permission is removed , ls wont
work.
$ ls –ld progs
drwxr-xr-x 2 kumar metal 320 May 9 09:57 progs
$ chmod –r progs ; ls progs
progs: Permission denied
total 2
Being unreadable, the progs directory couldn’t be
accesed by ls, and hence the error message.
129. Directory : Write Permission
Write permission for a directory implies that you
are permitted to create or remove files in it.
To try that out, restore the read permission and
remove the write permission from the directory
before you try to copy a file to it:
$chmod 555 progs; ls –ld progs
dr-xr-xr-x 2 kumar metal 320 May 9 09:57 progs
$ cp emp.lst progs
cp : cannot create progs/emp.lst : Permission denied
The directory doesn’t have write permission; you
can’t create, copy or delete a file in it.
130. Directory : Execute Permission
Executing a directory just doesn’t make any
sense, so what does its execute privilege mean?
It only means that a user can “pass through” the
directory in searching for subdirectories.
A directory has to be searched for the next
directory, so the cd command won’t work if the
search permission for the directory is turned off:
$ chmod 666 progs; ls –ld progs
drw-rw-rw- 2 kumar metal 320 May 9 09:57 progs
$ cd progs
Bash: cd : progs : Permission denied
131. Umask: Default File and Directory
Permissions
When we create a new file or directory, shell
automatically assigns the default permission to it.
Default permission is the subtraction of umask
permission and pre-defined initial permission.
Default permission = pre-defined initial permission –
umask permission
The pre-defined initial permissions for files and
directories are 666 and 777 respectively.
The default umask permissions for root user and
remaining users are 0022 and 0002 respectively.
The pre-defined initial permissions are fixed and
cannot be changed. The default umask permissions
are flexible and can be updated as per requirement.
132. So if our umask value is 022, then any
new files will, by default, have the
permissions 644 (666 - 022).
Likewise, any new directories will, by
default, be created with the
permissions 755 (777 - 022).
133. Changing umask value
To change umask values, following command
is used.
#umask [new values]
For example to change default umask values
to 777, following command is used.
#umask 777
134. Environment Variable
In Linux and Unix based systems environment
variables are a set of dynamic named values,
stored within the system that are used by
applications launched in shells or subshells.
In simple words, an environment variable is a
variable with a name and an associated value.
Environment variables allow you to customize how
the system works and the behavior of the
applications on the system.
For example, the environment variable can store
information about the default text editor or
browser, the path to executable files, or the system
locale and keyboard layout settings
135. List Environment Variable
env – The command allows you to run another
program in a custom environment without
modifying the current one. When used without an
argument it will print a list of the current
environment variables.
printenv – The command prints all or the specified
environment variables.
If the name of the variable is passed as an
argument to the command, only the value of that
variable is displayed. If no argument is specified,
printenv prints a list of all environment variables,
one variable per line.
For example, to display the value of the HOME
environment variable you would run:
printenv HOME
You can also pass more than one arguments to the printenv command:
136. You can also pass more than one arguments
to the printenv command:
printenv LANG PWD
Echo can also be used to display a given ENV
echo $HOME
137.
138. Some commonly used ENVs in
Linux
$USER: Gives current user's name.
$PATH: Gives search path for commands.
$PWD: Gives the path of present working
directory.
$HOME: Gives path of home directory.
$HOSTNAME: Gives name of the host.
$LANG: Gives the default system language.
$EDITOR: Gives default file editor.
$UID: Gives user ID of current user.
$SHELL: Gives location of current user's shell
program.
140. Process
A process is a program in execution in memory or
in other words, an instance of a program in
memory.
Any program executed creates a process.
A program can be a command, a shell script, or
any binary executable or any application.
However, not all commands end up in creating
process, there are some exceptions.
Similar to how a file created has properties
associated with it, a process also has lots of
properties associated to it.
141. Process Attributes
A process has some properties associated to it:
PID : Process-Id. Every process created in Unix/Linux has an identification number
associated to it which is called the process-id. This process id is used by the kernel to
identify the process similar to how the inode number is used for file identification. The
PID is unique for a process at any given point of time. However, it gets recycled.
PPID : Parent Process Id: Every process has to be created by some other process.
The process which creates a process is the parent process, and the process being
created is the child process. The PID of the parent process is called the parent
process id(PPID).
TTY: Terminal to which the process is associated to. Every command is run from a
terminal which is associated to the process. However, not all processes are
associated to a terminal. There are some processes which do not belong to any
terminal. These are called daemons.
UID: User Id- The user to whom the process belongs to. And the user who is the
owner of the process can only kill the process(Of course, root user can kill any
process). When a process tries to access files, the accessibility depends on the
permissions the process owner has on those files.
File Descriptors: File descriptors related to the process: input, output and error file
descriptors.
142. Processes in Linux/Unix
A program/command when executed, a special instance is provided
by the system to the process. This instance consists of all the
services/resources that may be utilized by the process under
execution.
• Whenever a command is issued in unix/linux, it creates/starts a new
process. For example, pwd when issued which is used to list the
current directory location the user is in, a process starts.
• Through a 5 digit ID number unix/linux keeps account of the
processes, this number is call process id or pid. Each process in the
system has a unique pid.
• Used up pid’s can be used in again for a newer process since all
the possible combinations are used.
• At any point of time, no two processes with the same pid exist in the
system because it is the pid that Unix uses to track each process.
143. Types of Processes
1. Parent and Child process : The 2nd and 3rd column of the ps –f command
shows process id and parent’s process id number. For each user process
there’s a parent process in the system, with most of the commands having
shell as their parent.
2. Zombie and Orphan process : A zombie process is a process whose
execution is completed but it still has an entry in the process table. Zombie
processes usually occur for child processes, as the parent process still
needs to read its child’s exit status. Once this is done using the wait system
call, the zombie process is eliminated from the process table. But at times
when the parent process is killed before the termination of the child process,
the child processes becomes orphan processes, with the parent of all
processes “init” process, becomes their new ppid.
Zombie processes don't use any system resources but they do retain their
process ID. If there are a lot of zombie processes, then all the available
process ID’s are monopolized by them. This prevents other processes from
running as there are no process ID’s available.
3. Daemon process : A daemon process is a background process that is not
under the direct control of the user. This process is usually started when the
system is bootstrapped and it terminated with the system shut down. Usually
the parent process of the daemon process is the init process.
144. Daemons are system-related background
processes that often run with the permissions of
root and services requests from other processes.
Some of the examples of daemon processes in
Unix are −
crondThis is a job scheduler that runs jobs in the
background.
syslogdThis is the system logger that implements
the system logging facility and collects system
messages.
httpdThis is the web server daemon process that
handles the Hypertext Transfer Protocol.
dhcpdThis daemon configures the TCP/IP
information for users dynamically.
145. Process states in Unix
UNIX has the following process states:
1. Runnable
2. Running
2. Sleeping or waiting or blocked
3. Stopped
4. Zombie
146.
147. During execution, a process changes from one state to another
depending on its environment/circumstances. In Linux, a process
has the following possible states:
• Running – here it’s either running (it is the current process in
the system) or it’s ready to run (it’s waiting to be assigned to
one of the CPUs).
• Waiting – in this state, a process is waiting for an event to
occur or for a system resource. Additionally, the kernel also
differentiates between two types of waiting processes;
interruptible waiting processes – can be interrupted by signals
and uninterruptible waiting processes – are waiting directly on
hardware conditions and cannot be interrupted by any
event/signal.
• Stopped – in this state, a process has been stopped, usually
by receiving a signal.
• Zombie – here, a process is dead, it has been halted but it’s
still has an entry in the process table.
148. Process state: Running
The most healthy state of all. It indicates the process is active and serving its requests. The
process is properly getting system resources (especially CPU) to perform its operations. Running
process is a process which is being served by CPU currently. It can be identified by state flag R in
ps or top output.
The runnable state is when the process has got all the system resources to perform its operation
except CPU. This means the process is ready to go once the CPU is free. Runnable processes
are also flagged with state flag R
Process state: Sleeping
The sleeping process is the one who waits for resources to run. Since its on the waiting stand, it
gives up CPU and goes to sleep mode. Once its required resource is free, it gets placed in the
scheduler queue for CPU to execute. There are two types of sleep modes: Interruptible and
Uninterruptible
Interruptible sleep mode
This mode process waits for a particular time slot or a specific event to occur. If those conditions
occur, the process will come out of sleep mode. These processes are shown with state S in ps or
top output.
Uninterruptible sleep mode
The process in this sleep mode gets its timeout value before going to sleep. Once the timeout sets
off, it awakes. Or it awakes when waited-upon resources become available for it. It can be
identified by the state D in outputs.
Process state : Stopped
The process ends or terminates when they receive the kill signal or they enter exit status. At this
moment, the process gives up all the occupied resources but does not release entry in the process
table. Instead it sends signals about termination to its parent process. This helps the parent
process to decide if a child is exited successfully or not. Once SIGCHLD received by the parent
process, it takes action and releases child process entry in the process table.
Process state: Zombie
As explained above, while the exiting process sends SIGCHLD to parents. During the time
between sending a signal to parent and then parent clearing out process slot in the process table,
the process enters zombie mode. The process can stay in zombie mode if its parent died before it
releases the child process’s slot in the process table. It can be identified with Z in outputs.
149. How to View Active Processes in
Linux
There are several Linux tools for viewing/listing
running processes on the system, the two
traditional and well known
are ps and top commands:
ps Command
It displays information about a selection of the
active processes on the system as shown
below:
150. top – System Monitoring Tool
top is a powerful tool that offers you a dynamic
real-time view of a running system as shown in
the screenshot below:
151. Top command
The top command is a very useful tool for quickly
showing processes sorted by various criteria.
It is an interactive diagnostic tool that updates
frequently and shows information about physical and
virtual memory, CPU usage, load averages, and your
busy processes.
Here is the simple syntax to run top command and to
see the statistics of CPU utilization by different
processes −
$top
152. Starting a Process
When you start a process (run a command), there
are two ways you can run it −
• Foreground Processes
• Background Processes
Foreground Processes:
By default, every process that you start runs in the
foreground. It gets its input from the keyboard and
sends its output to the screen.
You can see this happen with the ls command. If
you wish to list all the files in your current directory,
you can use the following command −
$ls ch*.doc
153. The process runs in the foreground, the output
is directed to my screen.
While a program is running in the foreground
and is time-consuming, no other commands
can be run (start any other processes)
because the prompt would not be available
until the program finishes processing and
comes out.
154. fg command
fg command in linux used to put a background job in
foreground.
Syntax:
fg [job_spec]
job_spec may be:
%n : Refer to job number n.
%str : Refer to a job which was started by a command
beginning with str.
%- : Refer to the previous job.
155. Background Processes
A background process runs without being
connected to your keyboard. If the background
process requires any keyboard input, it waits.
The advantage of running a process in the
background is that you can run other commands;
you do not have to wait until it completes to start
another!
The simplest way to start a background process is
to add an ampersand (&) at the end of the
command.
$ls ch*.doc &
156. bg command
bg command in linux is used to place foreground jobs in
background.
Syntax:
bg [job_spec ...]
job_spec may be:
%n : Refer to job number n.
%str : Refer to a job which was started by a command beginning
with str.
%- : Refer to the previous job.
157. Stopping Processes
If a process is running in the background, you should get its Job ID using
the ps command. After that, you can use the kill command to kill the process
as follows −
$ps -f
UID PID PPID C STIME TTY TIME CMD
amrood 6738 3662 0 10:23:03 pts/6 0:00 first_one
amrood 6739 3662 0 10:22:54 pts/6 0:00 second_one
amrood 3662 3657 0 08:10:53 pts/6 0:00 -ksh
amrood 6892 3662 4 10:51:50 pts/6 0:00 ps -f
$kill 6738
Terminated
Here, the kill command terminates the first_one process. If a process ignores a
regular kill command, you can use kill -9 followed by the process ID as follows
−
$kill -9 6738
Terminated
158. Process Table in unix
The operating system maintains pointers to
each process’s PCB in a process table so that
it can access the PCB quickly.
159. Process table is a kernel data structure that
describes the state of a process. It contains
fields that must always be available to the
kernel.
160. Process Hierarchy
pstree is a small, command line (i.e., all-text
mode) program that displays the processes (i.e.,
executing instances of programs) on the system in
the form of a tree diagram.
It differs from the much more commonly
used ps program in a number of respects, including
that the latter shows the processes in a list rather
than a tree diagram but provides more detailed
information about them.
161. One of the advantages of pstree as compared with ps is that
it makes it easier to terminate a series of related processes
(i.e., all of the descendants of a particular process).
This is because pstree makes it immediately clear which
process is the parent, and all that is necessary is to
terminate the parent in order to extinguish all of its
descendant processes.
That is, it is not necessary to manually search through a list
to find and individually terminate each process as would be
necessary using ps.
The kill command is commonly used to terminate a crashed
or otherwise misbehaving program or process.
162. pstree syntax
The basic syntax for pstree is:
pstree [options] [pid or username]
The square brackets indicate that the items in them
are optional. If pstree is used without any options or
arguments, that is, by typing
pstree
and then pressing the ENTER key, the result is a tree
diagram that shows all of the processes currently on
the system.
At the very top of the diagram is the process init. This
is because init is always the first process that is started
when Linux is booted up (i.e., started up). It is the
ancestor of all other processes, and it remains on the
system for the duration of the session. It can be seen
that pstree itself is also listed as a process, as is
163.
164. Process Management system calls – fork, getpid, getppid, exit,
wait, exec
Process management uses certain system calls. They
are explained below.
1. To create a new process – fork () is used.
2. To run a new program = exec () is used.
3. To make the process to wait = wait () is used.
4. To terminate the process – exit () is used.
5. To find the unique process id – getpid () is used.
6. To find the parent process id – getppid () is used.
165. Creating a New Process: fork()
- A process may duplicate itself by using “fork()”, which works like this:
System Call: pid_t fork(void)
“fork()” causes a process to duplicate.
The child process is an almost-exact duplicate of the original parent
process; it inherits a copy of its parent’s code, data, stack, open file
descriptors,and signal table.
The parent and child processes have different process ID numbers
and parent process ID numbers.
If “fork()” succeeds, it returns the PID of the child to the parent
process and returns a value of 0 to the child process.
A process may obtain its own process ID and parent process ID
numbers by using the “getpid()” and “getppid()” system calls,
respectively. –
Here’s a synopsis of these system calls:
System Call : pid_t getpid(void)
pid_t getppid(void)
“getpid()” and “getppid()” return a process’ID number and parent
process’ ID number, respectively.
The parent process ID number of PID 1 (i.e., “init”) is 1.
166. Terminating a Process: exit()
- A process may terminate at any time by executing “exit()”, which
works as
follows:
System Call: void exit( int status )
“exit()” closes all of a process’ file descriptors; deallocates its code,
data, and
stack; and then terminates the process.
When a child process terminates, it sends its parent a SIGCHLD
signal and
waits for its termination code status to be accepted.
A process that is waiting for its parent to accept its return code is
called a
zombie process.
A parent accepts a child’s termination code by executing “wait()”.
Orphan Processes
- The kernel ensures that all of a terminating process’ children are
orphaned
and adopted by “init” by setting their PPID to 1.
167. Waiting for a child(): wait()
- A parent process may wait for one of its children to
terminate and then accept
its child’s termination code by executing “wait()”.
System Call : pid_t wait( int* status )
“wait()” causes a process to suspend until one of its children
terminates.
A successful call to “wait()” returns the PID of the child that
terminated.
- If a process executes a “wait()” system call and has no
children, “wait()” returns immediately with a value of -1.
- If a process executes a “wait()” system call and one or more
of its children are already zombies, “wait()” returns
immediately with the status of one of the zombies.
168. Differentiating a Process: exec()
- A process may replace its current code, data,
and stack with those of another executable by
using one of the “exec()” family of system
calls.
- When a process executes an “exec()” system
call, its PID and PPID numbers stay the same
- only the code that the process is executing
changes.
169. Zombie Processes
- A process that terminates cannot leave the
system until its parent accepts its return code.
- If its parent process is already dead, it’ll already
have been adopted by the “init” process, which
always accepts its childrens’ return codes.
- If a process’ parent is alive, but the parent never
executes a “wait()” system call, the process’ return
code will never be accepted and the process will
remain a zombie.
- A zombie process doesn’t have any code, data,
or stack, so it doesn’t use up many system
resources,
170. Password Management in Unix
The passwd command is used to create
and change the password of a user
account. A normal user can
run passwd to change their own
password, and a system administrator
(the superuser ROOT) can
use passwd to change another user’s
password, or define how that account’s
password can be used or changed.
174. Introduction to Shell Scripting
A computer program can run in the UNIX shell command line
interpreter for file manipulation, program execution and
printing text is called Shell Scripting.
Mostly system administration tasks are performed using Shell
scripting such as disk backups, evaluating system logs and
also used as installation scripts for complex programming.
It has a set of commands followed by the Shell that has
some parameters instructing the shell to do certain
operations.
Individual commands follow a sequence and perform the
operations and hence it is known as Shell Script.
This is useful for the repetitive tasks in the system
administration.
175. Working with Shell Scripting
Shell Scripting program contains a series of
command for the shell to execute.
Steps to create Shell Script
1. Create a shell script a file using a vi editor or any
other editor. Give the file name with extension .sh
2. Start the script with #! /bin/sh
3. Write the required code and save file.
4. execute the script file by type bash file.sh or
./file.sh or sh file.sh
The “#!” is an operator which directs the script to
the interpreter location called shebang. So, #!
/bin/sh mean the script gets directed to the Bourne
shell for execution.
176. Shell Scripts
The basic concept of a shell script is a list of
commands, which are listed in the order of
execution. A good shell script will have comments,
preceded by # sign, describing the steps.
It is a simple text file in which we would put all our
commands and several other required constructs
that tell the shell environment what to do and
when to do it.
Shell scripts and functions are both interpreted.
This means they are not compiled.
177. For our first shell script, we'll just write a script
which says "Hello World".
Create a file (first.sh) as follows:
#!/bin/sh
# This is a comment!
echo Hello World
178. Variables
A variable is simply a placeholder for some value. The
value can change; however, the variable name will
always be the same. Shell variables are capitalized by
convention. The shell maintains two lists of variables:
1. Those local to the current shell
2. Those global to all shells (environment
variables).
Use the set and env commands to display local and
environmental variables, respectively. The following is
a partial output of the set and env statements. Many
variables appear in both the local and environment
variable list.
179. – Shell variables are capitalized by convention.
– Variable names can contain uppercase or
lowercase letters, digits, and underscores.
– Variable names cannot begin with a digit.
– Be sure to use meaningful variable names.
– Variable names have a limit of 255 characters.
–Do not place spaces around the = sign. If the
value contains spaces or special characters, use
single or double quotes; for example:
$ var="variable with spaces"
180. Display the value of variable
To display the value of a shell variable, use the
echo command and place a $ immediately in front
of the variable name. You can also display more
than one variable in the same line using single
echo command. For Example :
$ var1=18
$ var2="Till I Die"
$ echo $var1 $var2
18 Till I Die
181. We can interactively set variable names using
the read command; the following script asks
you for your name then greets you personally:
Var.sh
#!/bin/sh
echo What is your name?
read MY_NAME
echo "Hello $MY_NAME - hope you're well."
182. The expr command
The expr or the expression command in Linux is the most commonly
used command that is used to perform mathematical calculations.
You can use this command to perform functions like addition,
subtraction, multiplication, division, incrementing a value and, even
comparing two values.
The operators available for integers: addition, subtraction,
multiplication, division and modulus for strings: find regular
expression, find a set of characters in a string; in some versions: find
substring, length of string for either: comparison (equal, not equal,
less than, etc.)
You can perform multiplication of two or more numbers through the
expr command as follows:
$ expr number1 * number2
Please note that following is the wrong syntax for number
multiplication in the Linux command line as the command line uses
simple asterisks as a reference to all files in the current directory.
Addition
You can add two or more numbers through the expr command as follows:
$ expr number1 + number2
183. Types of if condition in shell script
Now its time for understanding the types of if conditional
statements.
1. Simple if statement
In this type of statement, only the if condition is used, which
essentially means that the conditions in if conditions will be
tested all along even if one of the conditions is satisfied. This
type of condition is eventually used when the user wants to go
through all the possible checks and not end the checks when
one of the conditions is true.
Syntax:
if <expression for checking>;
then
<set of commands to be executed>
fi
184. 2. If-Else condition
This type of statement is used when the program needs to
check one condition and perform a task if the condition is
satisfied or perform the other set of tasks if the condition is
not.
Syntax:
if <expression for checking>;
then
<set of commands to be executed>
else
<set of other commands to be executed>
fi
185. 3. If elif else fi statement
Starting from here, and the next type of if condition statement, the condition starts
becoming branched. This type of statement is more like a ladder. If the condition is not
satisfied one would feel like start climbing down the ladder and only stops if one condition
is satisfied.
Syntax:
if <expression 1 for checking>;
then
<set of commands to be executed>
.
.elif <expression 2 for checking>
then
<set of other commands to be executed>
.
.
else
<set of else set of commands to be executed>
fi
187. Test command
The test command is used to perform checks
and comparisons. Here's its syntax:
test EXPRESSION
And here's what the man page says about this
utility:
test - check file types and compare values
188. test exits with the status determined by
EXPRESSION.
Placing the EXPRESSION between square
brackets ([ and ]) is the same as testing
the EXPRESSION with test.
To see the exit status at the command
prompt, echo the value "$?"
A value of 0 means the expression
evaluated as true, and a value of 1
means the expression evaluated as false.
189. Simple, you just need to use the equal to (=) sign
between them. For example:
test howto = forge
A better way would be to write something like this:
test howto = forge && echo "same“
This is also very straight forward - just compare
them using '-eq'. For example:
test 5 -eq 7 && echo "same“
To check whether a given file is a directory, use the -d
option in the following way:
test -d [filename]
192. Operators in Unix
In shell also there are variables and operators
that are used to manipulate these variables.
There are 5 basic operators in shell scripting.
• Arithmetic Operators
• Relational Operators
• Boolean Operators
File Test Operators
193. Arithmetic Operators
Arithmetic operators in shell scripting are used to perform general
arithmetic/ mathematical operations. There are 7 valid arithmetic operators
in shell scripting −
• Addition (+) is used to add two operands (variables).
• Subtraction (-) is used to subtract two variables (operands) in shell
scripting.
• Multiplication (*) is used to multiply two variables (operands) in shell
scripting.
• Division (/) is used to divide two variables (operands) in shell scripting.
• Modulus (%) is used to find the remainder on division of operands in shell
scripting.
• Increment operator (++) is used to add one to the current value of the
operator.
• Decrement operator (--) is used to subtract one from the current value of
the operator.
194. Relational Operator
The relational operator in shell scripting defines the relations between operands. The
return value of these are either true or false depending on the operator and
operands. There are 6 types of valid relational operators in shell scripting −
• == operator is the operator that equates the values of two operators. It returns true if
the values are equal and returns false otherwise.
• != operator is the operator that equates the values of two operators and check for
their inequality. It returns true if the values are not equal and returns false otherwise.
• < operator is the less than operator comparing the values of two operators. If first
operend’s value is smaller than seconds one then operator returns true otherwise
returns false.
• <= operator is less than or equal to operator that compares the values of two
operators. If first operend’s value is smaller than or equal to seconds one then
operator returns true otherwise returns false.
• >operator is the greater than operator comparing the values of two operators. If first
operend’s value is larger than seconds one then operator returns true otherwise
returns false.
• >= operator is greater than or equal to operator that compares the values of two
operators. If first operend’s value is larger than or equal to seconds one then
operator returns true otherwise returns false.
195. Logical Operators
We use the logical operators to test more than
one condition.
The logical AND -a operator will give true if
both the operands are true.
The logical OR -o operator will give true if any
one of the operand is true.
Operator Description
-a Logical AND
-o Logical OR
198. File Test Operator
The file test operators are used to test particular properties of the file. Some
of the file test operators are :
• -b operator is used to check if the specified file is a block special file or not.
If the file is a block special file then the function returns true otherwise
returns false.
• -s operator is the operator that is used to check the size of the given file. If
the file size is greater than 0, it returns true otherwise returns false.
• -r operator is the operators that check if the access to read file contents is
granted or not. If read access is granted than it returns true otherwise false.
• -w operator is the operators that check if the access to write into file is
granted or not. If write access is granted than it returns true otherwise false.
• -x operator is the operators that check if the access to execute the file is
granted or not. If execution access is granted than it returns true otherwise
false.
199. Types of Loops in Shell
Scripting
Below are the 3 different types of loops in shell scripting which are as follows:
1. While Loop
2. For Loop
3. Until Loop
The while loop syntax in the shell scripting will be represented in the following way.
Syntax:
while [ condition ] do
command1
command2
done
In the above syntax, initially, the condition will be evaluated if it is true then the
commands inside the loop will execute until the condition remains true else it will
come outside of the loop. Here while, do, and done are built-in keywords. It will
execute all the commands while the condition evaluates to true.
200. Example:
While loop to display numbers from 1 to 10.
Code:
number = 1
while [ $number –lt 11 ]
do
echo $number
((number++))
done
201. 2. For Loop
For loop is another type of looping statement to execute a set
of commands for a certain number of times. Let’s have a look
at the syntax of for loop in shell scripting and it can be
represented as below:
syntax:
for var in list
do
command 1
command 2
done
202. For loop, example to display names from a list.
Code:
for p_name in Stan Kyle Cartman
do
echo $p_name
done
Output:
203. 3. Until Loop
Until loop is one of the looping statements in the shell
scripting and this looping statement is similar to the while loop
statement which we have discussed earlier. The difference
between two is, it will execute the body of the loop until the
conditional statement becomes true whereas while loop
executes commands if the condition is true. Let us have a
look at the syntax of until loop in the shell scripting as below:
Syntax:
until [ conditional statement ] do
command1
command2
done
204. Example:
Until example to display numbers from 1 to 10.
Code:
number = 1
until [ $number –gt 10 ] do
echo $number
((number++))
done
205. Using Continue is shell scripts
As the name would suggest, continue command would allow us to skip the
current step of the loop & continue to next step. Let’s see an example of`
this implementation,
#!/bin/bash
for i in 1 2 3 4 5 6 7 8 9
do
if [ $i -eq 3 ]
then
echo “skipping number 3”
continue
fi
echo “I is equal to $i”
done
So here in this for loop, we have mentioned numbers from 1 to 9 & for every
number it will print I is equal to the mentioned number, but when the number
3 comes, it will skip it & continue to next number.
206. Using Break command in shell
scripts
Break command is used when we need to exit out of the current step as well from the
loop completely. This is especially useful when we are not sure as to how long the
loop will last like when we require user input. Let’s discuss how we can use break
command in shell script,
#!/bin/bash
num=1
while [ $num -lt 10 ]
do
if [ $num -eq 4 ]
then
break
fi
done
echo “Loop is complete”
This script is quite similar to the script we mentioned as an example for continue
command with difference being instead of using continue command, we will be using
break command in shell script. Now the loop will completely exit out once the number
reaches 4 & will not go any further.
207.
208. Unix Commands Quiz
1. What command clears the contents of your terminal display?
2. What is the command to delete a file?
3. What do you type in to move to the parent directory?
4. What command is used to change directories?
5. What command and parameter (or switch) will force a program to quit (even one running in the
background)?
6. What command is used to change ownership of a file?
7. What command is used to copy a file?
8. What command shows you what directory you are in?
9. What command creates an empty directory?
10. What command displays your current username?
11. What command shows you CPU and memory utilization for running processes?
12. What command allows you to open and view a file one page at a time?
13. Which command(s) show users that are logged in?
14. What command is used to change a file name?
15. What command is used to change the permissions of a file?
16. What is the command to change your password?
17. What command is used to display your previous commands?
18. What command is used to display files in the current directory long format.?
19. Which command is used to display the help for the command?
20. Which command is used to create an empty file?